Search code examples
jsonregexjqwildcardactivemq-artemis

Parse jolokia output with jq


I have an Apache Artemis broker, of which I can get some management information through jolokia. This response is in json format; I also have jq to do "json stuff" with it.

curl -s -X GET --url 'http://localhost:8161/console/jolokia/read/org.apache.activemq.artemis:*'

This works; and provides a json response.

I want to make a kind of generic script to check some values from this response; hence a few questions:

(For ease of testing I stored the response in a file broker.json, normally I would just pipe the output from curl to jq or store it in a variable, depending on how often jq has to be called)

One of the keys I want to query I can get like this:

 jq '."value"."org.apache.activemq.artemis:broker=\"broker1\""' broker.json

However, in a more generic script, I won't know the name of the broker (which is "broker1" here); is there some way I can wildcard the key like this: "org.apache.activemq.artemis:broker=\"*\"" ? My attempts so far have not given me anything

The second question is a bit harder I think. In the response there is a field that can be found by querying .request.timestamp the value is in seconds since epoch.

On the broker are queues, and some of them might have messages; I want to find those that have messages older than, say, 5 minutes.

I can find one such object with this key:

  jq '."value"."org.apache.activemq.artemis:address=\"my.queue\",broker=\"broker1\",component=addresses,queue=\"my.queue\",routing-type=\"anycast\",subcomponent=queues"' broker.json

This object contains two keys I can use for this purpose: - FirstMessageAge : age in ms - FirstMessageTimestamp: timestamp in miliseconds since epoch.

How would I query for this? Ideally I'd like to get the answer "my.queue has messages older than X"; where my.queue can also be obtained from having the key "Address" or "Name"

Artemis uses Address and Queues as separate entities; for all practical purposes here, both have the same name.

I am trying to make a (simple) script that can periodically monitor the broker health (not too many messages on queues for too long, queues having consumers, stuff like that; which all can be gotten from this single rest call; I think that with the answers to above questions I should be able to figure out how to get this.


Solution

  • is there some way I can wildcard the key like this: "org.apache.activemq.artemis:broker=\"*\""

    The best way to match wildcards on key names is by using with_entries or to_entries. Since you have not provided an example in accordance with the MCVE guidelines, it's not clear exactly how you'd do so, but by analogy with the example you give, you could start with:

    .value
    | to_entries[]
    | select(.key | test("^org.apache.activemq.artemis:broker=\".*\""))
    | .value