How to use the command 'gnmic get' (with examples)

How to use the command 'gnmic get' (with examples)

The ‘gnmic get’ command is used to get a snapshot of a gnmi network device operation data. It allows you to query the device state at specific paths and retrieve the response in the desired encoding format.

Use case 1: Get a snapshot of the device state at a specific path

Code:

gnmic --address ip:port get --path path

Motivation: This use case is useful when you want to fetch the current state or value of a specific data path on a network device. It helps you retrieve real-time information about a specific parameter or metric of interest.

Explanation:

  • --address ip:port: Specifies the IP address and port number of the network device.
  • get: Indicates the action of retrieving data from the device.
  • --path path: Specifies the path to the desired data node on the device.

Example output:

{
  "updates": [
    {
      "path": "system/interfaces/interface[name=eth0]/state/counters/in-all",
      "value": 12345
    }
  ]
}

Use case 2: Query the device state at multiple paths

Code:

gnmic -a ip:port get --path path1 --path path2

Motivation: This use case allows you to query the device state at multiple paths simultaneously. It is useful when you need to fetch data from multiple data nodes on the device and retrieve them together in a single response.

Explanation:

  • -a ip:port: Specifies the IP address and port number of the network device.
  • get: Indicates the action of retrieving data from the device.
  • --path path1 and --path path2: Specifies the paths to the desired data nodes on the device.

Example output:

{
  "updates": [
    {
      "path": "system/interfaces/interface[name=eth0]/state/counters/in-all",
      "value": 12345
    },
    {
      "path": "system/interfaces/interface[name=eth1]/state/counters/out-unicast-pkts",
      "value": 9876
    }
  ]
}

Use case 3: Query the device state at multiple paths with a common prefix

Code:

gnmic -a ip:port get --prefix prefix --path path1 --path path2

Motivation: When you have multiple data paths that share a common prefix, this use case allows you to query the device state at those paths efficiently. It helps reduce redundancy by specifying the common prefix only once.

Explanation:

  • -a ip:port: Specifies the IP address and port number of the network device.
  • get: Indicates the action of retrieving data from the device.
  • --prefix prefix: Specifies the common prefix for the paths.
  • --path path1 and --path path2: Specifies the paths relative to the common prefix.

Example output:

{
  "updates": [
    {
      "path": "system/interfaces/interface[name=eth0]/state/counters/in-all",
      "value": 12345
    },
    {
      "path": "system/interfaces/interface[name=eth0]/state/counters/out-all",
      "value": 54321
    },
    {
      "path": "system/interfaces/interface[name=eth1]/state/counters/in-all",
      "value": 9876
    }
  ]
}

Use case 4: Query the device state and specify response encoding (json_ietf)

Code:

gnmic -a ip:port get --path path --encoding json_ietf

Motivation: In some cases, you may want to specify the encoding format of the response to suit your requirements. This use case allows you to retrieve the device state and specify the response encoding as “json_ietf” for better compatibility with other systems or tools.

Explanation:

  • -a ip:port: Specifies the IP address and port number of the network device.
  • get: Indicates the action of retrieving data from the device.
  • --path path: Specifies the path to the desired data node on the device.
  • --encoding json_ietf: Specifies the desired encoding format for the response as “json_ietf”.

Example output:

{
  "updates": [
    {
      "path": "system/interfaces/interface[name=eth0]/state/counters/in-all",
      "value": 12345
    }
  ]
}

Conclusion:

The ‘gnmic get’ command provides a flexible way to retrieve device state and operational data from network devices. With various options and arguments, you can efficiently query specific paths, multiple paths, and control the response encoding format. This command is a powerful tool for network administrators and operators to gather real-time information from network devices.

Related Posts

How to use the command az login (with examples)

How to use the command az login (with examples)

The “az login” command is used to log in to Azure using the Azure CLI (Command Line Interface).

Read More
How to use the command "sockstat" (with examples)

How to use the command "sockstat" (with examples)

The “sockstat” command is used to list open Internet or UNIX domain sockets.

Read More
How to use the command 'timedatectl' (with examples)

How to use the command 'timedatectl' (with examples)

Timedatectl is a command-line tool in Linux that allows users to control and manage the system time and date.

Read More