Apr/100
MKLivestatus – Experience the new way getting Nagios live data
I introduced MKLivestatus before so I won’t get in detail what MKLivestatus is and what it is meant for. Today I give you some simple examples on how straight forward it is to get status information out of Nagios using Livestatus.
Assumptions
In the following examples I assume you got a working Nagios installation with a loaded MKLivestatus NEB module. Having this you got the Livestatus Socket somewhere on your system. You will need this path now to execute those examples.
In my case the path is /var/run/nagios/rw/live.
When listing the file it looks like this:
blath:/home/lami# ls -al /var/run/nagios/rw/live srw-rw---- 1 nagios nagcmd 0 2010-03-05 18:45 /var/run/nagios/rw/live
You will also need the unixcat binary to communicate with the livestatus socket. The unixcat binary is part of the MKLivestatus package.
So let’s go now …
Our first manual Livestatus query
blath:/home/lami# echo -e "GET hosts\nColumns: name address state worst_service_state\nFilter: name = www.nagvis.org\n" | unixcat /var/run/nagios/rw/live www.nagvis.org;www.nagvis.org;0;0
So what did happen now?
- We created a Query for the Livestatus Socket using the LQL (Livestatus Query Language).
- The
-eParameter for the echo call is important to have the newlines (\n) interpreted. - Then we send the query to the unixcat binary which queries the unix socket
/var/run/nagios/rw/live - The Livestatus module proceeds the query and the unixcat binary outputs the response to stdout
The Query in detail:
- GET hosts: This query wants to fetch data from the hosts table
- Columns: name address state worst_service_state: We request the name, address, current state and worst service state of the host(s)
- Filter: name = www.nagvis.org: We only want to see the host with the name www.nagvis.org
Those of you who know SQL will see many similarities in LQL. Besides the mentioned parts GET, Columns and Filters are also Limits. It is possible to append a Limits: 10\n to limit the elements in the answer to 10 pieces.
Going deeper
We only got the single answer line in the response above. Querying many columns it may get hard to get the right columns. For this case you can add a command to let MKLivestatus display the column names in the response:
blath:/home/lami# echo -e "GET hosts\nColumns: name address state worst_service_state\nFilter: name = www.nagvis.org\nColumnHeaders: on \n" | unixcat /var/run/nagios/rw/live name;address;state;worst_service_state www.nagvis.org;www.nagvis.org;0;0
Until now we used the default output format of MKLivestatus. If you use some scripting language to communicate with MKLivestatus it might be easier to parse the data when using the JSON format.
blath:/home/lami# echo -e "GET hosts\nColumns: name address state worst_service_state\nFilter: name = www.nagvis.org\nOutputFormat: json\n" | unixcat /var/run/nagios/rw/live [["www.nagvis.org","www.nagvis.org",0,0]]
This output can easily be parsed e.g. in PHP, Perl, Python, JavaScript and all other languages which have json parsing builtin.
Enough for today. I will continue this journey soon…






