#!/usr/bin/perl # Downloaded from exchange.nagios.org # URL: https://exchange.nagios.org/directory/Plugins/Anti-2DVirus/Amavis/check_amavis/details # # Maintained later on by Elan Ruusamäe # 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; my $from = ''; my $to = ''; my $debug = 0; my $help = 0; my $timeout = 15; my $check_file = "/tmp/amavis-service-down.check_amavis"; my %STATES = ( "OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3, "DEPENDENT" => 4, ); 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 ) { &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, Timeout => $timeout, ); if (!$smtp) { #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; } exit $STATES{CRITICAL}; } $smtp->mail($from); $smtp->to($to); $smtp->data(); $smtp->datasend($top->stringify); $smtp->dataend(); my $result = $smtp->message(); $smtp->close(); 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 "CRITICAL - amavisd-new returned $result\n"; 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}; } # # - 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 ; }