#/bin/bash
# Restart Script - Trazzdk november 2014
# Version 1.0
# Simple Nagios reload/restart script

# Load nagios cfg files into the variable nagios_err
nagios_err=$(/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg)

# Total Warnings and Errors into variables.. and cfg error into variable
total_warnings_flag=$(echo "$nagios_err"|grep "Total Warnings"|awk '{print $3}')
total_errors_flag=$(echo "$nagios_err"|grep "Total Errors"|awk '{print $3}')
cfg_err=$(echo "$nagios_err"|grep Error:)

# If Total Warnings & Total Errors == 0, restart nagios
if [ "$total_errors_flag" == 0 ] && [ "$total_warnings_flag" == 0 ] ; then
	echo -e "\033[33;32mEverything is fine!\033[0m"
	echo "Will proceed by restarting nagios in 5 seconds"

# All OK, restart nagios
	for i in {5..1};do echo -n "$i.." && sleep 1; done
    	/etc/init.d/nagios restart

# If errors found, abort and tell wich cfg file has trouble
else
	echo -e "\033[33;31m You have Errors, so we will abort this operation\033[0m"
	echo "$cfg_err"
fi
