During my exploration of Nagios, of which I am still a newb, I came across many ways of gererating configuration files that either did too much, too little or really didn't even play well with my distribution of choice: Ubuntu. Other methods promised the world, but only delivered a rock. Others required Java, and others didn't even launch because of the level of antiquity or simply did not support this version of Nagios.
That being said, I decided to whip up this little script in order to make the creating and administering of my network just that much simpler. The work done here should allow you to have a working Nagios setup in about 10 minutes.
I've decided to release this script under the terms of the GPL 3 so that others may enjoy this work and see just really how easy it is to get Nagios up and running.
As stated, my distro of choice is currently Ubuntu, HOWEVER, as long as you have NMap installed and it is configured to generate XML files, you should be able to run it on any distro, which will also include BSD derivitives such as FreeBSD and OpenBSD.
Steps to Operate
Step 1: Generating the XML To get started, you will need NMap 6.47 or greater installed. It should support generating XML out of the box, so I won't dive into that part here. This is the version Ubuntu gave me so don't give me any crap about it. Just work with it. It may work with what you have already, so who knows?
nmap -oX 192.168.1.0.xml 192.168.1.0/24
This will scan your entire 192.168.1.0 network for open ports and stuff the results into the XML file indicated. You may rename this anything you want.
Step 2: Run phpNMAPNagios.php Against the XML The second step is to run the PHP script. This script requires SimpleXML support built into PHP. Ubuntu provides this in the repo, so you should have no trouble installing it. Also, make sure the script is chmod +x.
This will generate one configuration file for each up host, and multiple service configuration files for each host with a port designation, with supportive directives only for what is open for each host:
-rw-rw-r-- 1 nagiosuser nagiosuser 446 Oct 14 12:41 192.168.1.201.host.cfg -rw-rw-r-- 1 nagiosuser nagiosuser 637 Oct 14 12:41 192.168.1.201.service.111.cfg -rw-rw-r-- 1 nagiosuser nagiosuser 633 Oct 14 12:41 192.168.1.201.service.22.cfg -rw-rw-r-- 1 nagiosuser nagiosuser 636 Oct 14 12:41 192.168.1.201.service.5666.cfg
Step 3: Copy the Configuration Files to your Nagios Configuration Folder Copy these .cfg files to where you primary Nagios configuration directives live. Mine are located in /etc/nagios3/conf.d/.
Step 4: Restart Nagios sudo service nagios3 restart
This script is meant to get a quick BASE Nagios up and running for you. It's not meant to be the do-all, be-all of Nagios nor is it meant to make you coffee, but if you do have such a device connected to your network, it should have no problem seeing if it's up. :)
One offering of advice I would give is this: put the resulting configuration files from Step 2 into their own directory and simply move or recursively copy that directory over to the Nagios configuration directory in Step 3. The next version of this script will put the files into a timestamped directory.
From there, you can delete specific service files without having to worry about whether or not you missed a brace which causes Nagios to scram on restart.
As always, I take no responsibility for anything that gets hosed on your system as a result of you misusing this script. The fine-tuning is left up to you.
TODO: o Use a template to determine what commands should be run for each specific service.