How to use the command gnmic subscribe (with examples)
The gnmic subscribe
command is used to subscribe to the state updates of a gnmic network device. It allows users to receive notifications whenever there are changes in the specified path of the device. This can be helpful for monitoring and managing network devices in real-time.
Use case 1: Subscribe to target state updates under the subtree of a specific path
Code:
gnmic --address ip:port subscribe --path path
Motivation: This use case is useful when you want to subscribe to the state updates of a specific subtree in the network device. By specifying the path, you can focus on the relevant information and receive updates only for that portion of the device’s state.
Explanation:
--address ip:port
: Specifies the IP address and port of the gNMI target.--path path
: Specifies the path of the target state updates to subscribe to.
Example output:
2022-01-01T10:00:00Z path: <path>, update:<update>
2022-01-01T10:00:01Z path: <path>, update:<update>
...
Use case 2: Subscribe to a target with a sample interval of 30s (default is 10s)
Code:
gnmic -a ip:port subscribe --path path --sample-interval 30s
Motivation: By setting a sample interval, you can control how frequently you receive state updates from the target. This can be useful when you want to reduce the frequency of updates to avoid overwhelming your system with notifications.
Explanation:
-a ip:port
: Specifies the IP address and port of the gNMI target.--path path
: Specifies the path of the target state updates to subscribe to.--sample-interval 30s
: Sets the sample interval to 30 seconds.
Example output:
2022-01-01T10:00:00Z path: <path>, update:<update>
2022-01-01T10:00:30Z path: <path>, update:<update>
2022-01-01T10:01:00Z path: <path>, update:<update>
...
Use case 3: Subscribe to a target with sample interval and updates only on change
Code:
gnmic -a ip:port subscribe --path path --stream-mode on-change --heartbeat-interval 1m
Motivation: This use case is helpful when you want to receive state updates only when there are actual changes in the target’s state. By using the on-change
stream mode, you can avoid receiving unnecessary updates. The heartbeat interval can be used to specify the interval at which the target sends heartbeats to ensure the connection is active.
Explanation:
-a ip:port
: Specifies the IP address and port of the gNMI target.--path path
: Specifies the path of the target state updates to subscribe to.--stream-mode on-change
: Sets the stream mode to only send updates on change.--heartbeat-interval 1m
: Sets the heartbeat interval to 1 minute.
Example output:
2022-01-01T10:00:00Z path: <path>, update:<update>
2022-01-01T10:02:30Z path: <path>, update:<update>
2022-01-01T10:03:00Z path: <path>, update:<update>
...
Use case 4: Subscribe to a target for only one update
Code:
gnmic -a ip:port subscribe --path path --mode once
Motivation: Sometimes, you may only need to receive one update from the target. This use case allows you to subscribe to the target for a single update and then unsubscribe automatically. It can be useful when you want to retrieve immediate information without continuously monitoring the target.
Explanation:
-a ip:port
: Specifies the IP address and port of the gNMI target.--path path
: Specifies the path of the target state updates to subscribe to.--mode once
: Sets the mode to receive only one update.
Example output:
2022-01-01T10:00:00Z path: <path>, update:<update>
Use case 5: Subscribe to a target and specify response encoding (json_ietf)
Code:
gnmic -a ip:port subscribe --path path --encoding json_ietf
Motivation: This use case allows you to specify the response encoding format for the state updates. By using the json_ietf
encoding, you can receive the updates in a JSON format that follows the IETF standards. This can be helpful for interoperability and ease of integration with other systems.
Explanation:
-a ip:port
: Specifies the IP address and port of the gNMI target.--path path
: Specifies the path of the target state updates to subscribe to.--encoding json_ietf
: Sets the encoding format to JSON with IETF standards.
Example output:
{
"timestamp": "2022-01-01T10:00:00Z",
"path": "<path>",
"update": "<update>"
}
{
"timestamp": "2022-01-01T10:00:01Z",
"path": "<path>",
"update": "<update>"
}
...
Conclusion:
The gnmic subscribe
command provides flexible options for subscribing to the state updates of gnmic network devices. Whether you need to monitor specific paths, control the update frequency, or customize the response encoding, this command allows you to efficiently manage and retrieve real-time information from your network devices.