Page 1 of 1

json query from Bash

Posted: Wed Feb 25, 2015 10:35 am
by WillemDH
Hello,

Today I combined some of my Reactor scripts, so that I can call an event chain with a host or with a hostgroup. The script detects if the target parameter is a host or a hostgroup and will then perform soem actions on the host or the hostgroup members.
To query the hostgroup members I was using the backend api in the past. As I really like the json way of doing things, and as I need json anyway to check if the hostgroup exists, I would like to transfrom the part of my scripts that query the hostgroup members from using the backend api component to json.
I seem to experience some issue though.

When I execute this query in a browser, I get a result:

Code: Select all

http://<nagiosserver>/nagios/cgi-bin/objectjson.cgi?query=hostgroup&hostgroup=hg_naf_reactor_win_autoupdate_prio5
The hostgroup "hg_naf_reactor_win_autoupdate_prio5" 100 % surely exists and has members.

When I do this query in Bash however I get a result with message

"message": "Hostgroup information requested, but no hostgroup name specified."

Other queries do work in Bash, for example

Code: Select all

HostList=$(curl -s $NagReadOnlyUser:$NagReadOnlyPw@$NagiosXiServer/nagios/cgi-bin/objectjson.cgi?query=hostlist)
So why is this query not working? ($Target is hg_naf_reactor_win_autoupdate_prio5)

Code: Select all

HostMemberList=$(curl -s $NagReadOnlyUser:$NagReadOnlyPw@$NagiosXiServer/nagios/cgi-bin/objectjson.cgi?query=hostgroup&hostgroup=$Target)
Thanks and grtz

Willem

Re: json query from Bash

Posted: Wed Feb 25, 2015 10:48 am
by abrist
I kept getting usage when testing this until I wrapped the full url in quote to pass to curl. The following spits out usage info:

Code: Select all

curl -k https://<user>:<pass>@<ip>/nagios/cgi-bin/objectjson.cgi?query=hostgroup&hostgroup=linux-servers
The following, actually returns the expected info:

Code: Select all

curl -k "https://<user>:<pass>@<ip>/nagios/cgi-bin/objectjson.cgi?query=hostgroup&hostgroup=linux-servers"
Odd and it does not match your specific behavior either. What version of core are you running?

Re: json query from Bash

Posted: Wed Feb 25, 2015 11:01 am
by WillemDH
Andy,

I'm on Nagios XI R2.6. I'm not sure what core version that is. I'm guessing you can answer that. I'll try tomorrow with double quotes and will let you know the results.

Grtz

Re: json query from Bash

Posted: Wed Feb 25, 2015 11:08 am
by abrist
WillemDH wrote:I'm on Nagios XI R2.6. I'm not sure what core version that is.
Probably 4.0.8. Let me know tomorrow about the quotes.

Re: json query from Bash

Posted: Thu Feb 26, 2015 3:14 am
by WillemDH
I can confirm it works with quotes. At the moment I'm using this command to only get the members. My sed knowledge is still developing however. What do you think of it Andy?

Code: Select all

curl -s "pass:user@nagiosserver/nagios/cgi-bin/objectjson.cgi?query=hostgroup&hostgroup=hg_naf_reactor_win_autoupdate_prio5" | sed -e '1,/members/d' | sed '/]/,+100 d' | tr -d '"' | tr -d ',' | tr -d ' '
Grtz

Willem

Re: json query from Bash

Posted: Thu Feb 26, 2015 11:14 am
by abrist
I tested the sed and it works ok on my boxes, though if you have some really odd characters in the hostnames/service descriptions, you may have parsing problems. There are a few different json2csv scripts out there (though it looks like you are going for newline separated), though again, it currently works fine with my (less than creatively named) hosts and services.

Re: json query from Bash

Posted: Sun Mar 01, 2015 6:42 am
by WillemDH
Well , as we don't have any special characters in any hostnames, I guess it will do it's job. Thanks for confirming. This thread can be closed.

Grtz

Willem