A PHP library and REST API endpoint for interacting with nagios via the socket provided by mk-livestatus. It can both query data about object states in the Nagios server and issue Nagios commands.
All examples in this document assume that the API is available at
http://nagios.example.com/livestatus-api/
All responses are in JSON and have the following format:
{"success": <bool>, "content": <object>}
If "success" is true, "content" will contain the requested data. If false, it will contain
{"code": <int>, "message": <string>}
where "code" is the mk-livestatus error code and "message" is a human-readable explanation of the error.
The query interface returns a list of objects in JSON. The available endpoints are the same as the tables available from mk-livestatus itself:
To retrieve all records from a table, send a GET request to
http://nagios.example.com/livestatus-api/{tablename}
For example, to get all host records from the server, GET
http://nagios.example.com/livestatus-api/hosts
To limit the returned data to a subset of the available fields, pass a Columns query parameter containing a comma-separated list of column names. To fetch the name and services list for all hosts:
http://nagios.example.com/livestatus-api/hosts?Columns=name,services
To filter the result set to records meeting some criteria, pass one or more Filter[] params. Each Filter is a urlencoded LQL filter (see the [mk-livestatus documentation](http://mathias-kettner.com/checkmk_livestatus.html#H1:LQL - The Livestatus Query Language) for detailed LQL filter syntax). If more than one filter is specified, they are ANDed together. To get all hosts starting with "api" in state OK (0):
http://nagios.example.com/livestatus-api/hosts?Filter[]=name%20~%20%5Eapi&Filter[]=state%20%3D%200
Stats queries allow you to get a count of objects matching a criteria. Stats queries return a list of counts and never take a Columns parameter. You can request several Stats with a single API call. You can also restrict the objects counted by adding Filters to your query. To count the number of hosts starting with "api" in state OK:
http://nagios.example.com/livestatus-api/hosts?&Stats[]=name%20~%20%5Eapi&Filter[]=state%20%3D%200
All calls to livestatus-api
to execute Nagios commands must be HTTP POST requests.
Acknowledgements for host and service alerts can be sent via the acknowledge_problem
endpoint.
curl -is -XPOST https://nagios.example.com/livestatus-api/acknowledge_problem -d '{"host": "host.example.com", "author": "rfrantz", "comment": "acked from livestatus"}'
Acknowledging service alerts is similar to host alerts, with the addition of the service
parameter:
curl -is -XPOST https://nagios.example.com/livestatus-api/acknowledge_problem -d '{"host": "host.example.com", "service": "Apache", "author": "rfrantz", "comment": "acked from livestatus"}'
cancel_downtime
Existing scheduled downtimes for a host can be canceled. cancel_downtime
expects the downtime_id
parameter. Downtime IDs can be found by querying a
host and extracting the downtimes
array:
curl -s https://nagios.example.com/livestatus-api/hosts?Filter=name%20%3D%20my_host | jq '.' | grep 'downtimes"' -A 2
"downtimes": [
12345
],
The subsequent request to cancel the host's downtime is:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/cancel_downtime' -d '{"downtime_id": "12345"}'
To cancel the downtime for a service, pass the name of the service along with the downtime_id:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/cancel_downtime' -d '{"downtime_id": "12345", "service": "CPU"}'
schedule_downtime
Schedule downtime for a host as follows:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/schedule_downtime' -d '{"host": "host.example.com", "duration": "7200", "author": "rfrantz", "comment": "Downtimed via livestatus"}'
NOTE: The duration
field expects a value whose unit is in seconds.
Downtimes can be scheduled for a particular service by adding a "service"
parameter:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/schedule_downtime' -d '{"host": "host.example.com", "service": "CPU", duration": "7200", "author": "rfrantz", "comment": "Downtimed via livestatus"}'
disable_notifications
Notifications for a host, a host's service, or all of the host's services can be disabled via the disable_notifications
endpoint.
Send a request that includes a valid 'host' value:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/disable_notifications' -d '{"host": "host.example.com"}'
Send a request that includes valid 'host' and 'service' values:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/disable_notifications' -d '{"host": "host.example.com", "service": "httpd"}'
Send a request that includes a valid 'host' value and set 'scope' to 'all':
curl -s -XPOST 'https://nagios.example.com/livestatus-api/disable_notifications' -d '{"host": "host.example.com", "scope": "all"}'
enable_notifications
Notifications for a host, a host's service, or all of the host's services can be enabled via the enable_notifications
endpoint.
Send a request that includes a valid 'host' value:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/enable_notifications' -d '{"host": "host.example.com"}'
Send a request that includes valid 'host' and 'service' values:
curl -s -XPOST 'https://nagios.example.com/livestatus-api/enable_notifications' -d '{"host": "host.example.com", "service": "httpd"}'
Send a request that includes a valid 'host' value and set 'scope' to 'all':
curl -s -XPOST 'https://nagios.example.com/livestatus-api/enable_notifications' -d '{"host": "host.example.com", "scope": "all"}'
Keep yer eyes peeled for the 'acknowledge_problem' endpoint!