Zimbra service status notification

Step: 01
vi /tmp/mon.sh


#!/bin/bash
# Script monitoring status ZCS
clear

LISTZIMBRA="mail.drbdtest.com";

for ZCS in $LISTZIMBRA; do

# Delete files before filling
yes | rm /tmp/status-$ZCS-current.txt
yes | rm /tmp/status-$ZCS.txt

# Check the service status and enter it in the file
su - zimbra -c 'zmcontrol status' > /tmp/status-$ZCS-current.txt
su - zimbra -c '/opt/zimbra/check_zimbra.pl' > /tmp/status-$ZCS.txt


# Parameter cek status LDAP
ldap=`grep -wi "ldap:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;

# Check LDAP service
if [[ "$ldap" == STOPPED* ]]; then
echo "Restart service Zimbra"
su - zimbra -c 'zmcontrol restart'

# Regenerate Zimbra status
su - zimbra -c '/opt/zimbra/check_zimbra.pl' > /tmp/status-$ZCS.txt

else
echo "Status service ldap OK"
fi

amavis=`grep -wi "amavis:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
antispam=`grep -wi "antispam:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
antivirus=`grep -wi "antivirus:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
cbpolicyd=`grep -wi "cbpolicyd:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
logger=`grep -wi "logger:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
mailbox=`grep -wi "mailbox:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
memcached=`grep -wi "memcached:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
mta=`grep -wi "mta:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
opendkim=`grep -wi "opendkim:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
proxy=`grep -wi "proxy:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
snmp=`grep -wi "snmp:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
zmconfigd=`grep -wi "zmconfigd:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
stats=`grep -wi "stats:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;
spell=`grep -wi "spell:STOPPED" /tmp/status-$ZCS.txt | cut -d ":" -f2`;

# Cek service zmconfigd
if [[ "$zmconfigd" == STOPPED* ]]; then
echo "restart service zmconfigd"
su - zimbra -c 'zmconfigdctl restart'
else
echo "Status service zmconfigd OK"
fi

# Check service logs
if [[ "$logger" == STOPPED* ]]; then
echo "restart service"
su - zimbra -c 'zmloggerctl restart'
else
echo "Status service logger OK"
fi

# Check the memcached service
if [[ "$memcached" == STOPPED* ]]; then
echo "restart service memcached"
su - zimbra -c 'zmmemcachedctl restart'
else
echo "Status service memcached OK"
fi

# Cek service proxy
if [[ "$proxy" == STOPPED* ]]; then
echo "restart service proxy"
su - zimbra -c 'zmproxyctl restart'
else
echo "Status service proxy OK"
fi

# Check the Amavis service
if [[ "$amavis" == STOPPED* ]]; then
echo "restart service amavis"
su - zimbra -c 'zmamavisdctl restart'
else
echo "Status service amavis OK"
fi

# Check anti-spam service
if [[ "$antispam" == STOPPED* ]]; then
echo "restart service antispam"
su - zimbra -c 'zmantispamctl restart'
else
echo "Status service antispam OK"
fi

# Check antivirus service
if [[ "$antivirus" == STOPPED* ]]; then
echo "restart service antivirus"
su - zimbra -c 'zmantivirusctl restart'
else
echo "Status service antivirus OK"
fi

# Check opendkim service
if [[ "$opendkim" == STOPPED* ]]; then
echo "restart service opendkim"
su - zimbra -c 'zmopendkimctl restart'
else
echo "Status service opendkim OK"
fi

# Check the cbpolicyd service
if [[ "$cbpolicyd" == STOPPED* ]]; then
echo "restart service cbpolicyd"
su - zimbra -c 'zmcbpolicydctl restart'
else
echo "Status service cbpolicyd OK"
fi

# Cek service snmp
if [[ "$snmp" == STOPPED* ]]; then
echo "restart service snmp"
su - zimbra -c 'zmswatchctl restart'
else
echo "Status service snmp OK"
fi

# Cek service spell
if [[ "$spell" == STOPPED* ]]; then
echo "restart service spell"
su - zimbra -c 'zmspellctl restart'
else
echo "Status service spell OK"
fi

# Cek service mta
if [[ "$mta" == STOPPED* ]]; then
echo "restart service mta"
su - zimbra -c 'zmmtactl restart'
else
echo "Status service mta OK"
fi

# Cek service stats
if [[ "$stats" == STOPPED* ]]; then
echo "restart service stats"
su - zimbra -c 'zmstatctl restart'
else
echo "Status service stats OK"
fi

# Cek service mailbox
if [[ "$mailbox" == STOPPED* ]]; then
echo "restart service mailbox"
su - zimbra -c 'zmmailboxdctl restart'
else
echo "Status service mailbox OK"
fi
done

# Check the service conditions before restarting and after restarting then send via email

