#!/usr/bin/perl -w # # Copyright (c) 2012 Ralf Schiffers # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # ################################################################################ # This script is used check the the bayes database of Spamassassin is up-to-date # and running. As we cannot tie directly to the database we use a file generated # by sa-learn as input. # If the newest entry in the database is older than the specified time (default # for warning is 60 minutes, for critical 120 minuts) it will trigger warning/ # critical state. # # Furthermore the scripts prints the number of spams, hams and tokens in # Spamassassins bayes database. # # In order to use this script, you might need to make the following # adjustments: # - Set the path to your sa-learn output. # - setup a cronjob to generate the necessary sa-learn output # Example: 30 * * * * /usr/bin/sa-learn -u amavis --dbpath /var/amavis/.spamassassin/bayes --dump magic > /tmp/sabayes # # This scripts needs the following perl modules (available via CPAN) # - Getopt::Long # ################################################################################ use strict; use Getopt::Long; use vars qw ($opt_h $opt_w $opt_c); # Where is sa-learn output generated by cron-job? my $sa_output = "/tmp/sabayes"; # Default warning and critical of age of newest token in database (minutes) my $warn_level = 60; my $crit_level = 120; ######################## Nothing to change below this ########################## my $script = "check_bayes"; my $script_version = "1.0.0"; # Check sa-learn output exists and is readable for us if (!-r $sa_output) { die("ERROR: Unable to read $sa_output"); } # Get the Options Getopt::Long::Configure('bundling'); GetOptions ("h" => \$opt_h, "help" => \$opt_h, "w=i" => \$opt_w, "warning=i" => \$opt_w, "c=i" => \$opt_c, "critical=i" => \$opt_c, ); if ($opt_h) { usage(); } $warn_level = $opt_w if defined $opt_w; $crit_level = $opt_c if defined $opt_c; if ($warn_level >= $crit_level) { die ("ERROR: Warning must not be greater or equal than Critical"); } # Get the current date to compare it with bayes-Database entry my $current_timescan = time(); # Get the date of current database my @sa_learn_output; open (FILE, $sa_output) || die $!; while () { my $zeile = $_; chomp ($zeile); push (@sa_learn_output, $zeile); } close FILE; # Get number of spam mails in database my @spams = split(/ +/,$sa_learn_output[1]); my $spamcount = $spams[2]; # Get number of ham mails in database my @hams = split(/ +/,$sa_learn_output[2]); my $hamcount = $hams[2]; # Get number of tokens in database my @satokens = split(/ +/,$sa_learn_output[3]); my $satokencount = $satokens[2]; # Get timestamp of newest token in database my @newesttoken = split(/ +/,$sa_learn_output[5]); my $newest_token_stamp = $newesttoken[2]; # Difference between current time and newest timestamp my $time_diff = int (($current_timescan - $newest_token_stamp) / 60); # Compare the times and set exit code for Nagios my $bayes_status = "Spamassassin bayes OK - newest entry is $time_diff minutes old ($spamcount Spams, $hamcount Hams, $satokencount Tokens in Bayes database)\n"; my $plugin_status = 0; if ($time_diff >= $crit_level) { $bayes_status = "Spamassassin bayes CRITICAL - newest entry is $time_diff minutes old ($spamcount Spams, $hamcount Hams, $satokencount Tokens in Bayes database)\n"; $plugin_status = 2; } elsif ($time_diff >= $warn_level) { $bayes_status = "Spamassassin bayes WARNING - newest entry is $time_diff minutes old ($spamcount Spams, $hamcount Hams, $satokencount Tokens in bayes database)\n"; $plugin_status = 1; } print $bayes_status; exit $plugin_status; ################################################################################ # help and usage information # ################################################################################ sub usage { print << "USAGE"; ----------------------------------------------------------------- $script v$script_version Nagios plugin to check if Spamasassin bayes database is working Usage: $script -w -c Options: -h show this help -w generate a warning state if newest entray in bayes database is older than minutes (default is 60) -c generate a critical state if newest entra in bayes database is older than minutes (default is 120) ----------------------------------------------------------------- Copyright (C) 2012 Ralf Schiffers. All rights reserved This program is free software; you can redistribute it or modify it under the terms of the GNU General Public License ----------------------------------------------------------------- USAGE exit 0; }