check_amavis.sh: restart entire server if at least two checks failed.

This commit is contained in:
Christoph 2022-02-08 18:46:51 +01:00
parent 15a4c23f15
commit 4402e143f5
3 changed files with 266 additions and 51 deletions

150
OLD/check_amavis.sh.01 Executable file
View File

@ -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 <glen@pld-linux.org>
# 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 <<ENDE;
Test of the AMaViS Service.
Programm sends a (test-)email to the AmaVis daemon. If service is
unreachable, AMaViS Daemon will be restartet. If all is fine, the
programm ends silently.
Usage:
$prog -f <from-addresse> [-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 ;
}

View File

@ -1,14 +1,15 @@
#!/usr/bin/perl #!/usr/bin/perl
# Downloaded from exchange.nagios.org # 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 <glen@pld-linux.org> # Maintained later on by Elan Ruusamäe <glen@pld-linux.org>
# http://cvs.pld-linux.org/packages/nagios-plugin-check_amavis/ # https://github.com/glensc/monitoring-plugin-check_amavis
# v1.1, 2011-12-22
use Getopt::Long; use Getopt::Long;
use MIME::Entity; use MIME::Entity;
use Net::SMTP; use Net::SMTP;
use strict;
use warnings;
my $server = ''; my $server = '';
my $port = 10024; my $port = 10024;
@ -16,22 +17,26 @@ my $from = '';
my $to = ''; my $to = '';
my $debug = 0; my $debug = 0;
my $help = 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 = ( my %STATES = (
"OK" => 0, "OK" => 0,
"WARNING" => 1, "WARNING" => 1,
"CRITICAL" => 2, "CRITICAL" => 2,
"UNKNOWN" => 3, "UNKNOWN" => 3,
"DEPENDENT" => 4, "DEPENDENT" => 4,
); );
$result = GetOptions ( GetOptions (
"server|s=s" => \$server, "server|s=s" => \$server,
"port|p=s" => \$port, "port|p=s" => \$port,
"from|f=s" => \$from, "from|f=s" => \$from,
"debug|d" => \$debug, "timeout=s" => \$timeout,
"to|t=s" => \$to, "debug|d" => \$debug,
"help|h" => \$help, "to|t=s" => \$to,
"help|h" => \$help,
); );
if ( $help ) { if ( $help ) {
@ -39,12 +44,12 @@ if ( $help ) {
} }
if (!$server || !$from) { if (!$server || !$from) {
print "ERROR: Please specify --server, --from\n"; print "ERROR: Please specify --server, --from\n";
exit $STATES{UNKNOWN}; exit $STATES{UNKNOWN};
} }
if (!$to) { if (!$to) {
$to = $from; $to = $from;
} }
my $EICAR = <<'EOF'; my $EICAR = <<'EOF';
@ -52,31 +57,62 @@ X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
EOF EOF
my $top = MIME::Entity->build( my $top = MIME::Entity->build(
Type => "multipart/mixed", Type => "multipart/mixed",
From => $from, From => $from,
To => $to, To => $to,
Subject => "EICAR test", Subject => "EICAR test",
Data => "This is a test", Data => "This is a test",
); );
$top->attach( $top->attach(
Data => $EICAR, Data => $EICAR,
Type => "application/x-msdos-program", Type => "application/x-msdos-program",
Encoding => "base64", Encoding => "base64",
); );
my $smtp = new Net::SMTP( my $smtp = new Net::SMTP(
$server, $server,
Port => $port, Port => $port,
Debug => $debug, Debug => $debug,
Timeout => $timeout,
); );
if (!$smtp) { 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}; exit $STATES{CRITICAL};
} }
@ -88,19 +124,48 @@ $smtp->dataend();
my $result = $smtp->message(); my $result = $smtp->message();
$smtp->close(); $smtp->close();
if ($result =~/2.7.[01] Ok, discarded/) { warn "RESULT[$result]\n" if $debug;
#print "\n$result\n";
# <<< 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}; exit $STATES{OK};
} else { } else {
print "[ Warning ]: Respond of amavisd-new service is not as expected !\n"; #print "CRITICAL - amavisd-new returned $result\n";
print " amavisd-new returned:\n $result\n\n";
print "\n\nRestart Service amavisd-new now..\n";
# - Restart Service amavisd-new if (-e $check_file) {
# -
system("/etc/init.d/amavis", "stop"); print "\n";
sleep 2; print " [ Error ]: AMaViS Service was restarted during the last check\n";
system("/etc/init.d/amavis", "start"); 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}; exit $STATES{CRITICAL};
} }
@ -114,13 +179,13 @@ sub usage {
print <<ENDE; print <<ENDE;
Test of the AMaViS Service. Test of the AMaViS Service.
Programm sends a (test-)email to the AmaVis daemon. If service is Programm sends a (test-)email to the AmaVis daemon. If service is
unreachable, AMaViS Daemon will be restartet. If all is fine, the unreachable, AMaViS Daemon will be restartet. If all is fine, the
programm ends silently. programm ends silently.
Usage: Usage:
$prog -f <from-addresse> [-h] [-d] [-f from-address] [-t to-address] [-s server] [-p port-number] $prog -f <from-addresse> [-h] [-d] [-f from-address] [-t to-address] [-s server] [-p port-number]
-d. --debug -d. --debug
@ -133,16 +198,16 @@ Usage:
Prints this helpmessage. Prints this helpmessage.
-p PORT, --port PORT -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 Server, where AmaVis is running. Defaults to "127.0.0.1", if not given
-t EMAIL, --to EMAIL -t EMAIL, --to EMAIL
Recipient of the Email. Defaults to the value given by --from option Recipient of the Email. Defaults to the value given by --from option
Example: 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 ENDE