Table of Contents
netconf xpath filter example for get command is the topic of this section.
In the previous section, we used xpath filter for the netconf get-config command, which is used to get the configuration of network devices.
However, the netconf get command is mainly used to get the statistics of network devices.
With xpath filter it is also possible to filter the statistics in order to show only a certain section in the output.
NETCONF Xpath Filter for get Command
If you remember, it was easy to write XPath filters with netconf get-config command, because when we get the entire running configuration, it shows all supported YANG files and the structure of each YANG file.
Then we find the interesting part of the configuration and the associated TAGs. Then the TAG is used directly in the xpath filter to limit the output of the running configuration to the interesting section.
However, with the netconf get command, you already need to know which YANG file is displaying your interesting statistics.
Then we can find the associated TAG in the structure of that YANG file.
When we find the TAG, Then it can be used in the xpath filter to restrict the statistics output to a specific section.
How YANG models keep statistics data?
The question is, how do I find the YANG file for the statistics that I’m looking for?
To get the answer to this question, you need to know how Cisco, Openconfig, and IETF, model the statistics data and how it differs from the modeling of the running configuration.
The answer of this question is displayed in this figure.
In Cisco, YANG files are different for configuration and statistics. The name of the YANG file related to statistics is the same as that of the YANG file related to configuration, but with a “-oper” at the end of the file name.
For example, if “Cisco-IOS-XE-interfaces.yang” is used for interface configuration, then “Cisco-IOS-XE-interfaces-oper.yang” is used to keep interface statistics.
If “Cisco-IOS-XE-ospf.yang” is used for OSPF configuration, then “Cisco-IOS-XE-ospf-oper.yang” is used to keep OSPF statistics.
But in IETF and Openconfig YANG data models, configuration and statistics are kept in a single file but with different xml TAG.
For example, in IETF, the XML tag “interface” is used for interface configuration and the XML tag “interface-state” is used for maintaining interface statistics and both in a single file “ietf-interfaces.yang”.
Openconfig always uses the XML tag “config” to store the configuration and the XML tag “state” to store the statistics, both in a single YANG file.
With this knowledge in mind, it is not difficult to find the right YANG model for any type of statistic.
For Cisco, we find the YANG model for the configuration and then there is another YANG model with the same name and a “-oper” at the end.
For IETF and Openconfig we use the same YANG file used for configuration. Different XML tags are used in the same file, as we have explained earlier, to store configuration and statistics.
Create Sample XM Skeleton from YANG Model
Now that we know which YANG file to use to get the statistics, how do we find the right XML tag to use in the xpath filter?
The easiest way is to create a sample XML skeleton structure from a YANG file that contains all the XML tags needed to get statistics.
A sample XML skeleton can be created using the “pyang” tool with the “-f sample-xml-skeleton” parameter and the name of YANG file.
pyang -f sample-xml-skeleton Cisco-IOS-XE-interfaces-oper.yang
To see some examples, let’s see sample xml skeleton of some famous YANG data model.
As we know, for Cisco there is a different YANG data model with the name including “-oper” at the end like “Cisco-IOS-XE-interfaces-oper.yang” and “Cisco-IOS-XE-ospf-oper.yang”.
For IETF, a separate xml tag exists to keep statistical information. Let’s check “ietf-interfaces.yang”.
The XML tag “Interface” is used to keep the configuration and “interfaces-state” XML tag is used to store statistical information.
For openconfig, a “config” xml tag is used to keep the configuration information and “state” xml tag is used to keep statistical information as we can see in “openconfig-interfaces.yang” and “openconfig-network-instance.yang”.
Find XML TAG from Sample Skeleton
Now suppose we want to check the number of bytes statistics entering the interfaces.
If we examine again the XML skeleton of the YANG data model “Cisco-IOS-XE-interfaces-oper.yang“, there is an XML tag “in-octets” that shows the number of bytes entering into interfaces.
So we use the XML tag “in-octets” as the first xpath filter to find the exact xpath filter in the next step.
NETCONF Xpath Filter Code Example
This is the script to get input byte statistics of the interfaces.
We use the task “netconf_get” from “nornir_scrapli” or “nornir_netconf” plugin to get the statistics.
In the previous section, we used “netconf_get_config” to get the configuration of network devices.
In the first step, we use “//in-octets” as the xpath filter to extract any statistics from any YANG model up to the keyword “in-octets” in YANG model hierarchical structure.
Let’s run the script and see the result.
task.run(task=netconf_get, filter_type="xpath", filter_="//in-octets")
In the output of running script, we see that the interface statistics from three YANG models are displayed, “Cisco-IOS-XE-interfaces-oper”, “openconfig-interfaces” and “ietf-nterfaces”.
To limit the output to only Cisco YANG data model, we use the exact xpath filter “/interfaces/interface/statistics/in-octets” to reach input byte statistics of different interfaces.
task.run(task=netconf_get, filter_type="xpath", filter_="/interfaces/interface/statistics/in-octets")
Now we run the script and see the result.
you can download netconf get command xpath filter from here.