From eb0065d0b0ba5cb09c041de06b71ba2eb62fd56b Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 13 Feb 2017 11:46:15 +0100 Subject: [PATCH] - add check_amavis.sh - add check_ownership.sh --- check_amavis.sh | 150 +++++++++++++++++++++++++++++++++++++++++++++ check_ownership.sh | 139 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100755 check_amavis.sh create mode 100755 check_ownership.sh diff --git a/check_amavis.sh b/check_amavis.sh new file mode 100755 index 0000000..1797bd9 --- /dev/null +++ b/check_amavis.sh @@ -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"; + print "\n\nRestart Service amavisd-new now.."; + + # - 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_ownership.sh b/check_ownership.sh new file mode 100755 index 0000000..d132231 --- /dev/null +++ b/check_ownership.sh @@ -0,0 +1,139 @@ +#!/usr/bin/env bash + +function usage () { + + if [ -n "$1" ];then + echo -e "\nError: $1" + fi + +cat< [-g groupname] + +`basename $0` proofs ownership of the given pathname, and change it to the given username +if needed. If flag "-g" is given, also group membership will be changed. + +If pathname is a directory, all regular files inside will be proofed and also changed +if needed. If flag "-r" is present, also subdirectories will be considered. + +Mandytory Options: + + -u username + name of the user, that should own the given file(s) + + +Optinal Control Options: + + -h + print that help text + + + -g groupname + name of the group, the given file(s) should be member of. + + + -r + means recursive. script also traverses into subdirectories + + +Example: + + /root/bin/check_ownership.sh -u amavis -g amavis /var/lib/amavis/.spamassassin + +EOF +exit + +} + + +recursiv=false +while getopts d:g:hru: opt ; do + case $opt in + h) usage ;; + u) + user="$OPTARG" + ;; + g) group="$OPTARG" ;; + r) recursiv=true ;; + \?) usage ;; + esac +done + +shift `expr $OPTIND - 1` + +path=$1 + + +if [ -z "$path" ];then + usage "Es wurde kein Verzeichnis angegeben" +fi + +if [ ! -d "$path" -a ! -f "$path" ];then + usage "\"$path\" is neither a directory nor a regular File" +fi + +if [ -z "$user" ];then + usage "Benutzername (-u) muss angegeben werden" +fi + +[ $# -ne "1" ] && usage "wrong number of arguments" + + +if [ -z "$group" ] ; then + _ownership=$user + check_group=false +else + _ownership="$user:$group" + check_group=true +fi + + +function check_owner () { + _path=$1 + change_ownership=false + + if [ -d $_path -o -f $_path ]; then + _u=`stat -c "%U" $_path` + _o="$_u" + if [ "$_u" != "$user" ]; then + change_ownership=true + fi + if $check_group ; then + _g=`stat -c "%G" $_path` + _o="${_u}:$_g" + if [ "$_g" != "$group" ]; then + change_ownership=true + fi + fi + + if $change_ownership ; then + echo + echo "$_path has wrong ownership ($_o)." + echo -en "\tChange to ownership $_ownership.." + chown $_ownership $_path > /dev/null 2>&1 + if [ "$?" = "0" ];then + echo " [ Ok ]" + else + echo " [ Failed ]" + fi + echo + + fi + + if [ -d $_path ]; then + for _file in $_path/* ; do + if [ -d $_file ] && $recursiv ; then + check_owner $_file + elif [ -f $_file ]; then + check_owner $_file + fi + done + fi + fi +} + + +check_owner $path + +exit 0