Page 1 of 1

Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Tue Jul 18, 2017 2:11 am
by sree478
Hi Team,

I need support on how to pass time variables to get Host/Service Status in Backend API URL

I want to get a host availability with up or down based on specific Host with day wise or between two dates.

If any possibility get reports directly from nagios?

Thanks
Srikanth

Re: Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Tue Jul 18, 2017 11:41 am
by mcapra
I'm not sure if the XI REST API can do that (it probably can), but you can definitely do it with the JSON CGIs present in Nagios Core.

Code: Select all

http://your_nagios_host/nagios/jsonquery.html
2017_07_18_11_36_56_JSON_Query_Generator.png
Note that the START and END times are Unix epochs.

Which then, on the right-hand side of the page, churns out a lovely HTTP request which returns JSON with the results you'd like (you will need to include credentials in your request):

Code: Select all

https://username:password@your_nagios_host/nagios/cgi-bin/archivejson.cgi?query=alertlist&objecttypes=host&hoststates=up+down+unreachable&hostname=esprod00&starttime=0&endtime=1500395712000
Which might return something like this:

Code: Select all

{
	"format_version": 0,
	"result": {
		"query_time": 1500395888000,
		"cgi": "archivejson.cgi",
		"user": "nagiosadmin",
		"query": "alertlist",
		"query_status": "released",
		"program_start": 1500385372000,
		"last_data_update": 1500392869000,
		"type_code": 0,
		"type_text": "Success",
		"message": ""
	},
	"data": {
		"selectors": {
			"endtime": 1500395712000000,
			"objecttypes": 1,
			"hostname": "esprod00"
		},
		"alertlist": [{
				"timestamp": 1497553881000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 2,
				"plugin_output": "CRITICAL - Could not complete HTTP request"
			}, {
				"timestamp": 1497553938000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 2,
				"plugin_output": "CRITICAL - Could not complete HTTP request"
			}, {
				"timestamp": 1497553998000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 2,
				"plugin_output": "CRITICAL - Could not complete HTTP request"
			}, {
				"timestamp": 1497554054000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 1,
				"plugin_output": "OK - Can connect to ElasticSearch on port 9200"
			}, {
				"timestamp": 1497965729000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 2,
				"plugin_output": "CRITICAL - Could not complete HTTP request"
			}, {
				"timestamp": 1500385374000,
				"object_type": 1,
				"name": "esprod00",
				"state_type": 2,
				"state": 1,
				"plugin_output": "OK - Can connect to ElasticSearch on port 9200"
			}
		]
	}
}
And if you only wanted OK/CRITICAL/whatever states, you would just adjust the hoststates part of your request.

Re: Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Tue Jul 18, 2017 4:27 pm
by tgriep
Thanks @mcapra for the help.

The API in XI does have the ability to specify the Start and End times on the API call to get specific dates.

Code: Select all

starttime 	<timestamp> (Default: -24 hours) 	objects/statehistory?starttime=1499808016 - Displays the last week of data until now.
endttime 	<timestamp> (Default: now) 	objects/statehistory?starttime=1499203216&endtime=1499808016 - Displays 1 week of data starting 2 weeks ago. Should be used with starttime.
To get more information on building queries in the API, login to the XI GUI and go to the Help > REST API Docs > Object Reference menu to see the options you can use in the queries.

Re: Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Wed Jul 19, 2017 2:24 am
by sree478
Hi mcapra,

cgi-bin/archivejson.cgi for older versions.For >5.0,it's not working.

Please suggest, if tried in higher version.



Thanks,
Srikanth

Re: Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Wed Jul 19, 2017 8:32 am
by mcapra
Refer to @tgriep's suggestion. The statehistory REST endpoint does roughly the same thing and has better granularity.

Though i'm 90% sure the JSON cgis should work on versions of XI >5.0. I was using it on 5.2.7 way back when, which I think had Nagios Core 4.1.1 under the hood. I most recently did this against a Nagios Core 4.3.0 machine. If it's not working, I think there's problems beyond the XI version, possibly an incorrect URL being used.

Re: Pass StartTime,Endtime Parameters in BackEnd URL API

Posted: Wed Jul 19, 2017 10:19 am
by tgriep
I just ran the JSON query on my XI server running 5.4.7 and it worked.
Verify that you are using this URL to access it. Replace xxx.xxx.xxx.xxx with the IP address or the hostname.

Code: Select all

http://xxx.xxx.xxx.xxx/nagios/jsonquery.html