Simple Check config / Restart script

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
trazzdk
Posts: 6
Joined: Mon Dec 08, 2014 4:52 am
Location: Denmark

Simple Check config / Restart script

Post by trazzdk »

Hi all.

I have created a simple Check & restart script for Nagios..

It does the "nagios -v .. check" and if errors found, it will report which cfg files and which lines.. If none found it will restart with a 5 seconds timer (in case you regret).

I was looking for something like this to make it easier to "test/restart" when I started using nagios, so I thought I would share it with you.

/usr/local/bin/nagios-reload

Code: Select all

#/bin/bash
# Restart Script - Trazzdk november 2014
# 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
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Simple Check config / Restart script

Post by tmcdonald »

Much appreciated! If you want to get more exposure for the script, you can upload it to our Nagios Exchange site: http://exchange.nagios.org/
Former Nagios employee
emislivec
Posts: 52
Joined: Tue Feb 25, 2014 10:06 am

Re: Simple Check config / Restart script

Post by emislivec »

Thanks trazzdk! Some of this could be useful to have in the init script itself so it reports errors better.

There is a "/etc/init.d/nagios configtest" command that runs "nagios -vp ..." but doesn't do a summary like your script.

The current "/etc/init.d/nagios start" and "/etc/init.d/nagios restart" run a "/etc/init.d/nagios checkconfig" that does do a summary, but yours is cleaner.
Locked