2017-08-31 78 views
-2

因此,我试图在NagiosXI中设置check_json.pl来监视一些统计信息。 https://github.com/c-kr/check_jsoncheck_json.pl中JSON查询的属性语法

我使用的是code with the modification I submitted in pull request #32,所以行号反映了该代码。

JSON的查询返回这样的事:

[ 
    { 
     "total_bytes": 123456, 
     "customer_name": "customer1", 
     "customer_id": "1", 
     "indices": [ 
      { 
       "total_bytes": 12345, 
       "index": "filename1" 
      }, 
      { 
       "total_bytes": 45678, 
       "index": "filename2" 
      }, 

     ], 
     "total": "765.43gb" 
    }, 
    { 
     "total_bytes": 123456, 
     "customer_name": "customer2", 
     "customer_id": "2", 
     "indices": [ 
      { 
       "total_bytes": 12345, 
       "index": "filename1" 
      }, 
      { 
       "total_bytes": 45678, 
       "index": "filename2" 
      }, 

     ], 
     "total": "765.43gb" 
    } 
] 

我试图监视大小的特定文件。所以检查应该是这个样子:

/path/to/check_json.pl -u https://path/to/my/json -a "SOMETHING" -p "SOMETHING" 

...这里我试图找出出头,这样我可以监视的customer2文件名1的TOTAL_BYTES那里,我认识了CUSTOMER_ID和指标,但不是他们的位置在相应的阵列中。

我可以通过使用字符串“[0]->{'total_bytes'}”来监控customer1的总字节数,但我需要能够指定哪个客户并深入挖掘文件名(已知)和文件大小(监控的stat),而工作查询只给出我的状态(OK,WARNING或CRITICAL)。添加-p我得到的是错误....

与-p错误无论怎样我已经能够句话总是:

Not a HASH reference at ./check_json.pl line 235. 

即使我能得到一个有效的OK从示例“[0]->{'total_bytes'}”中,使用-p仍然会给出相同的错误。

链接指向要使用的格式的文档将是非常有用的。 README中的脚本或-h输出中的示例在这里失败。有任何想法吗?

+0

如果你要向下纪念这个岗位,我将不胜感激,如果你至少有礼节线索我,为什么。 –

+0

看起来,你和原始check_json之间的巨大断开是原始的期望返回的JSON是一个单一的对象,而你的JSON是一个对象数组。你的$ json_response是一个ARRAY ref:第235行(和第229,259,265行)期望它是一个HASH文件。 –

回答

0

我真的不知道你的问题是什么。我敢肯定,我并不孤单,因此是降价。

一旦你解码JSON,如果你有一个CUSTOMER_ID搜索,你可以这样做:

my ($customer_info) = grep {$_->{customer_id} eq $customer_id} @$json_response; 

关于上线235错误,这看起来很奇怪:

foreach my $key ($np->opts->perfvars eq '*' ? map { "{$_}"} sort keys %$json_response : split(',', $np->opts->perfvars)) { 
    # ....................................... ^^^^^^^^^^^^^ 
    $perf_value = $json_response->{$key}; 

如果perfvars eq“*”,例如,您似乎在寻找$json_reponse->{"{total}"}。您可能需要验证用户输入:

die "no such key in json data: '$key'\n" unless exists $json_response->{$key}; 

这整个字符串化的哈希查找裁判的业务只是味道不好。

更好的问题应该是这样的:

我有这样的JSON数据。如何获得ID为1的客户的total_bytes总和?

https://stackoverflow.com/help/mcve