#!/bin/bash # Author : mrkips (Cybergavin) # Date Created : 4th April 2015 # Description : This script accepts email requests for the health status of applications/hosts and sends # a response back to the requester with results from the Check_MK monitoring system. # Tested with Check_MK 1.2.8p9, Postfix 2.6.6, RHEL 6 #~~~~~~~~~~~~~~~~~~ # VERSION HISTORY #~~~~~~~~~~~~~~~~~~ # v1.0 | 04-Apr-2015 | First Version # v2.0 | 03-Dec-2015 | Created URLs for hosts pointing to their Check_MK pages # ############################################################################################## # # Determine Script Location and define directories # if [ -n "`dirname $0 | grep '^/'`" ]; then SCRIPT_LOCATION=`dirname $0` elif [ -n "`dirname $0 | grep '^..'`" ]; then cd `dirname $0` SCRIPT_LOCATION=$PWD cd - > /dev/null else SCRIPT_LOCATION=`echo ${PWD}/\`dirname $0\` | sed 's#\/\.$##g'` fi # # Variables and Constants # LOGDIR=${SCRIPT_LOCATION}/logs DATADIR=${SCRIPT_LOCATION}/data WORKDIR=${SCRIPT_LOCATION}/work TDATE=`date '+%Y%m%d'` YMDATE=`date '+%Y%m'` TIMESTAMP=`date '+%Y%m%d%H%M%S'` SCRIPT_NAME=`basename $0` OUTFILE=${SCRIPT_LOCATION}/${SCRIPT_NAME%%.*}.out ERRFILE=${SCRIPT_LOCATION}/${SCRIPT_NAME%%.*}.err LOGFILE=${LOGDIR}/${SCRIPT_NAME%%.*}_${YMDATE}.log DATAFILE=${DATADIR}/${SCRIPT_NAME%%.*}_${YMDATE}.tsv MAILFILE=${WORKDIR}/${SCRIPT_NAME%%.*}_$$.txt HTMLFILE=${WORKDIR}/${SCRIPT_NAME%%.*}.html CMKFILE=${WORKDIR}/${SCRIPT_NAME%%.*}.check MUTTRC=${SCRIPT_LOCATION}/.muttrc # # Redirect stdout and stderr # exec 1> $OUTFILE exec 2> $ERRFILE # # Validate directory structure # for mydir in $LOGDIR $DATADIR $WORKDIR do if [ ! -d $mydir ]; then mkdir $mydir 2> /dev/null if [ $? -ne 0 ]; then echo "\n`date '+%Y%m%d_%H%M'` : CRITICAL : $mydir does not exist and cannot be created. Exiting..." exit 999 fi fi done ##################################################################################### # Functions ##################################################################################### # # Parsing Functions # function parseMail { # Extract application(s) and host(s) - Try subject first (preferred) and then body sender=`grep "^From:" $MAILFILE | awk '{print $NF}' | sed 's///g'` # First try subject.. sub=`grep "^Subject:" $MAILFILE | grep -v Undeliverable | head -1` if [ -n "$sub" ]; then myhosts=`echo $sub | awk -F':' '{ for (i=1;i<=NF;i++) if (tolower($i) ~ /host=/) print $i}' | cut -d'=' -f2` myapps=`echo $sub | awk -F':' '{ for (i=1;i<=NF;i++) if (tolower($i) ~ /app=/) print $i}' | cut -d'=' -f2` fi # ..And then try Body if [ -n "`grep \"Content-Transfer-Encoding: quoted-printable\" $MAILFILE`" ]; then myhosts="${myhosts} `sed -n '/Content-Transfer-Encoding: quoted-printable/,/Content-Transfer-Encoding: quoted-printable/p' $MAILFILE | grep -i \"Host=\" | awk -F'=3D' '{print $2}'`" myapps="${myapps} `sed -n '/Content-Transfer-Encoding: quoted-printable/,/Content-Transfer-Encoding: quoted-printable/p' $MAILFILE | grep -i \"App=\" | awk -F'=3D' '{print $2}'`" elif [ -n "`grep \"Content-Transfer-Encoding: base64\" $MAILFILE`" ]; then myhosts="${myhosts} `sed -n '/Content-Transfer-Encoding: base64/,/Content-Transfer-Encoding: base64/p' $MAILFILE | sed '/^Content/d;/^--/d;/^$/d' | base64 -d | grep -i \"Host=\" | awk -F'=' '{print $2}' | tr -d '\r'`" myapps="${myapps} `sed -n '/Content-Transfer-Encoding: base64/,/Content-Transfer-Encoding: base64/p' $MAILFILE | sed '/^Content/d;/^--/d;/^$/d' | base64 -d | grep -i \"App=\" | awk -F'=' '{print $2}' | tr -d '\r'`" fi } # # Validation Functions # function ignoreNDR { ndr=0 if [ -n "`grep \"^Subject: Undeliverable\" $MAILFILE`" ]; then ndr=1 fi echo $ndr } function validateApp { cat< $HTMLFILE EOF } function htmlHostStatus { # Start HTML Table for HOST HEALTH SUMMARY cat <> $HTMLFILE
EOF } function htmlHostAlerts { # End HTML Table for HOST HEALTH SUMMARY and start HTML Table for HOST ALERTS cat <> $HTMLFILE
HOST HEALTH SUMMARY
HOSTOKWARNCRITUNKNOWN


EOF } function htmlFooter { # End HTML for Email Body cat <> $HTMLFILE
HOST ALERTS
HOSTALERTDESCRIPTION


ecMK is an application that allows users to request the health status (from Check_MK) of IT Infrastructure via email. Refer ecMK for more details.

EOF } function htmlInvalidApp { # Start HTML for Email Body when a request for health status for invalid application(s)/host(s) is received cat <> $HTMLFILE
You have sent an INVALID request for Application/Host Health status. In order to request Application health status, use App=<app name> in the email body. E.g. App=cards. In order to request Host health status, use Host=<host name> in the email body. E.g. Host=vtorcrddv01.
Given below is a list of valid applications:

EOF } # # Post-Processing Functions # function doHousekeep { # Housekeeping - cleanup if [ -f $MAILFILE ]; then rm $MAILFILE fi find $DATADIR -type f -name "*.tsv" -mtime +90 | xargs rm -f } function collectStats { # Collect statistics on script usage if [ -f $DATAFILE ]; then echo "${TIMESTAMP}~${sender}~${myapps}~${myhosts}" >> $DATAFILE else echo "TIMESTAMP~SENDER~APPS~HOSTS" > $DATAFILE echo "${TIMESTAMP}~${sender}~${myapps}~${myhosts}" >> $DATAFILE fi } ##################################################################################### # Main ##################################################################################### # Accept email from postfix cat - > $MAILFILE # Parse email parseMail # Check for NDR if [ `ignoreNDR` -eq 1 ]; then collectStats echo "${TIMESTAMP} : Received NDR, ignoring." >> $LOGFILE doHousekeep exit 0 fi # Start building HTML for EMail Body htmlInit # Validate app(s) and host(s) unset capp chost for a in `echo $myapps | sed 's/,/ /g'` do capp="`validateApp ${a}` ${capp}" done if [ -n "${capp}" ]; then for h in $capp do ah="`lqlAH $h | cut -d';' -f2 | sed 's/,/ /g'` ${ah}" done fi if [ -n "${ah}" -o -n "`echo $myhosts | sed 's/,/ /g'`" ]; then for ch in $ah `echo $myhosts | sed 's/,/ /g'` do chost="`validateHost ${ch}` ${chost}" done fi # Work on app(s) and host(s) if [ -n "${chost}" ]; then htmlHostStatus r=1 s1t=s2t=s3t=0 for myhost in $chost do svcs=`lqlHS $myhost` s0=`echo $svcs | cut -d';' -f1` s1=`echo $svcs | cut -d';' -f2` s2=`echo $svcs | cut -d';' -f3` s3=`echo $svcs | cut -d';' -f4` if [ $((r%2)) -eq 1 ]; then cat<> $HTMLFILE EOF else cat<> $HTMLFILE EOF fi r=$((r+1)) s1t=$((s1t+s1)) s2t=$((s2t+s2)) s3t=$((s3t+s3)) done if [ $s1t -gt 0 -o $s2t -gt 0 -o $s3t -gt 0 ]; then htmlHostAlerts for myhost in $chost do svcs=`lqlHS $myhost` s0=`echo $svcs | cut -d';' -f1` s1=`echo $svcs | cut -d';' -f2` s2=`echo $svcs | cut -d';' -f3` s3=`echo $svcs | cut -d';' -f4` /omd/sites/mysite/local/bin/check_mk -nv $myhost > $CMKFILE if [ $s1 -gt 0 ]; then grep "WARN -" $CMKFILE | while read line do s1_svc=`echo $line | cut -d'-' -f2-` cat<> $HTMLFILE EOF done fi if [ $s2 -gt 0 ]; then grep "CRIT -" $CMKFILE | while read line do s2_svc=`echo $line | cut -d'-' -f2-` cat<> $HTMLFILE EOF done fi if [ $s3 -gt 0 ]; then grep "UNKNOWN -" $CMKFILE | while read line do s3_svc=`echo $line | cut -d'-' -f2-` cat<> $HTMLFILE EOF done fi done htmlFooter elif [ $s1t -eq 0 -a $s2t -eq 0 -a $s3t -eq 0 ]; then echo "
APPLICATIONS
${myhost}${s0}${s1}${s2}${s3}
${myhost}${s0}${s1}${s2}${s3}
${myhost}WARN${s1_svc}
${myhost}CRIT${s2_svc}
${myhost}UNKNOWN${s3_svc}


OVERALL HEALTH : OK

ecMK is an application that allows users to request the health status (from Check_MK) of IT Infrastructure via email. Refer ecMK for more details.

" >> $HTMLFILE fi if [ -n "${capp}" ]; then sub="Application Health Status : ${capp}" else sub="Host Health Status : ${chost}" fi mutt -F ${MUTTRC} -s "${sub}" $sender < $HTMLFILE else htmlInvalidApp v_apps=`lqlHG` for v in $v_apps do cat<> $HTMLFILE ${v} EOF done htmlFooter mutt -F ${MUTTRC} -s "Health Status : INVALID Request" $sender < $HTMLFILE fi collectStats doHousekeep