Tech, Gadgets, Photography, Social Media and Poor Spelling

Automating Adding hosts into Nagios3

I’ve never been a huge fan of Nagios, it always struck me as a bit too fiddly to get setup correctly, I have however had a need this week to set up a simple display which displays the status of devices responding to a simple ping, no SNMP just is the server up, not the services. It’s a first line look and see display in the IT Room.

Setting up a Nagios Server for my needs was a simple as apt-get install nagios3 on Ubuntu you can also install the plugins as well via apt as well. This basically installs all the necessary software and the defaults and questions during the install are self explainitory, Apache2 server, setup passwords, MySQL Root passwords etc

Once setup the issue i had was adding the 50 endpoints i wanted to monitor, now at this point, i will state I didn’t RTFM so there may be a simpler way to do this, searching through the doc’s i did read it seemed i needed to create a config fiel for each server in the /etc/nagios3/conf.d/ folder. There is an example localhost.nagios3.conf file in the folder.

This is where the true power of Linux comes into its own, because BASH the great scripting language came into play, and i wrote the following script to help me out.

 #Static Variables
 cat file | while read line; do
 echo "Creating Host $line for Nagios" # or whaterver you want to do with the $line variable
 cp $configdir/$placeholder $configdir/$line"_nagios2.cfg"
 echo "Replacing localhost with correct hostname"
 hostresult=$(host -t A $line); echo "${hostresult##* }"
 remip=$(echo "${hostresult##* }")
 echo $remip
 sed -ie 's/localhost/'$line'/g' $configdir/$line"_nagios2.cfg"
 sed -ie 's/'$remip'/g' $configdir/$line"_nagios2.cfg"
 rm -rf $configdir/$line"_nagios2.cfge"
 echo "Host $line complete"

This script parses an external file called file (I kept it in the same directory as the script) this had each endpoint DNS name listed per line in a standard text file.

The script parses each line, then creates a hostfile in the conf.d folder for that endpoint using the hostname as a prefix for the file name, it then checks the hostname to determine the IP Address, so yes you need to be able to ping that hostname, the host command returns the ip address and then the script uses sed to find and replace the hostname and ip address lines in the config file.

You end up with an individual host file for each device you want to ping/scan

Restarting nagios with a /etc/init.d/nagios3 restart will then provide you with a list of devices which will ping every 90 seconds.

I also added a plugin to Firefox as this is an always on screen we need to see all day, to reload the tab every 10 seconds just to get a refresh.

Next stage is to get the services monitored on the devices.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: