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
# 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>
# 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,6 +17,9 @@ 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,
@ -25,10 +29,11 @@ my %STATES = (
"DEPENDENT" => 4,
);
$result = GetOptions (
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,
@ -69,14 +74,45 @@ my $smtp = new Net::SMTP(
$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;
}
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
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("/etc/init.d/amavis", "stop");
sleep 2;
system("/etc/init.d/amavis", "start");
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};
}