TUT:Customized Output Formats
From Net-SNMP Wiki
Contents |
Output Formatting
All of the net-snmp commands pick a common set of options, which the next few sections will be discussing. The only commands that do not accept these common set of arguments are the running demons (like snmpd and snmptrapd).
A list of all of the options that are available for use from a command can be printed by passing "-h" on the command line to any of the commands.
Command Options Affecting Output Formatting
All of the commands allow you to format their output in a manner which you personally will find attractive. The behavior is controlled using the -O flag:
% snmpget -h
Usage: snmpget [options...] [ ...]
...
-O Toggle various defaults controlling oid printing:
OIDOPTS values:
n: Print oids numerically.
e: Print enums numerically.
E: Escape quotes in string indices.
X: Extended index format.
b: Dont break oid indexes down.
q: Quick print for easier parsing.
f: Print full oids on output.
s: Print only last symbolic element of oid.
S: Print MIB module-id plus last element.
t: Print timeticks unparsed as numeric integers.
v: Print values only (not OID = value).
Here are brief examples of all the above options in use:
-On
This flag controls whether or not you want to display the results using numerical order to textual OIDs:
% snmpgetnext -v 2c -c demopublic test.net-snmp.org sysObjectID system.sysObjectID.0 = OID: enterprises.ucdavis.ucdSnmpAgent.hpux10
% snmpgetnext -v 2c -On -c demopublic test.net-snmp.org sysObjectID .1.3.6.1.2.1.1.2.0 = OID: .1.3.6.1.4.1.2021.250.6
-Oe
Similarly, this flag controls whether you want to display enumerated lists numerically or with textual translations (this example will not work for you the:
% snmpgetnext -v 2c -c demopublic test.net-snmp.org snmpEnableAuthenTraps snmp.snmpEnableAuthenTraps.0 = disabled(2)
% snmpgetnext -v 2c -c demopublic test.net-snmp.org snmpEnableAuthenTraps snmp.snmpEnableAuthenTraps.0 = 2
-Ob
Many SNMP tables are indexed by strings. The strings are then translated into OID segments so that they can be referenced in a SNMP request. The best way to understand this is through an example. So, let's take a look at the usmUserEntry object and how it is indexed:
% snmptranslate -Td -IR usmUserEntry
.1.3.6.1.6.3.15.1.2.2.1
usmUserEntry OBJECT-TYPE
-- FROM SNMP-USER-BASED-SM-MIB
MAX-ACCESS not-accessible
STATUS current
INDEXES { usmUserEngineID, usmUserName }
DESCRIPTION "A user configured in the SNMP engine's Local
Configuration Datastore (LCD) for the User-based
Security Model.
"
::= { iso(1) org(3) dod(6) internet(1) snmpV2(6) snmpModules(3) snmpUsmMIB(15) usmMIBObjects(1) usmUser(2) usmUserTable(2) 1 }
As you can see by the above description, it is indexed by two different objects: usmUserEngineID, and usmUserName. Both of these are strings. The default for all of the commands is to breakdown the result for you to make it easier for you to decode the above indexes out of the printed result:
% snmpgetnext -v 2c -c demopublic test.net-snmp.org usmUserEntry .iso.org.dod.internet.snmpV2.snmpModules.snmpUsmMIB.usmMIBObjects.usmUser.usmUserTable.usmUserEntry.usmUserOwnPrivKeyChange."........."."MD5User" = ""
As you can see, they OID in the output above shows 2 strings in it: "........." and "MD5User". The first string is an engine ID which is full of unprintable characters, so in this case it didn't help much that the library printed it out for us. However, the user string "MD5User" got nicely printed instead. We generally think this is much easier to understand than looking at the numbers directly. However, this is available to you as an option as well:
% snmpgetnext -Ob -v 2c -c demopublic test.net-snmp.org usmUserEntry .iso.org.dod.internet.snmpV2.snmpModules.snmpUsmMIB.usmMIBObjects.usmUser.usmUserTable.usmUserEntry.usmUserOwnPrivKeyChange.9.128.0.7.229.1.169.237.235.161.7.77.68.53.85.115.101.114 = ""
Here you can see that the OID is much more complex, and does not intuitively look like it contains 2 strings to the naked eye.
Of course, all of the net-snmp commands also accept strings in the requested OID, although most shells will require you to escape the quotes with a back slash:
% snmpget -v 2c -c demopublic test.net-snmp.org usmUserOwnPrivKeyChange.9.128.0.7.229.1.169.237.235.161.\"MD5User\" .iso.org.dod.internet.snmpV2.snmpModules.snmpUsmMIB.usmMIBObjects.usmUser.usmUserTable.usmUserEntry.usmUserOwnPrivKeyChange."........."."MD5User" = ""
-OX
A more elaborate output for index values is -OX. This format is output-only - no provision has been made to accept it on input. Especially the IPv6 mibs return values that can be very difficult to read, and is very different from the ways you normally see the values involved. Consider for instance the IPV6-MIB:ipv6RouteTable
% snmptranslate -Td IPV6-MIB::ipv6RouteEntry
.1.3.6.1.2.1.55.1.11.1
ipv6RouteEntry OBJECT-TYPE
-- FROM IPV6-MIB
MAX-ACCESS not-accessible
STATUS current
INDEX { ipv6RouteDest, ipv6RoutePfxLength, ipv6RouteIndex }
DESCRIPTION "A routing entry."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) ipv6MIB(55) ipv6MIBObjects(1) ipv6RouteTable(11) 1 }
i.e. the index is an IPv6 address and two integers. So requesting
% snmpgetnext -OS host IPV6-MIB::ipv6RouteTable IPV6-MIB::ipv6RouteIfIndex.63.254.1.0.255.0.0.0.0.0.0.0.0.0.0.0.64.1 = 2
is not easily read. But how about
% snmpgetnext -OSX host IPV6-MIB::ipv6RouteTable IPV6-MIB::ipv6RouteIfIndex[3ffe:100:ff00:0:0:0:0:0][64][1] = 2
It looks a lot nicer. The format puts square brackets around each index, and uses DISPLAY-HINT information and string conversions to format each index.
-Os, -OS, and -Of
Shortening the output of those really long OIDs is also possible, using the -Os and -OS command (the -OS version prepends the url with the MIB name that the node came from):
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 ucdDemoUserList.0 system.sysUpTime.0 = Timeticks: (586910990) 67 days, 22:18:29.90 enterprises.ucdavis.ucdDemoMIB.ucdDemoMIBObjects.ucdDemoPublic.ucdDemoUserList.0 = " noAuthUser MD5User MD5DESUser SHAUser SHADESUser"
% snmpget -Os -v 2c -c demopublic test.net-snmp.org sysUpTime.0 ucdDemoUserList.0 sysUpTime.0 = Timeticks: (586910990) 67 days, 22:18:29.90 ucdDemoUserList.0 = " noAuthUser MD5User MD5DESUser SHAUser SHADESUser"
% snmpget -OS -v 2c -c demopublic test.net-snmp.org sysUpTime.0 ucdDemoUserList.0 RFC1213-MIB::sysUpTime.0 = Timeticks: (586911833) 67 days, 22:18:38.33 UCD-DEMO-MIB::ucdDemoUserList.0 = " noAuthUser MD5User MD5DESUser SHAUser SHADESUser"
As you can see the above options only print out the last node in the OID, but the -OS option additionally prepends the output with the name of the MIB that the OID can be found in. -Of Is sort of the reverse of the previous options and always displays the full OID:
% snmpget -Of -v 2c -c demopublic test.net-snmp.org sysUpTime.0 ucdDemoUserList.0 .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.0 = Timeticks: (741998862) 85 days, 21:06:28.62 .iso.org.dod.internet.private.enterprises.ucdavis.ucdDemoMIB.ucdDemoMIBObjects.ucdDemoPublic.ucdDemoUserList.0 = " noAuthUser MD5User MD5DESUser SHAUser SHADESUser"
-Ov
The -Ov flag outputs only the value, but not variable name
% snmpget -Ov -v 2c -c demopublic test.net-snmp.org sysObjectID.0 OID: enterprises.ucdavis.ucdSnmpAgent.hpux10
-Oq
Finally, the -Oq flag allows us to print everything as quick as possible in a fashion that is easily parsible by things like scripting languages:
% snmpget -Oq -v 2c -c demopublic test.net-snmp.org sysObjectID.0 system.sysObjectID.0 enterprises.ucdavis.ucdSnmpAgent.hpux10
Note that in the above output, there is no equal sign and there is no "OID:" string in the output.
Combinations
Of course, you're allowed to combine many of these options into one statement:
% snmpget -Oqs -v 2c -c demopublic test.net-snmp.org sysObjectID.0 sysObjectID.0 hpux10
For shell scripts, the -Oqv option is useful. It only returns the value of the attribute, which is useful for instance in (using sh/ksh/bash syntax)
% interfaces=`snmpget -Oqv -v 2c -c demopublic test.net-snmp.org ifNumber.0````
Tutorial Sections
- Command Line Applications
- snmptranslate: learning about the MIB tree.
- snmpget: retrieving data from a host.
- snmpgetnext: retrieving unknown indexed data.
- snmpwalk: retrieving lots of data at once!
- snmptable: displaying a table.
- snmpset: peforming write operations.
- snmpbulkget: communicates with a network entity using SNMP GETBULK request
- snmpbulkwalk: retrieve a sub-tree of management values using SNMP GETBULK requests.
- snmptrap: Sending and receiving traps, and acting upon them.
- Traps/informs with SNMPv3: Sending and receiving SNMPv3 TRAPs and INFORMs
- Common command line options:
- Using and loading MIBS
- SNMPv3 Options
- Customized Output Formats
- Writing mib2c config files
- SNMP Daemons
- SNMP Agent (snmpd) Configuration
- SNMP Notification Receiver (snmptrapd)
- Agent Monitoring
- Coding Tutorials
- Client / Manager Coding Tutorials
- Agent Coding Tutorials
- Writing a mib module to serve information described by an SNMP MIB, and how to compile it into the net-snmp snmpd agent.
- Writing a Dynamically Loadable Object that can be loaded into the SNMP agent.
- Writing a Subagent that can be run to attach to the snmpd master agent.
- Writing a perl plugin to extend the agent using the NetSNMP::agent module.
- Using mib2c to help write an agent code template for you
- Header files and autoconf
- Building With Visual Studio 2005 Express
- Debugging SNMP Applications and Agent's