su - zimbra -c 'zmcontrol status' > /tmp/status-$ZCS-post-restart.txt
NOTRUNNING=`grep -woi "not running" /tmp/status-$ZCS-current.txt | uniq`;
STOPPED=`grep -woi "stopped" /tmp/status-$ZCS-current.txt | uniq`;

#SERVER= $LISTZIMBRA
STATUS1=`cat /tmp/status-$ZCS-current.txt`;


if [ "$NOTRUNNING" == "not running" -o "$STOPPED" == "Stopped" ] ; then
#echo "Mail queue count on mail.sender.com is ${mailq_count}" | /opt/zimbra/common/sbin/sendmail bolcorp@zaapparels.com
FROM=adminx@drbdtest.com
TO=bolcorp@drbdtest.com
/opt/zimbra/common/sbin/sendmail -i $FROM $TO <<MAIL_END
Subject: Zimbra services not running OR stopped
FROM: $FROM
To: $TO
Hi All,

There were some/all of the Zimbra services. Not running. Here's the status

*****************
$STATUS1

Thank you
Bangladesh Online LTD.


MAIL_END

fi

Step: 02
chmod +x /tmp/mon.sh


Step: 03
vi /opt/zimbra/check_zimbra.pl

#!/usr/bin/perl

use Getopt::Std;
use vars qw/ %opt /;

# Just change this command line to be compatible with your setup
$zimbra_status_command='/opt/zimbra/bin/zmcontrol status';

# You should'n change anything behind this line.
$DEBUG=0;
$output="";
$faulty_service=0;
$faulty_daemon=0;

getopts('sSChde:', \%opt);

if (exists $opt{h}) {
usage();
exit(0);
}

if (exists $opt{d}) {
$DEBUG=1;
}

if (exists $opt{e}) {
$exclude_list=$opt{e};
print "Excluded list : $exclude_list\n" if $DEBUG;
}

# Getting zimbra status :
open (ZMSTATUS, "$zimbra_status_command |");
while (<ZMSTATUS>){
print $_ if $DEBUG;
if (/^Host/){
my ($tmp, $hostname)=split();
$output="HOST : $hostname"
} else {
($service, $state)=split();
}

if ($exclude_list =~ /$service/){
print "Service $service is excluded from monitoring\n" if $DEBUG;
next;
}

if ( $state eq "Running" ){
$output=$output . ", $service : OK";
} elsif ( $state eq "Stopped" ){
$output=$output . ", $service : STOPPED";
$faulty_service++;
} elsif ( $state eq "is" ){
$output=$output . " and $service down";
$faulty_daemon++;
}

}
print $output . "\n";
close (ZMSTATUS);

print "Faulty Services : $faulty_service, Faulty Daemons : $faulty_daemon\n" if $DEBUG;
# Choosing right exit code :
# 0 OK, 1 Warning, 2 Critical, 3 Unknow
if (exists $opt{s}) {
#stopped service are ignored until some daemon is faulty
if ( $faulty_service > 0 && $faulty_daemon > 0){
exit(2);

} elsif ( $faulty_service > 0 && $faulty_daemon == 0){
exit(0);
} elsif ( $faulty_service == 0 && $faulty_daemon == 0){
exit(0);
} else {
exit(3);
}
}

if (exists $opt{S}) {
#stopped service give warning state
if ( $faulty_service > 0 && $faulty_daemon > 0){
exit(2);
} elsif ( $faulty_service > 0 && $faulty_daemon == 0){
exit(1);
} elsif ( $faulty_service == 0 && $faulty_daemon == 0){
exit(0);
} else {
exit(3);
}
}

if (exists $opt{C}) {
#stopped service give critical state in all cases
if ( $faulty_service > 0 && $faulty_daemon > 0){
exit(2);
} elsif ( $faulty_service > 0 && $faulty_daemon == 0){
exit(2);
} elsif ( $faulty_service == 0 && $faulty_daemon == 0){
exit(0);
} else {
exit(3);
}
}

sub usage {
if (@_ == 1) {
print "$0: $_[0].\n";
}

print << "EOF";
Usage: $0 [options]
-s
stopped service are ignored until some daemon is faulty
-S
stopped service give warning state if a service is faulty
-C
stopped service give critical if a service is faulty
-e service1,service2,..
list of excluded services from monitoring
-d
enable debug mode
-h
display usage information
EOF
}


Step: 04
chmod +x /opt/zimbra/check_zimbra.pl


Step: 05
crontab -e

*/5 * * * * /tmp/mon.sh

Comments

Popular posts from this blog

Cambium cnPilot E400/E410/E500 Configuration Tutorial

Disabling Zimbra's AntiSpam, Amavis and AntiVirus filtering

Error "Unable to retrive Zimbra GPG key for package validation"