Nagios lookup.
I wanted an easy way to query our nagios server to see which hosts are being monitored and what checks they have assigned to them for auditing. I also wanted it to be speedy as no one wants to wait 30 seconds for a lookup, so some kind of caching was needed. It uses redis for the backend, with the the hosts IP as the key and the hosts info (in a json array) as the value.
The generate script first parses the localhost.cfg into a PHP array with a sub array for each host like so:
It then loops through the array of hosts to get the service checks specified for each host from the nagios config. The results are then constructed into a new array for each host which is then json_encoded() and set in redis with the hosts IP address as the key.
Full script is:
The generate script is ran via cron every so often, however I have also modified our in-house nagios editor so when a host is added/edited it updates the array stored in redis for the IP, and if you delete a host it removes it from redis.
The lookup endpoint is then just a simple PHP script (it sits behind a firewall for security) that either returns a json array of all IPs in nagios, or if an IP is specified the details for that host in a json array.