zimbra-script/zabbix_zimbra_service_check.sh

122 lines
3.1 KiB
Bash
Raw Permalink Normal View History

2021-01-15 18:51:12 +03:00
#!/bin/bash
################################################
# Get and checking Zimbra services.
#
# Author: Sergey Kalinin
# https://nuk-svk.ru
# svk@nuk-svk.ru
################################################
#
# Discover services and making JSON file for Zabbix
# ./zabbix_zimbra_service_check.sh discover
#
# Get status for all zimbra services and create file for next tasks
# ./zabbix_zimbra_service_check.sh get-all-status
#
# Read JSON for send into zabbix
# ./zabbix_zimbra_service_check.sh read-json-discover
#
# Checking service
# ./zabbix_zimbra_service_check.sh _ZIMBRA_SERVICE_NAME_
#
# Where _ZIMBRA_SERVICE_NAME_ - name of zimbra service returned by command "zmcontrol status"
#
###################################################
# Based on:
#
# Zabbix script to check Zimbra services and perform service discovery.
# Supports Zimbra 8.6 and "two-worded" service names
# Author: Lorenzo Milesi <maxxer@yetopen.it>
# Copytight: YetOpen S.r.l. 2015
# License: GPLv3
#
###################################################
# uncomment for debug
#set -x
COMMAND="sudo -u zimbra /opt/zimbra/bin/./zmcontrol"
FILE_ZIMBRA_SERVICE_STATUS='/tmp/zimbra_service_status'
FILE_ZIMBRA_SERVICE_LIST='/tmp/zimbra_service_list'
# Maximum file age in seconds (20 min)
MAXAGE=1200
case "$1" in
version)
# Return zimbra version
VERS=$($COMMAND -v)
if [ $? -eq 0 ] ; then
echo $VERS
exit 0;
fi
# error
exit 1;
;;
discover)
# Return a list of running services in JSON
# And create JSON-file
echo "{" > ${FILE_ZIMBRA_SERVICE_LIST}.json
echo -e "\t\"data\":[\n" >> ${FILE_ZIMBRA_SERVICE_LIST}.json
SRVCS=$($COMMAND status | grep -v Host | awk '{$(NF--)=""; print}' | sed 's/^/\t{ \"{#ZIMBRASERVICE}\":\"/' | sed 's/\ $/\" },/')
# Remove last comma from the sting, to make a good JSON
echo $(echo $SRVCS | sed 's/,\+$//') >> ${FILE_ZIMBRA_SERVICE_LIST}.json
echo -e "\n\t]\n" >> ${FILE_ZIMBRA_SERVICE_LIST}.json
echo "}" >> ${FILE_ZIMBRA_SERVICE_LIST}.json
exit 0;
;;
read-json-discover)
# Read a JSON and return
if [ ! -f ${FILE_ZIMBRA_SERVICE_LIST}.json ]; then
echo "File ${FILE_ZIMBRA_SERVICE_LIST}.json not found"
exit 1
fi
while read LINE; do
echo ${LINE}
done < "${FILE_ZIMBRA_SERVICE_LIST}.json"
;;
get-all-status)
$COMMAND status| grep -v Host | /usr/bin/tr -d "\t" > ${FILE_ZIMBRA_SERVICE_STATUS}
;;
*)
# move on...
CHECK=$1
if [ "$CHECK" = "" ]; then
echo "No Zimbra service specified..."
exit 1
fi
if [ ! -f $FILE_ZIMBRA_SERVICE_STATUS ]; then
echo "File $FILE_ZIMBRA_SERVICE_STATUS not found"
exit 1
fi
#check if cached status file size > 0
# and age of file
if [ -s ${file} ]; then
OLD=`stat -c %Z $FILE_ZIMBRA_SERVICE_STATUS`
NOW=`date +%s`
#echo $OLD
# if older then maxage, update file
if [ `expr $NOW - $OLD` -gt ${MAXAGE} ]; then
echo "File $FILE_ZIMBRA_SERVICE_STATUS is to old"
exit 1
fi
fi
STATUS="$(cat $FILE_ZIMBRA_SERVICE_STATUS | grep -w "$CHECK" | awk '{print $NF}')"
if [ "$STATUS" != "Running" ]; then
echo 0
else
echo 1
fi
;;
esac
exit 0;