extract availability data

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
smoren
Posts: 62
Joined: Tue Sep 29, 2015 7:24 am

extract availability data

Post by smoren »

Hello,
is there any supported/documented method to get availability data from nagios to external system e.g. using some API? My idea is to call some function with parameters similar to the ones in availability report, and get back 'Service Data' table in the form of JSON (for example).

As far as I know, using direct SQL query for this might be quite challenging (correct me if I'm wrong), however direct sql query would be best solution for us. Just an idea - could you develop stored procedure or function that would return availability data? I believe this would be very useful for many customers(e.g. to report SLA using custom manager-friendly ;) interface).

Thanks.
bwallace
Posts: 1145
Joined: Tue Nov 17, 2015 1:57 pm

Re: extract availability data

Post by bwallace »

As of Nagios XI 5, a new REST API was introduced (this new API uses JSON instead of XML). This API allows you to read, write, delete, and update data in the Nagios XI system through commands that are authenticated via Nagios XI API keys.
All of the documentation is available inside of the Nagios XI product via the Help menu. In the left pane you will find the section Nagios XI API Docs. The API is split into three separate sections and the one which seems to most apply to your scenario is 'Objects.' This is the standard read-only backend API that has always been available in Nagios XI. The objects section returns XML or JSON data about objects in Nagios XI including things like object statuses, contacts, contact groups, logs, history, downtime, etc.

Have you tried this approach?
https://support.nagios.com/kb/article.php?id=176
Be sure to check out the Knowledgebase for helpful articles and solutions!
smoren
Posts: 62
Joined: Tue Sep 29, 2015 7:24 am

Re: extract availability data

Post by smoren »

I know about that API, but I didn't find right command to return availability. I'm missing something like: GET objects/serviceavailability...
If I understand this correctly, availability report component (including its legacy equivalent) makes that really complex computation of availability. And I'm trying to find a way, how to get result of such computation without reinventing a wheel (making such computation on my own and guessing algorithm...).

What do you think about making that procedure/function in database? Is it even possible to get such data purely from database? Or are there some limitations(e.g. without ability to define 'Report Time Period')?
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: extract availability data

Post by mcapra »

The bulk of the Nagios historical data is contained in Round Robin Database files created by RRDtool. Generally these files are kept in the following path: /usr/local/nagios/share/perfdata. If you wanted to do your own calculations, all the data you would need should be in those RRDs.

You might be able to do some short term reporting with the nagios.nagios_logentries table based on logentry_data fields. You'd have to parse every single field though and I imagine that would get pretty heavy pretty fast.

I don't think anything pertaining to reporting will be included in the REST API, but I put in a feature request for it (internal ID 9208). Mostly because a lot of that functionality currently exists under the enterprise license. Generating a report is also a fairly heavy task which takes a long time to resolve; Not something that lends it self very well to a REST API. Perhaps services/hosts could instead maintain a "running tally" of their average availability internally? I'll leave that up to the devs to figure out. I agree that this would be useful functionality. Something like a general "Health Summary" for a host/service would probably have to be implemented in Core first.
Former Nagios employee
https://www.mcapra.com/
smoren
Posts: 62
Joined: Tue Sep 29, 2015 7:24 am

Re: extract availability data

Post by smoren »

I don't want nor need to go into RRD. And I'm trying not to make my own calculation, since nagios is doing it just fine.

I was able to make some reporting directly from database - So far I'm able to return a table: service name, outage start, outage end, outage duration, comment. This works for our another use-case... Challenging part in calculating availability is to take into account scheduled downtimes, reporting periods,... That's why I'd like your feedback if it is even possible to do this all only in database. Currently I don't know how complex this can be, but I believe your devs do know. So if I may ask, try to ask them if it is possible to return such data only from database. If yes, please create a feature request for one of these(whatever seems to be quicker):
- create a stored procedure/function in database that will return availability data for specific service, or
- create a knowledge base document with detailed example of how such SQL query should look like, including all possible filtering options (like in availability report)

If they answer No(or do not recommend such approach), I'd wait for feature request you just created (ID 9208). By the way, we have enterprise license. I believe such request should be fairly easy. Just define new API command and call function that is called when I click on 'Run' button in availability report :) In first version I'd leave possible capacity issues on integrators (me :-)), and perhaps later some well-defined indices may improve performance. I'm not sure if there is any way how to compute availability in advance. There is just too many variables(period, report time period, etc..).

Thanks.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: extract availability data

Post by ssax »

You should be able to get availability data from here:

http://YOURXISERVER/nagios/jsonquery.html

CGI: Archive JSON CGI
Query: availability

Fill out the rest of the data, click the Send Query button, and it will show you the JSON and the URL you can call to get that data.

Will that work for you?
smoren
Posts: 62
Joined: Tue Sep 29, 2015 7:24 am

Re: extract availability data

Post by smoren »

Thanks. Seems to be interesting. Please clarify some things for me:
- Start Time and End time must be entered in epoch time(if there are another options, let me know)
- is there any way how to automatically return percentage?
- if not, is this calcuation correct?: %available = time_ok / (time_ok + time_warning + time_critical + time_unknown) *100%
- what are "scheduled*" values? Should it be included in previous formula?
- is there any detailed documentation for json values? (e.g. what are possible values for "type_code" or "query_status" and their meaning)

