Difference between revisions of "TUT:snmpget"
m (Tweak layout of multiple-commands) |
|||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | The GET request is one of the basic operations of the SNMP protocol, retrieving the information associated with the specified OID from the target agent. | + | The [[GET]] request is one of the basic operations of the SNMP protocol, retrieving the information associated with the specified OID from the target agent. The [[snmpget]] application implements this protocol operation. |
== Basic Example == | == Basic Example == | ||
Line 8: | Line 8: | ||
and what information is actually required: | and what information is actually required: | ||
− | % snmpget -v | + | % snmpget -v 2c -c demopublic test.net-snmp.org SNMPv2-MIB::sysUpTime.0 |
SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77 | SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77 | ||
− | In this example, ''test.net-snmp.org'' is the host name of the agent to query, using version | + | In this example, ''test.net-snmp.org'' is the host name of the agent to query, using version 2 of the SNMP protocol, and the community string "demopublic". The OID being requested is <tt>sysUpTime.0</tt> from the MIB module <tt>SNMPv2-MIB</tt>. |
The same basic command can also be used to retrieve a single element from within a table: | The same basic command can also be used to retrieve a single element from within a table: | ||
− | % snmpget -v | + | % snmpget -v 2c -c demopublic test.net-snmp.org '''SNMPv2-MIB::sysORDescr.1''' |
SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities | SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities | ||
Line 27: | Line 27: | ||
equally well be given as: | equally well be given as: | ||
− | % snmpget -v | + | % snmpget -v 2c -c demopublic test.net-snmp.org '''sysUpTime.0''' |
SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77<BR> | SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77<BR> | ||
− | % snmpget -v | + | % snmpget -v 2c -c demopublic test.net-snmp.org '''sysORDescr.1''' |
SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities | SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities | ||
Line 40: | Line 40: | ||
== SNMP Versions and Administration == | == SNMP Versions and Administration == | ||
− | Both the original SNMPv1 and the later SNMPv2c use the clear-text "community string" as a de-facto password, to indicate whether a particular request should be authorised or not. The | + | Both the original SNMPv1 and the later SNMPv2c use the clear-text "community string" as a de-facto password, to indicate whether a particular request should be authorised or not. The SNMPv1 equivalent of the first example would be almost identical: |
− | % snmpget -v ''' | + | % snmpget -v '''1''' -c demopublic test.net-snmp.org sysUpTime.0 |
SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77 | SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77 | ||
Line 85: | Line 85: | ||
SNMPv3 reports problems in the same way as SNMPv2c. | SNMPv3 reports problems in the same way as SNMPv2c. | ||
+ | === Timeouts === | ||
+ | |||
+ | Another possible type of failure is that the request may timeout | ||
+ | without returning any information at all. | ||
+ | Assuming that the remote system is actually running an SNMP agent, | ||
+ | the most likely cause of this would be the access control settings | ||
+ | of the remote agent. | ||
+ | See the FAQ entries | ||
+ | [[FAQ:Applications 04|Why doesn't the agent respond?]] and | ||
+ | [[FAQ:Agent 23|How do I configure access control?]] for further details. | ||
== Multiple Values == | == Multiple Values == |
Latest revision as of 23:36, 30 June 2011
The GET request is one of the basic operations of the SNMP protocol, retrieving the information associated with the specified OID from the target agent. The snmpget application implements this protocol operation.
Contents
Basic Example
There are three main elements to such a request - where to retrieve this information from, the administrive information associated with the request, and what information is actually required:
% snmpget -v 2c -c demopublic test.net-snmp.org SNMPv2-MIB::sysUpTime.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77
In this example, test.net-snmp.org is the host name of the agent to query, using version 2 of the SNMP protocol, and the community string "demopublic". The OID being requested is sysUpTime.0 from the MIB module SNMPv2-MIB.
The same basic command can also be used to retrieve a single element from within a table:
% snmpget -v 2c -c demopublic test.net-snmp.org SNMPv2-MIB::sysORDescr.1 SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities
The snmptranslate tutorial described several ways to specify
an OID, and most of that discussion applies here too (as well as most of the other
Net-SNMP command-line tools). The one significant difference is that snmpget
(and most of the other tools) will apply "random-lookup" by default, so it is not
strictly necessary to specify the name of the MIB. The two commands above could
equally well be given as:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77
% snmpget -v 2c -c demopublic test.net-snmp.org sysORDescr.1 SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities
and this form will be used throughout the rest of these tutorials.
Similarly, the options discussed in the Customized Output Formats tutorial can be used to control how the results should be displayed.
SNMP Versions and Administration
Both the original SNMPv1 and the later SNMPv2c use the clear-text "community string" as a de-facto password, to indicate whether a particular request should be authorised or not. The SNMPv1 equivalent of the first example would be almost identical:
% snmpget -v 1 -c demopublic test.net-snmp.org sysUpTime.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77
SNMPv3 uses a significantly different authentication mechanism, typically based around usernames and passwords, and allows both proper validation of SNMP requests, and even encryption of the traffic between the management client and the SNMP agent. See the SNMPv3 Options tutorial for details.
The default version used will depend on how the software was configured when it was first compiled. Typically, the Net-SNMP suite will probably use SNMPv3 by default, but it is safest to always specify the version explicitly.
Failed Requests
The examples above show the successful retrieval of information from the target system. But what about requests that are not successful? How does SNMP handle failed requests?
A common mistake when using the snmpget command is to forget the index (or "instance subidentifier") of the data being requested. This is less likely when retrieving a value from within a table, where it is natural to include the index as part of the OID. But for scalar objects, there is only one value, so it doesn't seem necessary to specify an index - surely the MIB object name alone should be sufficient?
However SNMP is consistent in requiring an instance for all MIB objects - even scalar objects. In this case, the instance subidentifier is always a simple .0 (zero), as shown in the first example above.
Omitting this results in an error:
% snmpget -v 1 -c demopublic test.net-snmp.org sysUpTime Error in packet Reason: (noSuchName) There is no such variable name in this MIB. This name doesn't exist: sysUpTime
Note that SNMPv2c gives a slightly more informative message:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime SNMPv2-MIB::sysUpTime = No Such Instance currently exists
The other likely cause of failure is that the agent does not support the requested MIB object at all. With SNMPv1, the error is exactly the same ("noSuchName"). SNMPv2c uses a slightly different indication of this situation:
% snmpget -v 2c -c demopublic test.net-snmp.org .1.3.6.1.2.1.1.99.0 SNMPv2-MIB::system.99.0 = No Such Object available on this agent at this OID
SNMPv3 reports problems in the same way as SNMPv2c.
Timeouts
Another possible type of failure is that the request may timeout without returning any information at all. Assuming that the remote system is actually running an SNMP agent, the most likely cause of this would be the access control settings of the remote agent. See the FAQ entries Why doesn't the agent respond? and How do I configure access control? for further details.
Multiple Values
All the examples so far have worked with a single value. But snmpget can also retrieve several MIB values in a single request:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 sysLocation.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586903243) 67 days, 22:17:12.43 SNMPv2-MIB::sysLocation.0 = UCDavis
This works in the same way for all versions SNMP. The difference between SNMPv1 and the later versions becomes apparent when one of the OIDs being requested is not valid. SNMPv2c (and SNMPv3) will display what information they can:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 sysLocation # No instance .0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586903243) 67 days, 22:17:12.43 SNMPv2-MIB::sysLocation = No Such Instance currently exists
while the equivalent SNMPv1 request will simply fail:
% snmpget -Cf -v 1 -c demopublic test.net-snmp.org sysUpTime.0 sysLocation Error in packet Reason: (noSuchName) There is no such variable name in this MIB. This name doesn't exist: sysLocation
(Note that the -Cf flag is needed to prevent snmpget from automatically correcting this problem, and retrying the request - thus defeating the point of this example!)
Tutorial Sections
About the SNMP Protocol
These tutorial links talk about SNMP generically and how the protocol itself works. They are good introductory reading material and the concepts are important to understand before diving into the later tutorials about Net-SNMP itself.
- How SNMP Works: About the protocol itself (GETs, GETNEXTs, etc)
- What data is in SNMP: All about SNMP Management Information Bases (MIBs)
- Securing SNMP: How to use the SNMP protocol securely
Net-SNMP Command Line Applications
These tutorial pages discuss the command line tools provided in the Net-SNMP suite of tools. Nearly all the example commands in these tutorials works if you try it yourself, as they're all examples that talk to our online Net-SNMP test agent. Given them a shot!
- 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/USM: Sending and receiving SNMPv3/USM TRAPs and INFORMs
- Sending Traps/Informs via AgentX: Sending notifications from the command line through snmpd
- Common command line options:
- Writing mib2c config files
Application Configuration
All of our applications support configuration to allow you to customize how they behave.
Net-SNMP Daemons
Net-SNMP comes with two long-running daemons: a SNMP agent (snmpd) for responding to management requests and a notification receiver (snmptrapd) for receiving SNMP notifications.
- SNMP Agent (snmpd) Configuration
- SNMP Notification Receiver (snmptrapd)
- Agent Monitoring
Coding Tutorials
Net-SNMP comes with a highly flexible and extensible API. The API allows you to create your own commands, add extensions to the agent to support your own MIBs and perform specialized processing of notifications.
- Client / Manager Coding Tutorials
- Agent Coding Tutorials
- The Agent Architecture page might be worth reading before or after the agent coding tutorials, and describes how the Agent Helpers work under the hood.
- 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.
- Writing shell scripts to extend the agent
- Using mib2c to help write an agent code template for you
- Header files and autoconf
Debugging SNMP Applications and Agents
All our tools and applications have extensive debugging output. These tutorials talk about how the debugging system works and how you can add your own debugging statements to you code:
- Debugging output printed using the -D command line option
- Using -Ddump to display packet breakdowns
- Debugging using GDB
Operating System Specific Tutorials
- Building With Visual Studio 2005 Express
- Building Net-SNMP 64-bit with Visual C++ 2010 Express
- Net-Snmp on Ubuntu
- Net-SNMP and lm-sensors on Ubuntu 10.04
- Net-SNMP for windows: