简体   繁体   English

用jq解析jolokia输出

[英]Parse jolokia output with jq

I have an Apache Artemis broker, of which I can get some management information through jolokia. 我有一个Apache Artemis经纪人,可以通过jolokia获得其中的一些管理信息。 This response is in json format; 此响应为json格式; I also have jq to do "json stuff" with it. 我也有jq用它做“ json东西”。

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

This works; 这有效; and provides a json response. 并提供json响应。

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) (为了便于测试,我将响应存储在文件broker.json中,通常我会将输出从curl传递到jq或将其存储在变量中,具体取决于必须调用jq的频率)

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); 但是,在更通用的脚本中,我将不知道经纪人的名称(此处为“ broker1”)。 is there some way I can wildcard the key like this: "org.apache.activemq.artemis:broker=\\"*\\"" ? 有什么办法可以像这样通配密钥: "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. 在响应中,可以通过查询.request.timestamp找到一个字段,该值以秒为单位。

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. 我想查找那些消息早于5分钟的消息。

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. 该对象包含两个我可以用于此目的的键:-FirstMessageAge:以毫秒为单位的年龄-FirstMessageTimestamp:自纪元以来以毫秒为单位的时间戳。

How would I query for this? 我将如何查询? Ideally I'd like to get the answer "my.queue has messages older than X"; 理想情况下,我希望得到答案“ my.queue的消息早于X”; where my.queue can also be obtained from having the key "Address" or "Name" 也可以通过具有键“地址”或“名称”来获取my.queue

Artemis uses Address and Queues as separate entities; Artemis使用“地址”和“队列”作为单独的实体; 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. 我正在尝试制作一个(简单的)脚本,该脚本可以定期监视代理运行状况(队列中没有太多消息,时间太长,队列中没有使用方,诸如此类;所有这些都可以从单个rest调用中获得;我认为有了以上问题的答案,我应该能够弄清楚如何做到这一点。

is there some way I can wildcard the key like this: "org.apache.activemq.artemis:broker=\\"*\\"" 有什么办法可以像这样通配密钥: "org.apache.activemq.artemis:broker=\\"*\\""

The best way to match wildcards on key names is by using with_entries or to_entries . 匹配键名上的通配符的最佳方法是使用with_entriesto_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: 由于您尚未提供符合MCVE准则的示例,因此尚不清楚您将如何使用它,但是与给出的示例类似,您可以从以下内容开始:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM