Page 1 of 1
How to get JSON output using API Key.
Posted: Wed Sep 23, 2020 1:55 am
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.
Re: How to get JSON output using API Key.
Posted: Wed Sep 23, 2020 4:50 pm
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.
Re: How to get JSON output using API Key.
Posted: Wed Sep 23, 2020 8:28 pm
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.
Re: How to get JSON output using API Key.
Posted: Wed Sep 23, 2020 11:42 pm
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?
Re: How to get JSON output using API Key.
Posted: Thu Sep 24, 2020 5:44 pm
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.
Re: How to get JSON output using API Key.
Posted: Fri Sep 25, 2020 12:32 am
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'
Re: How to get JSON output using API Key.
Posted: Fri Sep 25, 2020 11:10 am
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!