TECHZEN Zenoss User Community ARCHIVE  

Searching for examples of JSON shell programming

Subject: Searching for examples of JSON shell programming
Author: Larry Virden
Posted: 2016-05-10 08:24

Platform: Zenoss Core 5, Linux

My goal is to write a shell script that will obtain event information from 0 or more user selected events in the console, then loop through the results, extracting specific attributes to use in a report.

With some help, I have been able to get the EventsRouter query to return a string of events.

Then, I went looking for ways to deal with the return value from the query. I am looking at "jq" right now. My hope was to be able to save its results to a file, then decypher how to specify the attributes I want, as well as determine whether there is an attribute indicating an event is selected.

The issue I have withjq is that I cannot figure out how to capture its output into a file - or even into a pipe. All I get is a usage statement.

Subject: Larry, are you a Perl guy?
Author: [Not Specified]
Posted: 2016-05-11 11:54

Larry, are you a Perl guy You look like you might be. :-) If so I have a lot of Perl based JSON API scripts that I've put up on Github,

Subject: Tools for JSON
Author: Larry Virden
Posted: 2016-05-11 12:09

I've hacked around perl in my time. I've been trying to use as basic of tools as I can initially to get through a PoC, with the idea that during a pilot and production deployment we would need to begin learning python, etc.

Just to give you a bit of an idea what I'm trying to accomplish - here's where I am right now. I have no idea why jq was acting the way it was yesterday, but it is working better today. The zenoss_api still displays the curl output, but I just ignore it right now.

In a dream scenario, I'd love to find an evconsole_router set of arguments which would allow me to say "return the information from the events that the user has selected". So far, I've been unable to figure out the code that would let me do that. But this generates output that I can store into a file, then have someone use, if need be.

I fiddled with trying to do the output in one statement. But I havent figured out the solution to:

$ jq '. | "key:"[3].eventKey, "device:"[3].device.text' /tmp/json26443

jq: error (at /tmp/json26443:0): Cannot index string with string "result"

#! /usr/bin/bash

source /serviced/movedir/bin/

# Store off the results so we are working against a static known list


zenoss_api evconsole_router EventsRouter query '{"limit":2000,"params":{"eventState":[0,1]}}' > $json

if [ ! -s $json ] ; then

echo "$0: No event data found" >&2

exit 1


count=$(cat $json | jq '.result.totalCount')

# echo "Number of events is: x${count}x"


while [ "$i" -lt "$count" ] ; do

echo "event_${i}_key: $(jq '. |['${i}'].eventKey' $json )"

echo "event_${i}_device: $(jq '. |['${i}'].device.text' $json )"

echo "event_${i}_message: $(jq '. |['${i}'].message' $json )"

echo "event_${i}_severity: $(jq '. |['${i}'].severity' $json )"

echo "event_${i}_firstTime: $(jq '. |['${i}'].firstTime' $json )"

echo "event_${i}_lastTime: $(jq '. |['${i}'].lastTime' $json )"



Subject: Not familiar with the jq
Author: [Not Specified]
Posted: 2016-05-11 12:30

Not familiar with the jq toolset. Looks like it takes JSON and parses it for you In that case it looks like your echo statements are suspect. What's that pipe doing in there

But you're on the right track, a combination of EventsRouter query to get the list of events based on search criteria; and EventsRouter detail to show the details.

Subject: So this is the core of the
Author: Larry Virden
Posted: 2016-05-11 14:33

So this is the core of the echo statements:

jq '. |['${i}'].eventKey' $json

jq is the command which has many functions that can be performed against a JSON data file.

The "." means to get the entire JSON data structure .

The | means to pass the entire structure on to the next part of the expression, to refine or filter.

The[some number].eventKey is of course the JSON path to the relevent slot.

So that particular expression says to extract the specific slot from the specific array member of the specific JSON data stream.

In hind sight, it makes lots of sense. Trying to look at it from a novice view, it was pretty baffling.

Thank you so much for taking time to share your tools! They will likely come in handy.

< Previous
Zenoss Core 5.0 Software Inventory Blank
Zenoss Ipservice "SSH" problem