From 4402e143f5b57546b1243931f09b90874ab3808e Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 8 Feb 2022 18:46:51 +0100 Subject: [PATCH] check_amavis.sh: restart entire server if at least two checks failed. --- OLD/{check_amavis.sh => check_amavis.sh.00} | 0 OLD/check_amavis.sh.01 | 150 ++++++++++++++++++ check_amavis.pl | 167 ++++++++++++++------ 3 files changed, 266 insertions(+), 51 deletions(-) rename OLD/{check_amavis.sh => check_amavis.sh.00} (100%) create mode 100755 OLD/check_amavis.sh.01 diff --git a/OLD/check_amavis.sh b/OLD/check_amavis.sh.00 similarity index 100% rename from OLD/check_amavis.sh rename to OLD/check_amavis.sh.00 diff --git a/OLD/check_amavis.sh.01 b/OLD/check_amavis.sh.01 new file mode 100755 index 0000000..2aba967 --- /dev/null +++ b/OLD/check_amavis.sh.01 @@ -0,0 +1,150 @@ +#!/usr/bin/perl +# Downloaded from exchange.nagios.org +# URL: http://exchange.nagios.org/directory/Plugins/Anti-2DVirus/Amavis/check_amavis/details +# +# Maintained later on by Elan Ruusamäe +# http://cvs.pld-linux.org/packages/nagios-plugin-check_amavis/ +# v1.1, 2011-12-22 + +use Getopt::Long; +use MIME::Entity; +use Net::SMTP; + +my $server = ''; +my $port = 10024; +my $from = ''; +my $to = ''; +my $debug = 0; +my $help = 0; + +my %STATES = ( + "OK" => 0, + "WARNING" => 1, + "CRITICAL" => 2, + "UNKNOWN" => 3, + "DEPENDENT" => 4, +); + +$result = GetOptions ( + "server|s=s" => \$server, + "port|p=s" => \$port, + "from|f=s" => \$from, + "debug|d" => \$debug, + "to|t=s" => \$to, + "help|h" => \$help, +); + +if ( $help ) { + &usage ; +} + +if (!$server || !$from) { + print "ERROR: Please specify --server, --from\n"; + exit $STATES{UNKNOWN}; +} + +if (!$to) { + $to = $from; +} + +my $EICAR = <<'EOF'; +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* +EOF + +my $top = MIME::Entity->build( + Type => "multipart/mixed", + From => $from, + To => $to, + Subject => "EICAR test", + Data => "This is a test", +); + +$top->attach( + Data => $EICAR, + Type => "application/x-msdos-program", + Encoding => "base64", +); + +my $smtp = new Net::SMTP( + $server, + Port => $port, + Debug => $debug, +); + +if (!$smtp) { + print "\n[ Error ]: amavisd-new server unreachable\n Restarting Server now..\n"; + + # - Restart Server + # - + system("/sbin/reboot -f"); + exit $STATES{CRITICAL}; +} + +$smtp->mail($from); +$smtp->to($to); +$smtp->data(); +$smtp->datasend($top->stringify); +$smtp->dataend(); +my $result = $smtp->message(); +$smtp->close(); + +if ($result =~/2.7.[01] Ok, discarded/) { + #print "\n$result\n"; + exit $STATES{OK}; +} else { + print "[ Warning ]: Respond of amavisd-new service is not as expected !\n"; + print " amavisd-new returned:\n $result\n\n"; + print "\n\nRestart Service amavisd-new now..\n"; + + # - Restart Service amavisd-new + # - + system("/etc/init.d/amavis", "stop"); + sleep 2; + system("/etc/init.d/amavis", "start"); + + exit $STATES{CRITICAL}; +} + +# +# - usage +# +sub usage { + my $prog = $0; + $prog =~ s#.*/([^/]+)$#$1# ; + + print < [-h] [-d] [-f from-address] [-t to-address] [-s server] [-p port-number] + + -d. --debug + Makes output verbose + + -f EMAIL, --from EMAIL + From address + + -h, --help + Prints this helpmessage. + + -p PORT, --port PORT + Serverport, wher AmaVis is listening. Defaults to "10024", if not given + + -s SERVER, --server SEVER + Server, where AmaVis is running. Defaults to "127.0.0.1", if not given + + -t EMAIL, --to EMAIL + Recipient of the Email. Defaults to the value given by --from option + +Example: + $prog -f postmaster\@mx.warenform.de -t do-not-reply\@mx.warenform.de -s 127.0.0.1 -p 10024 + +ENDE + + exit 1 ; +} diff --git a/check_amavis.pl b/check_amavis.pl index 2aba967..4fba62f 100755 --- a/check_amavis.pl +++ b/check_amavis.pl @@ -1,14 +1,15 @@ #!/usr/bin/perl # Downloaded from exchange.nagios.org -# URL: http://exchange.nagios.org/directory/Plugins/Anti-2DVirus/Amavis/check_amavis/details +# URL: https://exchange.nagios.org/directory/Plugins/Anti-2DVirus/Amavis/check_amavis/details # # Maintained later on by Elan Ruusamäe -# http://cvs.pld-linux.org/packages/nagios-plugin-check_amavis/ -# v1.1, 2011-12-22 +# https://github.com/glensc/monitoring-plugin-check_amavis use Getopt::Long; use MIME::Entity; use Net::SMTP; +use strict; +use warnings; my $server = ''; my $port = 10024; @@ -16,22 +17,26 @@ my $from = ''; my $to = ''; my $debug = 0; my $help = 0; +my $timeout = 15; +my $check_file = "/tmp/amavis-service-down.check_amavis"; +my $check_file2 = "/tmp/amavis-service-down.check_amavisi2"; my %STATES = ( - "OK" => 0, - "WARNING" => 1, - "CRITICAL" => 2, - "UNKNOWN" => 3, - "DEPENDENT" => 4, + "OK" => 0, + "WARNING" => 1, + "CRITICAL" => 2, + "UNKNOWN" => 3, + "DEPENDENT" => 4, ); -$result = GetOptions ( - "server|s=s" => \$server, - "port|p=s" => \$port, - "from|f=s" => \$from, - "debug|d" => \$debug, - "to|t=s" => \$to, - "help|h" => \$help, +GetOptions ( + "server|s=s" => \$server, + "port|p=s" => \$port, + "from|f=s" => \$from, + "timeout=s" => \$timeout, + "debug|d" => \$debug, + "to|t=s" => \$to, + "help|h" => \$help, ); if ( $help ) { @@ -39,12 +44,12 @@ if ( $help ) { } if (!$server || !$from) { - print "ERROR: Please specify --server, --from\n"; - exit $STATES{UNKNOWN}; + print "ERROR: Please specify --server, --from\n"; + exit $STATES{UNKNOWN}; } if (!$to) { - $to = $from; + $to = $from; } my $EICAR = <<'EOF'; @@ -52,31 +57,62 @@ X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* EOF my $top = MIME::Entity->build( - Type => "multipart/mixed", - From => $from, - To => $to, - Subject => "EICAR test", - Data => "This is a test", + Type => "multipart/mixed", + From => $from, + To => $to, + Subject => "EICAR test", + Data => "This is a test", ); $top->attach( - Data => $EICAR, - Type => "application/x-msdos-program", - Encoding => "base64", + Data => $EICAR, + Type => "application/x-msdos-program", + Encoding => "base64", ); my $smtp = new Net::SMTP( - $server, - Port => $port, - Debug => $debug, + $server, + Port => $port, + Debug => $debug, + Timeout => $timeout, ); if (!$smtp) { - print "\n[ Error ]: amavisd-new server unreachable\n Restarting Server now..\n"; + #print "CRITICAL - amavisd-new server unreachable\n"; + + if (-e $check_file) { + + print "\n"; + print " [ Error ]: AMaViS Service was restarted during the last check\n"; + print " but is still not available. \n"; + print "\n"; + print " Going to restart the entire server now.\n\n"; + + unlink($check_file); + + # - Restart Server + # - + system("/sbin/reboot -f"); + + } else { + + print "\n"; + print " [ Warning ]: amavisd-new service unreachable\n"; + print "\n"; + print " Going to restart AMaViS Service now..\n\n"; + + # - Restart Service amavisd-new + # - + sleep(2); + system("systemctl stop amavis"); + sleep(3); + system("systemctl start amavis"); + + open my $FH,'>', $check_file; + close $FH; + + } - # - Restart Server - # - - system("/sbin/reboot -f"); exit $STATES{CRITICAL}; } @@ -88,19 +124,48 @@ $smtp->dataend(); my $result = $smtp->message(); $smtp->close(); -if ($result =~/2.7.[01] Ok, discarded/) { - #print "\n$result\n"; +warn "RESULT[$result]\n" if $debug; + +# <<< 250 2.5.0 Ok, id=21563-09, BOUNCE +if ($result =~ /2\.7\.[01] Ok,/ && $result =~ /discarded|BOUNCE/) { + #print "OK - All fine\n"; + + unlink($check_file); + exit $STATES{OK}; } else { - print "[ Warning ]: Respond of amavisd-new service is not as expected !\n"; - print " amavisd-new returned:\n $result\n\n"; - print "\n\nRestart Service amavisd-new now..\n"; + #print "CRITICAL - amavisd-new returned $result\n"; - # - Restart Service amavisd-new - # - - system("/etc/init.d/amavis", "stop"); - sleep 2; - system("/etc/init.d/amavis", "start"); + if (-e $check_file) { + + print "\n"; + print " [ Error ]: AMaViS Service was restarted during the last check\n"; + print " but is still not responding as expected. \n"; + print "\n"; + print " Going to restart the entire server now.\n\n"; + + unlink($check_file); + + # - Restart Server + # - + system("/sbin/reboot -f"); + + } else { + print "\n"; + print " [ Warning ]: Respond of amavisd-new service is not as expected !\n"; + print "\n"; + print " amavisd-new returned:\n $result\n"; + print "\n"; + print " Going to restart Service amavisd-new now.\n\n"; + + open my $FH,'>', $check_file; + close $FH; + + sleep(2); + system("systemctl stop amavis"); + sleep(3); + system("systemctl start amavis"); + } exit $STATES{CRITICAL}; } @@ -114,13 +179,13 @@ sub usage { print < [-h] [-d] [-f from-address] [-t to-address] [-s server] [-p port-number] -d. --debug @@ -133,16 +198,16 @@ Usage: Prints this helpmessage. -p PORT, --port PORT - Serverport, wher AmaVis is listening. Defaults to "10024", if not given + Serverport, wher AmaVis is listening. Defaults to "10024", if not given - -s SERVER, --server SEVER + -s SERVER, --server SEVER Server, where AmaVis is running. Defaults to "127.0.0.1", if not given -t EMAIL, --to EMAIL Recipient of the Email. Defaults to the value given by --from option Example: - $prog -f postmaster\@mx.warenform.de -t do-not-reply\@mx.warenform.de -s 127.0.0.1 -p 10024 + $prog -f postmaster\@mx.warenform.de -t do-not-reply\@mx.warenform.de -s 127.0.0.1 -p 10024 ENDE