11 February, 2014

Cisco c3750-24TS-E spec-file odm netconf

I need my CLI output in a structured format, for example in XML.
The switch needs to convert the text-block output to a structured XML format.

The available CLI commands that can produce structured XML output can be shown with 
#show format


On c3750-24TS-E, by default, there are three possible CLI commands that can produce XML output:
#show inventory
#show ip interface brief
#show running-config

The rules for generating the XML output are defined in a so-called Spec File. Every IOS device comes with a builtin Spec File with a limited amount of available CLI commands.
Spec files can be replaced with custom spec files, which use the .ODM file extension.

The .odm spec file can easily be customized by investigating the template for odm files. The template can be seen with
#show odm-format

To make the investigation easier it is possible to compare the show odm-format output with one of the built-in definitions:
#sh format cli show ip interface brief
The previous command shows the rules for parsing text-block output of show ip int brief.

To see if a CLI command's output can be shown in XML, you can issue:
#show ip interface brief | format
If the command is defined in the spec file then you are given the output in XML structure. If the command is not defined then you are given an error:
#show arp | format
%unable to find the specified cli, spec file: built-in, cli: show arp
% Incomplete command before pipe.

The odm spec files must be local to the switch. You can't use a remote spec file.


The spec-file is also used when querying for XML-structured information via netconf:
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" 
xmlns:cpi="http://www.cisco.com/cpi_10/schema">
<get>
 <filter>
<oper-data-format-xml>
<show>inventory</show>
</oper-data-format-xml>
</filter>
</get>
</rpc>]]>]]>

I haven't found an easy way to view the contents of a built-in spec file. One way is to upload an empty file to the switch (emptyfile.odm for example) and then issue the command, which overwrites the empty file with the contents of its' built-in spec file:
#spec-file install flash:/emptyfile.odm built-in

An example of the built-in spec file of a c3750-24TS-E switch:
DLS1#more flash:/emptyfile.odm
###
show inventory
<?xml version="1.0" encoding="UTF-8"?>
<ODMSpec>
<SpecVersion>built-in</SpecVersion>
<Command><Name>show inventory</Name></Command>
<DataModel>
<Container name="ShowInventory">
<Container name="NAME:" alias = "InventoryEntry" dynamic = "true">
<Property name="NAME:" alias = "ChassisName" distance = "1" length = "1" end-delimiter = "," type = "String"/>
<Property name="DESCR:" alias = "Description" distance = "1" length = "-1" type = "String"/>
<Property name="PID:" alias="PID" distance = "1" length = "5" end-delimiter = "," type = "String"/>
<Property name="VID:" alias="VID" distance = "1" length = "1" end-delimiter = "," type = "String"/>
<Property name="SN:" alias="SN" distance = "1" length = "1" end-delimiter = "," type = "String"/>
</Container>
</Container>
</DataModel>
</ODMSpec>
###
show ip interface brief
<?xml version="1.0" encoding="UTF-8"?>
<ODMSpec>
<SpecVersion>built-in</SpecVersion>
<Command><Name>show ip interface brief</Name></Command>
<DataModel>
<Container name="ShowIpInterfaceBrief">
<Table name="IPInterfaces">
<Header name="Interface" type="String" start="0" end="22"/>
<Header name="IP-Address" type="IpAddress" start="23" end="38"/>
<Header name="OK" type="String" start="39" end="42"/>
<Header name="Method" type="String" start="43" end="49"/>
<Header name="Status" type="String" start="50" end="71"/>
<Header name="Protocol" type="String" start="72" end="-1"/>
</Table>
</Container>
</DataModel>
</ODMSpec>

To build a custom ODM spec file (in this case betaspec.odm) for sh arp :
###
show arp
<?xml version="1.0" encoding="UTF-8"?>
<ODMSpec>
<SpecVersion>built-in</SpecVersion>
<Command><Name>show arp</Name></Command>
<DataModel>
<Container name="ShowArp">
<Table name="ARPtable">
<Header name="Protocol" type="String" start="0" end="9"/>
<Header name="Address" type="IpAddress" start="10" end="26"/>
<Header name="Age (min)" type="String" start="27" end="37"/>
<Header name="Hardware Addr" type="String" start="38" end="53"/>
<Header name="Type" type="String" start="54" end="60"/>
<Header name="Interface" type="String" start="61" end="-1"/>
</Table>
</Container>
</DataModel>
</ODMSpec>

To import a modified spec file:
#(config)#format global flash:/betaspec.odm
#sh format flash:/betaspec.odm validate

You can customize the shown fields as desired. In this case entire output of sh arp is parsed to XML 
(notice you don't have to specify the custom spec file if configured as global):
DLS1#show arp | format flash:/betaspec.odm
<?xml version="1.0" encoding="UTF-8"?>
  <ShowArp xmlns="ODM://flash:/betaspec.odm//show_arp">
    <SpecVersion>built-in</SpecVersion>
    <ARPtable>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.11</Address>
        <Agemin>-</Agemin>
        <HardwareAddr>0014.a905.2c41</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.12</Address>
        <Agemin>59</Agemin>
        <HardwareAddr>0017.5983.c1c1</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.1</Address>
        <Agemin>20</Agemin>
        <HardwareAddr>503d.e52b.a206</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
    </ARPtable>
  </ShowArp>

Now the custom spec file has been imported and made global.
You can now issue a netconf query using the <oper-data-format-xml> tags. For example the oper-data-format-xml works only if the command is presented in the global spec file. After the query you get a reply in structured XML.

query:
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" 
xmlns:cpi="http://www.cisco.com/cpi_10/schema">
 <get>
  <filter>
   <oper-data-format-xml>
    <show>arp</show>
   </oper-data-format-xml>
  </filter>
 </get>
</rpc>]]>]]>

response:
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply message-id="101" xmlns="urn:ietf:params:netconf:base:1.0">
<data>
<cli-config-data-block>
here was show running config for some reason. to be investigated...
</cli-config-data-block>

<xml-oper-data>
<item>
<show>arp</show>
<response>
  <ShowArp xmlns="ODM://flash:/betaspec.odm//show_arp">
    <SpecVersion>built-in</SpecVersion>
    <ARPtable>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.11</Address>
        <Agemin>-</Agemin>
        <HardwareAddr>0014.a905.2c41</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.12</Address>
        <Agemin>66</Agemin>
        <HardwareAddr>0017.5983.c1c1</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
      <entry>
        <Protocol>Internet</Protocol>
        <Address>10.1.1.1</Address>
        <Agemin>27</Agemin>
        <HardwareAddr>503d.e52b.a206</HardwareAddr>
        <Type>ARPA</Type>
        <Interface>Vlan99</Interface>
      </entry>
    </ARPtable>
  </ShowArp>
</response>
</item>
</xml-oper-data>
</data>
</rpc-reply>]]>]]>


useful links:

3 comments:

  1. Hello thanks for your article. Do you have any idea whether there are any standard ODM files available online?

    ReplyDelete
  2. Sorry but no. I think I looked for them but couldn't find any. Unfortunately I didnt have time to write any more ODMs either. I was thinking of cooking something interesting (a monitoring solution), but didnt finish what i had started. And thank you for the kind words ;) !

    ReplyDelete
  3. Hello, thanks a lot :).

    The following works for firmware 15.x.
    show format cli show arp
    Result: odm spec file without
    ###
    show arp

    Also intresting:
    https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/xmlpi/configuration/12-2sy/xml-pi-12-2sy-book.pdf
    ODM Tool and Spec Files

    ReplyDelete