Nagios search auto complete

Today I have discovered a very nice plugin for the Vautour interface in Nagios that adds an auto complete query to the search box! In this article I will describe the installation (the author made-it extremely good but I wanted to add a small contribution too).
What you will need:

 

Installation:
As I said the author made a very good  job describing the installation part, so I’m just put it here with some small comments:

Extract “jquery.zip” in the “share/js” folder.
You should now have “jquery.js”, “jquery-ui.js”, and a “jquery-ui” folder in the “share/js” folder.
Make sure that all files in there are readable and executable.
Note: I made modifications to the jQuery UI files, to make them work with the (slightly rigid) Nagios iframe setup.
This is why I recommend downloading the files from Nagios Exchange, rather than from the creators themselves.
By the way, kudos to the jQuery team and also to the guys at jQuery UI! Without them, this mod wouldn’t be here. (:
Now, open “sidebar.html” (in the share folder) for editing.
Replace lines 10 and 11 (the two script references) with this:


css” href=”js/jquery-ui/jquery-ui.css” media=”screen, projection” />

Small comment – the original lines that need to be replaced are:


Replace lines 15 through 21 (the “search_box” div) with these lines:

<div id=”search_box”>
<form action=”/nagios/cgi-bin/status.cgi” id=”search_form” method=”get” target=”main”>
<input id=”search” name=”host” type=”text” />
<input type=”hidden” name=”navbarsearch” value=”1″ />
<input src=”images/interface/search.gif” alt=”OK” title=”Search” id=”search_submit” type=”image” />
form>
</div>
<script type=”text/javascript”>
$(document).ready(function() {
jQuery.get(‘hosts.txt’, function(data) {
hosts = data.split(‘\n’);
$( “#search” ).autocomplete({
source: hosts
})
});
});
</script>

Small comment – the original lines that need to be replaced are:
<div id=”search_box”>
            

nagios/cgi-bin/status.cgi” method=”get” target=”main”>

                <input id=”search” name=”host” type=”text” />
                <input type=”hidden” name=”navbarsearch” value=”1″ />
                <input src=”images/interface/search.gif” alt=”OK” title=”Search” id=”search_submit” type=”image” />
            </form>
        </div>

 

Save and close the file.
Now, we set-up the creation of a file named “hosts.txt”.
This file contains a list of all your hosts, that will automatically get updated every hour by cron.
We use this list for our autocomplete entries.

Open a Terminal.
Edit your crontab by running:
crontab -e

If applicable, select your editor of choice. I use nano.

Now, add this to the bottom of the cron file (copy/paste is your friend!):

@hourly /bin/cat/usr/local/nagios/var/objects.cache | /bin/grep “host_name” | /bin/sed ‘s/\s*host_name\s*//g’ | /usr/bin/sort | /usr/bin/uniq > /usr/local/nagios/share/hosts.txt

And finally, you should run the following command, to create hosts.txt immediately, without waiting for cron to kick in.

/bin/cat /usr/local/nagios/var/objects.cache | /bin/grep “host_name” | /bin/sed ‘s/\s*host_name\s*//g’ | /usr/bin/sort | /usr/bin/uniq > /usr/local/nagios/share/hosts.txt

You can, if you want, check out /usr/local/nagios/share/hosts.txt, to verify that the list was created successfully.

You should now have a working autocomplete search box!
Open up your Nagios web page and try it out!

 

Very nice solution! But what if you want to find an IP? You won’t be able to find it by doing the query for the host name. One can still search within Nagios the IP addresses of a host but will not have the auto complete feature.
Let’s add this to the installation:
You need to do a small modification to the install instructions. You will need to create a small bash script like the one  under:

#!/bin/sh
/bin/cat /usr/local/nagios/var/objects.cache | /bin/grep “host_name” | /bin/sed ‘s/\s*host_name\s*//g’ | /usr/bin/sort | /usr/bin/uniq > /usr/local/nagios/share/hosts.txt
/bin/cat /usr/local/nagios/var/objects.cache | /bin/grep “address” | /bin/sed ‘s/\s*address\s*//g’ | /usr/bin/sort | /usr/bin/uniq >> /usr/local/nagios/share/hosts.txt

(Please verify the paths so that they will correspond with your installation!)

Save this to a directory of choice and add it to the crontab or the Nagios start-up script. To simplify the tutorial we will do the crontab modification here (I named mine populate.sh):

crontab –e

@hourly /home/crontab/populate.sh

 

This will replace the crontab line in the tutorial:

@hourly /bin/cat/usr/local/nagios/var/objects.cache | /bin/grep “host_name” | /bin/sed ‘s/\s*host_name\s*//g’ | /usr/bin/sort | /usr/bin/uniq > /usr/local/nagios/share/hosts.txt

 

Some thoughts on security:
I recommend that you give the text file (hosts.txt – in which you will put all the host names and IP’s) a masked name (like point.gif) so that it wouldn’t be so easy to find.
Also you must use in this case the “Options –Indexes” in your .htaccess file so you will prevent the directory listing.
The last aspect to take into consideration is that all the logged users will see the names or the IP’s in the search box but they will not be able to find anything if the Nagios server does not give them access to that object.

Here is how it should look:

Nagios autocomplete

Nagios auto complete preview

 

That’s all!

Many thanks to the author of this plugin and for your attention!
Mihai out!

Leave a Reply