1

I manage to pull out some interface stats using Cisco genie and below is the JSON (apology for the long JSON).

As you can see, the Interface name is not static. Can I know how do I pull out the Interface Name together with other stats using json_query?

I use this query msg.genie.interface.*.{Bandwidth: bandwidth, crc: counters.in_errors, mac: mac_address, duplex: duplex_mode, speed: port_speed, vrf: vrf, oper: oper_status} and the result is this.

I am just missing the Interface name. Any suggestion on how do include the Interface name in the output?

JMESPath query:

msg.genie.interface.*.{Bandwidth: bandwidth, crc: counters.in_errors, mac: mac_address, duplex: duplex_mode, speed: port_speed, vrf: vrf, oper: oper_status}

outputs:

[
  {
    "Bandwidth": 1000000,
    "crc": 0,
    "mac": "2cab.eb8c.59ff",
    "duplex": "auto",
    "speed": "auto speed",
    "vrf": "Mgmt-intf",
    "oper": "down"
  },
  {
    "Bandwidth": 1000000,
    "crc": 0,
    "mac": "2cab.eb8c.5970",
    "duplex": "full",
    "speed": "1000mbps",
    "vrf": null,
    "oper": "up"
  }
]

Full JSON from the Cisco genie pull:

{
    "msg": {
        "changed": false,
        "failed": false,
        "genie": {
            "interface": {
                "GigabitEthernet0": {
                    "auto_negotiate": true,
                    "bandwidth": 1000000,
                    "counters": {
                        "in_broadcast_pkts": 0,
                        "in_crc_errors": 0,
                        "in_errors": 0,
                        "in_mac_pause_frames": 0,
                        "in_multicast_pkts": 0,
                        "in_octets": 0,
                        "in_pkts": 0,
                        "last_clear": "never",
                        "out_errors": 0,
                        "out_mac_pause_frames": 0,
                        "out_octets": 0,
                        "out_pkts": 0,
                        "rate": {
                            "in_rate": 0,
                            "in_rate_pkts": 0,
                            "load_interval": 300,
                            "out_rate": 0,
                            "out_rate_pkts": 0
                        }
                    },
                    "delay": 10,
                    "duplex_mode": "auto",
                    "enabled": false,
                    "encapsulation": {
                        "encapsulation": "arpa"
                    },
                    "flow_control": {
                        "receive": false,
                        "send": false
                    },
                    "mac_address": "2cab.eb8c.59ff",
                    "mtu": 1500,
                    "oper_status": "down",
                    "phys_address": "2cab.eb8c.59ff",
                    "port_channel": {
                        "port_channel_member": false
                    },
                    "port_speed": "auto speed",
                    "switchport_enable": false,
                    "type": "RP management port",
                    "vrf": "Mgmt-intf"
                },
                "GigabitEthernet0/0/0": {
                    "accounting": {
                        "arp": {
                            "chars_in": 1253820,
                            "chars_out": 1347648,
                            "pkts_in": 20897,
                            "pkts_out": 21057
                        },
                        "cdp": {
                            "chars_in": 93408529,
                            "chars_out": 93849340,
                            "pkts_in": 198326,
                            "pkts_out": 220327
                        },
                        "ip": {
                            "chars_in": 43533539576,
                            "chars_out": 243171224580,
                            "pkts_in": 51407196,
                            "pkts_out": 272100765
                        },
                        "ipv6": {
                            "chars_in": 351879534,
                            "chars_out": 0,
                            "pkts_in": 2933568,
                            "pkts_out": 0
                        },
                        "other": {
                            "chars_in": 1330840333,
                            "chars_out": 95196860,
                            "pkts_in": 18437333,
                            "pkts_out": 241382
                        },
                        "spanning tree": {
                            "chars_in": 2276096512,
                            "chars_out": 0,
                            "pkts_in": 35564008,
                            "pkts_out": 0
                        }
                    },
                    "auto_negotiate": true,
                    "bandwidth": 1000000,
                    "counters": {
                        "in_broadcast_pkts": 0,
                        "in_crc_errors": 0,
                        "in_errors": 0,
                        "in_mac_pause_frames": 0,
                        "in_multicast_pkts": 27276714,
                        "in_octets": 45638217915,
                        "in_pkts": 78759308,
                        "last_clear": "never",
                        "out_errors": 0,
                        "out_mac_pause_frames": 0,
                        "out_octets": 243268978899,
                        "out_pkts": 272342613,
                        "rate": {
                            "in_rate": 25000,
                            "in_rate_pkts": 5,
                            "load_interval": 300,
                            "out_rate": 73000,
                            "out_rate_pkts": 5
                        }
                    },
                    "delay": 10,
                    "duplex_mode": "full",
                    "enabled": true,
                    "encapsulation": {
                        "encapsulation": "dot1q",
                        "first_dot1q": "1"
                    },
                    "flow_control": {
                        "receive": false,
                        "send": false
                    },
                    "mac_address": "2cab.eb8c.5970",
                    "mtu": 1500,
                    "oper_status": "up",
                    "phys_address": "2cab.eb8c.5970",
                    "port_channel": {
                        "port_channel_member": false
                    },
                    "port_speed": "1000mbps",
                    "switchport_enable": false,
                    "type": "ISR4321-2x1GE"
                }
            }
        }
    }
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Michael
  • 197
  • 1
  • 2
  • 10
  • Sadly, this is not possible in JMESPath. the only function that gives you keys will make you lose the content of the objects (e.g. `keys(msg.genie.interface)` will give `["GigabitEthernet0", "GigabitEthernet0/0/0"]` ). And there is not much you can do about it since JMESPath do not allow you to request the key of the parent object although accessing the parent object is an often requested feature: https://github.com/jmespath/jmespath.js/issues/22 – β.εηοιτ.βε Jul 26 '21 at 17:31
  • I see....so jmespath only allow to trace "downwards" (parent to child) but not the reverse from child to parent? Does anyone know of any json tools that can do both ways? Or maybe have to do a "for" loop in python as a workaround... – Michael Jul 26 '21 at 23:32
  • [tag:jq] might well be able to do this. – β.εηοιτ.βε Jul 27 '21 at 16:48
  • Able to point me to a simple jq example of getting a parent key with a child query? ie. output should display both they parent key and child stats? Thanks.. – Michael Jul 27 '21 at 22:41

0 Answers0