I'd wait for your statement regarding reporting directly from database. It would make my work much easier. But jsonquery.html is also much better than nothing ;-) I hope there are no plans to withdraw these json cgi scripts...
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: extract availability data

Post by ssax »

The availability data is generated on-the-fly so direct DB access to the data is not possible (and it's pulled from the /usr/local/nagios/var/nagios.log and /usr/local/nagios/var/archives/* files)

Actually, you should be able to construct the URL and parse the data as CSV (which includes the percentages):

For all hostgroups:

http://YOURXISERVER/nagios/cgi-bin/avai ... tgroup=all[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=24x7&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=0&backtrack=4&csvoutput=

For a single hostgroup:

http://YOURXISERVER/nagios/cgi-bin/avai ... ux-servers[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=24x7&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=0&backtrack=4&csvoutput=

For all hosts:

http://YOURXISERVER/nagios/cgi-bin/avai ... ]&host=all[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=24x7&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=6&backtrack=4&csvoutput=

For a single host:

http://YOURXISERVER/nagios/cgi-bin/avai ... =localhost[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=24x7&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=6&backtrack=4&csvoutput=

For all servicegroups:

http://YOURXISERVER/nagios/cgi-bin/avai ... egroup=all[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=6&backtrack=4&csvoutput=

For a single servicegroup:

http://YOURXISERVER/nagios/cgi-bin/avai ... rvicegroup[/b]&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=6&backtrack=4&csvoutput=

For a single service:

http://YOURXISERVER/nagios/cgi-bin/avai ... =localhost[/b]&service=SSH&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedservicestate=6&backtrack=4&csvoutput=

But to be honest, you're really better off doing this:
- Go to http://YOURXISERVER/nagios/cgi-bin/avail.cgi
- Select if you want hostgroups, hosts, servicesgroups, or services
- Continue through your selection until you get to the Select Report Options page
- Set all the report options to what you want
- Hit the F12 key which will bring up the Firefox/Chrome inspector
- Click the Network tab
- Click the Create Availability Report! button
- Right-click on the avail.cgi entry and choose Copy URL and it will give you the URLs that you can use to query that information (see attached image).
Inspector.png
Note that you can add &csvoutput= on the end of the URL if you want the CSV.
You do not have the required permissions to view the files attached to this post.
smoren
Posts: 62
Joined: Tue Sep 29, 2015 7:24 am

Re: extract availability data

Post by smoren »

Hello,
thank you very much for exhaustive answer. So if I understand it correctly, you do not use DB to calculate availability (although it contains some useful data - statehistory table seems to be good starting point). In that case, it doesn't seem to be a good approach to do it on my own. Most probably I wouldn't be able to cover all possible situations. Correct me if I'm wrong...

So lets get back to another option - let nagios do the calculations and just export data. I tried steps you suggested, but I was not able to get csv data for service nor service groups (only these types are useful for me). Even wizard did not give me the option 'Output in CSV Format'. This option is available only in report for all services. But when I have tried to change URL and add parameter &csvoutput=, I got only html file containing table with data. Am I doing something wrong, or is there another way how to get that csv(for single service or service group)?

One more question - if I add attributes username and ticket to the url(accessing avail.cgi), should it work? Reason for this is that I'm trying to automate generation of these csv files and upload it on a fileserver. Do you have any idea how to do it using nagios? As far as I know, there is only option to send such file only via email, which is not very useful in my case. If I'm right, please consider creating a feature request to add option to upload (instead of sending email) such file to fileserver(using as much protocols as possible, but ftp could be a good start).
Until then my plan is to find a way how to automatically get csv reports (with your help), and then use old good cron to automate it. However, if you have any ideas how to make my life simpler :-) I'm open to suggestions...
Thanks.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: extract availability data

Post by ssax »

Ahh, it looks like you can't for service groups, sorry about that.

Correct, we do not currently use the DB to calculate availability (this may change in the future).

Because you are technically accessing the Nagios Core backend when calling the avail.cgi you would use:

Code: Select all

http://username:password@YOURXISERVER/nagios/cgi-bin/avail.cgi...
But you will essentially need to convert the HTML to a CSV, you won't be able to get the data any other way until it's implemented in the API.

You could do something like this:

Code: Select all

curl -s 'URL' | awk '/reportTime/{i++}i' | python htmltocsv.py > /tmp/OUTPUT.csv

So, using the information that we've gathered thus far:

Code: Select all

curl -s 'http://username:password@YOURXISERVER/nagios/cgi-bin/avail.cgi?show_log_entries=&hostgroup=all&timeperiod=last7days&smon=8&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=8&eday=3&eyear=2016&ehour=24&emin=0&esec=0&rpttimeperiod=24x7&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedhoststate=3&initialassumedservicestate=0&backtrack=4' | awk '/reportTime/{i++}i' | python htmltocsv.py > /tmp/myavail.csv
Let me know if that works for you.
Locked