check_json.pl 中 JSON 查询的属性语法

问题描述 投票:0回答:1

因此,我尝试在 NagiosXI 中设置 check_json.pl 来监控一些统计数据。 https://github.com/c-kr/check_json

我正在使用 代码以及我在拉取请求#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 中 filename1 的总字节数,其中我知道 customer_id 和索引,但不知道它们在各自数组中的位置。

我可以使用字符串“

[0]->{'total_bytes'}
”来监视 customer1 的总字节数,但我需要能够指定哪个客户并深入研究文件名(已知)和文件大小(要监视的统计数据)并且工作查询只提供给我状态(正常、警告或严重)。添加 -p 我得到的都是错误......

无论我如何表述,-p 的错误始终是:

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

即使我可以从示例“

[0]->{'total_bytes'}
”中获得有效的 OK,在 -p 中使用它仍然会给出相同的错误。

指向有关要使用的格式的文档的链接将非常有帮助。脚本的自述文件或 -h 输出中的示例在这里让我失败。有什么想法吗?

arrays json perl nagios nagiosxi
1个回答
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 总和?

© www.soinside.com 2019 - 2024. All rights reserved.