What is nagxmlstatus.cgi?
nagxmlstatus.cgi is a script, written in Perl, that translates Nagios status.dat file (in Nagios proprietary format) to an XML formatted file. The script was originally written to provide Nagios status information for Nagstatus Gadget for Windows Sidebar, but as it translates all data in Nagios status.dat, you can use it for other purposes too.
Requirements
To use the script, you must have Perl installed, along with XML::LibXML module. Most GNU/Linux distributions have them both available as installable packages (RPM based distros use perl-XML-LibXML, deb-based distros have libxml-perl and at least the newer Ubuntu have libxml-libxml-perl; consult your distribution documentation on how to install these). If you can’t find a ready installation package, and have Perl already installed, you can run the following command:
% perl -MCPAN -e "install XML::LibXML"
Installation and Configuration
The easiest way to install the script is to place it into the Nagios installation’s cgi-bin directory (the same where all Nagios cgi binaries are), and make sure it is executable by your http server’s user account (or, on a GNU/Linux platform, run chmod a+x nagxmlstatus.cgi).
There is one configuration parameter within the script. On line 15 is specified a variable called $STATUSFILE. This must point to the status.dat file of your Nagios installation.
Using via HTTP Requests
If you call the script seldom (like, you have only one or just a few Nagstatus gadgets running in the network), you can call the script directly via http server. For the Nagstatus gadget use, just specify “Nagios Web UI URL” config parameter (or “XML Status Provider URL”, in case you have put the script in somewhere else than the Nagios cgi directory), and choose “Script” in “URL points to:” (as is the default).
If you want to use it for other purposes, you can first try to specify “nagxmlstatus.cgi?node=all&elem=all” in a web browser url. This will return your whole status.dat content formatted as XML, and you can examine that to determine what content you really need. After that, just specify the nodes and elements you need as request parameters (you can, and probably will, have multiple node’s and elem’s). For your reference, the Nagstatus gadget uses the following request:
?node=host&node=service&elem=current_state&elem=plugin_output&elem=problem_has_been_acknowledged&elem=last_state_change&node=program&elem=program_start&elem=last_command_check&elem=scheduled_downtime_depth”
Using via Cron
If your script gets hit frequently, it might burden your server’s resources too much. The script is using a lot of pattern matching, XML libraries and other heavy methods to provide the XML result, so you probably want to lessen the times it gets run. You can use cron or other scheduling methods to run the script from commandline, and redirect it’s output to a file. Then you can access that file from the client using http or other methods. Even if you run the file every minute, it should be reasonably light on any modern server, and provides Nagios status information in a timely manner.
To do this, you must specify the wanted output using commandline parameters. The nodes are specified with –n –parameter, and elements with –e. For multiple nodes and/or elements just specify comma separated list as the parameter. For example, to use with Nagstatus gadget, use the following command line:
nagxmlstatus.cgi -n host,service,program -e current_state,plugin_output,problem_has_been_acknowledged,last_state_change,program_start,last_command_check,scheduled_downtime_depth
To use the resulting file with Nagstatus gadget, simply point the “XML Status Provider URL” setting to the file, and choose “File” as “The URL points to:”