Command Line JSON Attributes Usage

Someone please help me understand the json_attributes usage!

Here is my code:

  - platform: command_line
    name: 'NGINX domain.com outBytes'
    command: RESULT=$( cat data/curl-nginx-vts.json ); ECODE=$?; test $ECODE -gt 0 && echo "ERROR $ECODE" || ( [ -z "$RESULT" ] && echo 'UNAVAIL' || echo $RESULT );
    value_template: "{{ value_json.value }}"
    json_attributes:
      - "serverZones"
    scan_interval: 130
    command_timeout: 10

This gets me all the values from serverZones and below, but I want to crack down to a specific value: serverZones > domain.com > outBytes

If I try to append any kind of JSON notation after serverZones I just get an “unknown” value.

Temp File Contents:

{
    "cacheZones": {
        "3971d76d0ff74c9aa94d94cbec56e47b": {
            "inBytes": 13956598,
            "maxSize": 2147483648,
            "outBytes": 7496455487,
            "overCounts": {
                "bypass": 0,
                "expired": 0,
                "hit": 0,
                "inBytes": 0,
                "maxIntegerSize": 1.8446744073709552e+19,
                "miss": 0,
                "outBytes": 0,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            },
            "responses": {
                "bypass": 0,
                "expired": 0,
                "hit": 41771,
                "miss": 42960,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            },
            "usedSize": 49668096
        }
    },
    "connections": {
        "accepted": 37377,
        "active": 12,
        "handled": 37377,
        "reading": 0,
        "requests": 97519,
        "waiting": 10,
        "writing": 2
    },
    "hostName": "blah.example.com",
    "loadMsec": 1684447824804,
    "nginxVersion": "1.22.1",
    "nowMsec": 1684528319842,
    "serverZones": {
        "*": {
            "inBytes": 20450743,
            "outBytes": 7519216855,
            "overCounts": {
                "1xx": 0,
                "2xx": 0,
                "3xx": 0,
                "4xx": 0,
                "5xx": 0,
                "bypass": 0,
                "expired": 0,
                "hit": 0,
                "inBytes": 0,
                "maxIntegerSize": 1.8446744073709552e+19,
                "miss": 0,
                "outBytes": 0,
                "requestCounter": 0,
                "requestMsecCounter": 0,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            },
            "requestBuckets": {
                "counters": [],
                "msecs": []
            },
            "requestCounter": 95657,
            "requestMsec": 475,
            "requestMsecCounter": 346635778,
            "requestMsecs": {
                "msecs": [
                    119,
                    173,
                    141,
                    172,
                    176,
                    108,
                    28,
                    75,
                    134,
                    55,
                    188,
                    55,
                    55,
                    354,
                    254,
                    926,
                    520,
                    58,
                    87,
                    660,
                    993,
                    1138,
                    1303,
                    1457,
                    1475,
                    594,
                    210,
                    1212,
                    1518,
                    322,
                    1661,
                    1949,
                    423,
                    388,
                    363,
                    911,
                    240,
                    278,
                    789,
                    441,
                    236,
                    507,
                    757,
                    410,
                    386,
                    396,
                    275,
                    409,
                    261,
                    401,
                    276,
                    455,
                    500,
                    194,
                    161,
                    101,
                    288,
                    183,
                    120,
                    325,
                    378,
                    769,
                    169
                ],
                "times": [
                    1684528263820,
                    1684528263884,
                    1684528314647,
                    1684528315447,
                    1684528264239,
                    1684528264239,
                    1684528264293,
                    1684528264294,
                    1684528264295,
                    1684528264309,
                    1684528264312,
                    1684528264406,
                    1684528264407,
                    1684528264407,
                    1684528264519,
                    1684528264521,
                    1684528264557,
                    1684528264576,
                    1684528264626,
                    1684528264627,
                    1684528264632,
                    1684528265824,
                    1684528269701,
                    1684528270400,
                    1684528273173,
                    1684528273173,
                    1684528278336,
                    1684528280754,
                    1684528282202,
                    1684528288364,
                    1684528291112,
                    1684528291112,
                    1684528291749,
                    1684528291749,
                    1684528293303,
                    1684528293303,
                    1684528293303,
                    1684528293303,
                    1684528317987,
                    1684528294147,
                    1684528295390,
                    1684528305428,
                    1684528306976,
                    1684528312244,
                    1684528317209,
                    1684528293515,
                    1684528293515,
                    1684528293515,
                    1684528293515,
                    1684528293515,
                    1684528293515,
                    1684528295379,
                    1684528308925,
                    1684528294513,
                    1684528297511,
                    1684528305439,
                    1684528313409,
                    1684528313803,
                    1684528315327,
                    1684528263746,
                    1684528263820,
                    1684528263820,
                    1684528263820
                ]
            },
            "responses": {
                "1xx": 0,
                "2xx": 78204,
                "3xx": 5531,
                "4xx": 9633,
                "5xx": 2289,
                "bypass": 0,
                "expired": 0,
                "hit": 41771,
                "miss": 45181,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            }
        },
        "domain.com": {
            "inBytes": 8428104,
            "outBytes": 5212956097,
            "overCounts": {
                "1xx": 0,
                "2xx": 0,
                "3xx": 0,
                "4xx": 0,
                "5xx": 0,
                "bypass": 0,
                "expired": 0,
                "hit": 0,
                "inBytes": 0,
                "maxIntegerSize": 1.8446744073709552e+19,
                "miss": 0,
                "outBytes": 0,
                "requestCounter": 0,
                "requestMsecCounter": 0,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            },
            "requestBuckets": {
                "counters": [],
                "msecs": []
            },
            "requestCounter": 37294,
            "requestMsec": 3996,
            "requestMsecCounter": 116441217,
            "requestMsecs": {
                "msecs": [
                    0,
                    0,
                    0,
                    19,
                    29,
                    144,
                    7517,
                    7844,
                    559,
                    1043,
                    1129,
                    0,
                    1670,
                    12638,
                    538,
                    538,
                    538,
                    538,
                    538,
                    616,
                    17,
                    81,
                    191,
                    206,
                    1012,
                    1013,
                    1014,
                    1028,
                    1029,
                    387,
                    386,
                    386,
                    1238,
                    1240,
                    1276,
                    1294,
                    1345,
                    1345,
                    1349,
                    1364,
                    541,
                    13967,
                    487,
                    14823,
                    15973,
                    16151,
                    16477,
                    16683,
                    16860,
                    849,
                    18013,
                    18,
                    18143,
                    20524,
                    9598,
                    9598,
                    23187,
                    311,
                    545,
                    709,
                    521,
                    695,
                    137
                ],
                "times": [
                    1684528257300,
                    1684528257316,
                    1684528257323,
                    1684528257323,
                    1684528257337,
                    1684528258069,
                    1684528258597,
                    1684528258952,
                    1684528259355,
                    1684528262878,
                    1684528263117,
                    1684528263264,
                    1684528263391,
                    1684528263746,
                    1684528263820,
                    1684528263820,
                    1684528263820,
                    1684528263820,
                    1684528263820,
                    1684528263884,
                    1684528264008,
                    1684528264129,
                    1684528264239,
                    1684528264239,
                    1684528264293,
                    1684528264294,
                    1684528264295,
                    1684528264309,
                    1684528264312,
                    1684528264406,
                    1684528264407,
                    1684528264407,
                    1684528264519,
                    1684528264521,
                    1684528264557,
                    1684528264576,
                    1684528264626,
                    1684528264627,
                    1684528264632,
                    1684528264647,
                    1684528264744,
                    1684528265047,
                    1684528265325,
                    1684528265932,
                    1684528267054,
                    1684528267231,
                    1684528267586,
                    1684528267763,
                    1684528267940,
                    1684528268736,
                    1684528269121,
                    1684528269132,
                    1684528269180,
                    1684528271605,
                    1684528272077,
                    1684528272077,
                    1684528274267,
                    1684528294147,
                    1684528295390,
                    1684528305428,
                    1684528306976,
                    1684528312244,
                    1684528317209
                ]
            },
            "responses": {
                "1xx": 0,
                "2xx": 30479,
                "3xx": 1622,
                "4xx": 4491,
                "5xx": 702,
                "bypass": 0,
                "expired": 0,
                "hit": 11775,
                "miss": 23618,
                "revalidated": 0,
                "scarce": 0,
                "stale": 0,
                "updating": 0
            }
        }
    },
    "sharedZones": {
        "maxSize": 20971520,
        "name": "vhost_traffic_status",
        "usedNode": 12,
        "usedSize": 42418
    }
}

The RESTful Sensor has a “json_attributes_path” to specify where the JSON attributes live, but it appears the Command Line Sensor has no equivalent. Therefore you might have to resort to loading the serverZones attribute in one sensor, and use a Template Sensor to get the value you need.

1 Like

So the Command Line json_attributes variable has no way of extracting values using a JSON path? I.e. it can only set attributes from the top level?

Can I extract the value using a value_template?