How to get JSON output using API Key.

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
faldana
Posts: 4
Joined: Wed Apr 22, 2020 11:26 pm

How to get JSON output using API Key.

Post by faldana »

Hi, There.

I am trying to get some JSON output and check the status of the services we have.
However, this API requires an authentication method which we are using API Keys.
I've tried check_json script, but it doesn't show an option to provide the keyid:secret.

Is any way to accomplish that?
Thanks.
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: How to get JSON output using API Key.

Post by cdienger »

If you're using check_json.php which comes with the XI install then you can use the -h option to add a header to the request:

Code: Select all

./check_json.php -u 'http://1.2.3.4' -k '.key' -h 'keyid:secret' -w 1 -c 2
the above will add a http header called keyid with a value of secret to the request.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
faldana
Posts: 4
Joined: Wed Apr 22, 2020 11:26 pm

Re: How to get JSON output using API Key.

Post by faldana »

Hi cdienger,

Thanks for your reply.
I got to run the command using the syntax below:

php /usr/local/nagios/libexec/check_json.php -u 'https://<KEYID>:<SECRET>@<URL>' -k "status" -s 'normal' -r 0 -n 2

However, it brings me the output below:

PHP Notice: Array to string conversion in /usr/local/nagios/libexec/check_json.php on line 113
CRITICAL - Value 'Array' did not match 'normal'

The value I am trying to get is:

[
{
"status": "normal", <<<<<<<<<<<<<<<<<<<<<<<<<<
"type": "string",
"fqdn": "string",
"available_seconds": 0,
"notifications": [
{
"status": "normal",
"token": "string",
"message": "string"
}
],

Maybe, I am mentioning the wrong json key format.
User avatar
faldana
Posts: 4
Joined: Wed Apr 22, 2020 11:26 pm

Re: How to get JSON output using API Key.

Post by faldana »

I ran the command using the syntax and it doesn't work. So, I used the syntax below:

php /usr/local/nagios/libexec/check_json.php -u 'https://<$KEYID>:<$SECRET>@<$URL>:<$PORT>/api/v2/health' -k 'status' -s 'normal' -r 0 -n 2
PHP Notice: Array to string conversion in /usr/local/nagios/libexec/check_json.php on line 113
CRITICAL - Value 'Array' did not match 'normal'

I am trying to retrieve this information:

[
{
"status": "normal", <<<<<<<<<<<<<<<<<<<<
"type": "string",
"fqdn": "string",
"available_seconds": 0,
"notifications": [
{
"status": "normal",
"token": "string",
"message": "string"
}
],

Is anything wrong with my code?
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: How to get JSON output using API Key.

Post by ssax »

I'll lab this up and see what I can find but it's the end of our day today, if you don't hear from me by midday tomorrow please reply to the thread so that it pops up on my dashboard.
User avatar
faldana
Posts: 4
Joined: Wed Apr 22, 2020 11:26 pm

Re: How to get JSON output using API Key.

Post by faldana »

Hi, Tks for the feedback.

After tracing the script and check the Array output, I got the way it is evaluated:

# php /usr/local/nagios/libexec/check_json.php -u '<URL>/api/v2/health' -k 0.nodes.0.services.status -s normal -r 0 -n 2
OK - Value 'normal' matched 'normal'
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: How to get JSON output using API Key.

Post by ssax »

Thank you for posting your resolution! Let us know when we're okay to lock this up and mark it as resolved.

Thank you!
Locked