Parse Nagios status.dat and send daily report


#!/bin/bash

###################################################################
###################################################################
#                                                                 #
#   Script to check Nagios is running and alerts are enabled      #
#                                                                 #
###################################################################
###################################################################

ECHO="/bin/echo"
GREP="/bin/egrep"
SED="/bin/sed"
PIDOF="/sbin/pidof"

#Nagios Parent Host
host="nagios"

#Logfile Details
NAGIOS_LOG="/usr/local/nagios/var"
logfile="$NAGIOS_LOG/status.dat"

query="enable_notifications"
enabled=`ssh $host $GREP $query $logfile | $SED 's/enable_notifications=//g'`

PID=`ssh $host "$PIDOF nagios"`

#Host Status Details
totn_h=`ssh $host $GREP hoststatus $logfile |wc -l`
okn_h=`ssh $host $GREP -A50 hoststatus $logfile |$GREP current_state=0 |wc -l`
warnn_h=`ssh $host $GREP -A50 hoststatus $logfile |$GREP current_state=1 |wc -l`
criticaln_h=`ssh $host $GREP -A50 hoststatus $logfile |$GREP current_state=2 |wc -l`
pendingn_h=`ssh $host $GREP -A50 hoststatus $logfile |$GREP current_state=3 |wc -l`

notfn_h=`ssh $host $GREP -A50 hoststatus $logfile | $GREP "notifications_enabled=0" |wc -l`

#Service Status Details
totn_s=`ssh $host $GREP servicestatus $logfile |wc -l`
okn_s=`ssh $host $GREP -A50 servicestatus $logfile |$GREP current_state=0 |wc -l`
warnn_s=`ssh $host $GREP -A50 servicestatus $logfile |$GREP current_state=1 |wc -l`
criticaln_s=`ssh $host $GREP -A50 servicestatus $logfile |$GREP current_state=2 |wc -l`
unknownn_s=`ssh $host $GREP -A50 servicestatus $logfile |$GREP current_state=3 |wc -l`

notfn_s=`ssh $host $GREP -A50 servicestatus $logfile | $GREP "notifications_enabled=0" |wc -l`

#Critical Alerts
crit_service_checks=`ssh $host $GREP -B15 current_state=2 $logfile |$GREP -iE "(host_name|service_description|hoststatus|servicestatus)" | $SED '/servicestatus/d' | $SED '$!N;s/\n/ /' | $SED '/hoststatus/d' | $SED 's/host_name=//g' | $SED 's/service_description=//g'`
warn_service_checks=`ssh $host $GREP -B15 current_state=1 $logfile |$GREP -iE "(host_name|service_description|hoststatus|servicestatus)" | $SED '/servicestatus/d' | $SED '$!N;s/\n/ /' | $SED '/hoststatus/d' | $SED 's/host_name=//g' | $SED 's/service_description=//g'`

crit_host_checks=`ssh $host $GREP -A50 hoststatus $logfile | $GREP -iE "(host_name|current_state)" | $GREP -B1 current_state=2 | $GREP host_name | $SED 's/host_name=//g'`
warn_host_checks=`ssh $host $GREP -A50 hoststatus $logfile | $GREP -iE "(host_name|current_state)" | $GREP -B1 current_state=1 | $GREP host_name | $SED 's/host_name=//g'`

#Disabled Alerts
notf_service_disabled=`ssh $host $GREP -B45 notifications_enabled=0 $logfile |$GREP -iE "(host_name|service_description|hoststatus|servicestatus)" | $SED '/servicestatus/d' | $SED '$!N;s/\n/ /' | $SED '/hoststatus/d' | $SED 's/host_name=//g' | $SED 's/service_description=//g'`
notf_host_disabled=`ssh $host $GREP -A50 hoststatus $logfile | $GREP -iE "(host_name|notifications_enabled)" | $GREP -B1 notifications_enabled=0 | $GREP host_name | $SED 's/host_name=//g'`

echo " "
echo "                                    #######################################"
echo "                                            Checking Nagios Status"
echo "                                    #######################################"
echo " "

if [[ -n $PID ]]; then
echo ""
echo "nagios-------------------------------------------[running]"
else
echo ""
echo "nagios-------------------------------------------[*missing*!Please.resolve]"
fi

if [ $enabled = 1 ]; then
echo ""
echo "nagios alerts------------------------------------[Enabled]"
elif [ $enabled = 0 ]; then
echo ""
echo "nagios alerts------------------------------------[*Disabled*!Please.resolve]"
fi

echo ""
echo "Host Status Totals"
echo "TOTAL:$totn_h   OK:$okn_h  WARN:$warnn_h  CRITICAL:$criticaln_h  PENDING:$pendingn_h"
echo "Number of Hosts Silenced: $notfn_h"
echo ""
echo "Service Status Totals"
echo "TOTAL:$totn_s  OK:$okn_s  WARN:$warnn_s  CRITICAL:$criticaln_s  UNKNOWN:$unknownn_s"
echo "Number of Services Silenced: $notfn_s"

if [ $criticaln_h != 0 ]; then
echo ""
echo "CRITICAL HOST ALERTS:"
echo "${crit_host_checks}"
fi

if [ $warnn_h != 0 ]; then
echo ""
echo "WARNING HOST ALERTS:"
echo "${warn_host_checks}"
fi

if [ $criticaln_s != 0 ]; then
echo ""
echo "CRITICAL SERVICES ALERTS:"
echo "${crit_service_checks}"
fi

if [ $warnn_s != 0 ]; then
echo ""
echo "WARNING SERVICES ALERTS:"
echo "${warn_service_checks}"
fi

if [ $notfn_h != 0 ]; then
echo ""
echo "HOST ALERTS SILENCED:"
echo "${notf_host_disabled}"
fi

if [ $notfn_s != 0 ]; then
echo ""
echo "SERVICES ALERTS SILENCED:"
echo "${notf_service_disabled}"
fi