The idea: ask the devices, and let them call you
A network has routers, switches, servers and printers. An operator wants to know, for each of them: is it up,
which links are down, how busy is it? The Simple Network Management Protocol (SNMP) answers
this with two roles. A manager (the network management station, NMS) asks questions. An
agent, a small program on every device (on Linux: snmpd), answers them. Requests go
to UDP port 161 on the agent. When something happens, the agent can also send a
trap on its own, to UDP port 162 on the manager.
The page has one manager, 10.0.0.5, and one agent, the router r1 at
10.0.0.1 with two interfaces, eth0 and eth1. The clock is in seconds and
only moves when you press Advance Clock or when the manager waits for an answer. Messages take no time.
SNMP runs over UDP: no connection to set up, and nothing is resent unless the manager does it
(see TCP vs UDP).
The MIB is a tree of numbers
What an agent can report is described in a MIB (Management Information Base). The MIB is a
tree. Every node has a number, and the path of numbers from the root is the node's OID (object
identifier). The standard objects for any IP device live under mib-2 =
1.3.6.1.2.1 (iso.org.dod.internet.mgmt.mib-2).
- A scalar has one value, the instance
.0:sysUpTime.0=1.3.6.1.2.1.1.3.0, the time since the agent started in hundredths of a second (Timeticks). - A table has one value per row.
ifTable(1.3.6.1.2.1.2.2) has one row per interface. A cell's OID is the column's OID plus the row index:ifOperStatus.2=1.3.6.1.2.1.2.2.1.8.2is the status of interface 2 (eth1),up(1)ordown(2).
The canvas shows 14 values of the agent's MIB, from three MIB modules: SNMPv2-MIB (system),
IF-MIB (interfaces) and IP-MIB (ip). They are listed in lexicographic
order: compare OIDs number by number, like words in a dictionary.
GET and GETNEXT: walking the tree
Every request is a PDU (protocol data unit) with a request-id and a list of varbinds (variable bindings: OID = value pairs). In a request the values are empty (NULL). The agent's Response carries the same request-id, so the manager can match it to its question.
- GetRequest reads exact OIDs. One request may ask for several.
(Demo 1: GET two values:
sysUpTime.0= 8640000, one day, andsysName.0= "r1", in one round trip.) - GetNextRequest returns the first OID after the one asked for, with its value. The
manager doesn't need to know which rows exist: it asks for the column
ifOperStatusitself (…1.8, which has no value) and getsifOperStatus.1. Then it asks for the next after that, and so on. This is a walk (thesnmpwalkcommand). It stops when the answer is outside the subtree it started from.
Demo 2: Walk a column with GETNEXT takes 3 round trips for 2 values: ifOperStatus.1,
ifOperStatus.2, then ipForwarding.0, which is past the column, so the walk ends. A
walk of a whole table goes column by column, because that is the lexicographic order: all
ifIndex cells, then all ifDescr, and so on. Past the last OID the agent has, GETNEXT
returns endOfMibView. A GET of an OID it doesn't have returns noSuchObject or
noSuchInstance.
GETBULK: many GETNEXTs in one trip
A walk needs one round trip per value. On a switch with 48 ports and 20 columns that is almost a thousand round trips. SNMPv2 added GetBulkRequest: "do GETNEXT up to max-repetitions times, each starting where the last one ended, and send me all the answers at once". non-repeaters says how many of the first OIDs get only one GETNEXT (here 0).
Demo 3: GETBULK the whole ifTable asks once with max-repetitions 10. The Response has 10 varbinds: the
8 cells of ifTable in column order, then ipForwarding.0 and ipDefaultTTL.0,
which are past the table. A GETNEXT walk of the same table takes 9 round trips. The price: the agent may return
more than you need (the 2 grey lines), and a Response must fit in one UDP datagram, so the manager picks
max-repetitions with care.
| PDU | Sent by | What it does |
|---|---|---|
| GetRequest | manager | read the exact OIDs listed |
| GetNextRequest | manager | read the next OID after each one listed |
| GetBulkRequest | manager | up to max-repetitions GETNEXTs in one request (v2c and v3) |
| SetRequest | manager | write a value (needs a read-write community or user) |
| Response | agent | the answer to any of the above, same request-id |
| SNMPv2-Trap | agent | an event, sent on its own, not acknowledged |
| InformRequest | agent or manager | like a trap, but the receiver sends a Response |
| Report | agent (v3) | an error in the security layer, e.g. unknown engine |
Traps: linkDown
Polling answers "what is the state now?". For "tell me when it changes", the agent sends a trap. When
eth1 loses its cable, ifOperStatus.2 becomes down(2) and the agent sends an
SNMPv2-Trap to the manager's UDP port 162. Its varbinds are fixed by the standard (RFC 3416 and
IF-MIB, RFC 2863):
sysUpTime.0: when it happened;snmpTrapOID.0=linkDown(1.3.6.1.6.3.1.1.5.3): which event (linkUpis…5.4);ifIndex.2,ifAdminStatus.2,ifOperStatus.2: which interface, and its state.
The trap receiver (snmptrapd) decodes it and the NMS raises an alarm in the same second
(Demo 4: Cable unplugged).
Polling or traps?
A trap is one UDP datagram that nobody acknowledges. If it is lost, or the link that died was the path to the NMS, the manager never hears about it. So real systems do both: traps for speed, polls as a safety net.
| Polling (every 60 s) | Traps | |
|---|---|---|
| Who starts | manager | agent |
| Detection delay | up to one interval (60 s) | about zero |
| Traffic | steady, even when nothing changes | only when something happens |
| Lost message | the next poll asks again | gone; nobody knows |
| Device dead | poll times out: you notice | a dead device sends nothing |
Demo 5: Trap lost: the poll at t = 60 s sees both links up; the cable is pulled at t = 75 s and the
trap is dropped; the poll at t = 120 s finds down(2). The NMS knows 45 s late. A shorter interval
cuts the delay but costs traffic and agent CPU on every device. An InformRequest is a trap that
the receiver acknowledges, so the sender can resend it.
v2c community vs v3 users
SNMPv1 and v2c protect the agent with a community string, a shared word like
public, sent in clear text in every message. Anyone who can see one packet can read
it. An agent that gets a wrong community answers nothing at all, and counts it in
snmpInBadCommunityNames. The manager can't tell this from a dead device: it times out
(Demo 6: Wrong community: 1 s, a retry with the same request-id, 1 s, then
Timeout: No Response from 10.0.0.1).
SNMPv3 replaces the community with the User-based Security Model (USM, RFC 3414). Each user has keys made from a password and the agent's snmpEngineID:
- auth: an HMAC (here SHA-256) over the whole message, so a changed or forged message is rejected;
- priv: the PDU is encrypted (here AES-128), so OIDs and values can't be read on the wire;
- timeliness: each message carries the agent's snmpEngineBoots and snmpEngineTime; one more than 150 s off is rejected, so an old message can't be replayed later.
Because the keys depend on the engineID, the first contact is a discovery: an empty,
unauthenticated request, answered by a Report PDU (usmStatsUnknownEngineIDs) that
carries the engineID, boots and time. The manager caches them; later requests go straight to the real thing
(Demo 7: SNMPv3 authPriv). How HMAC and AES work inside is on
AES and SHA-256: Inside the Rounds.
| v2c | v3 (USM) | |
|---|---|---|
| Who may ask | anyone who knows the community | named users, each with own keys |
| On the wire | community and values in clear text | header readable, PDU encrypted (authPriv) |
| Forged or changed message | accepted | rejected by the HMAC |
| Replayed old message | accepted | rejected outside the 150 s window |
| First contact | 1 round trip | +1 for discovery (Report) |
See also How a Device Gets an IP Address and How a DNS Name Is Resolved, two other UDP services every network runs; NTP, which keeps the clocks that timestamp traps right; and Network Protocol Layers for where UDP sits under SNMP.
What the page leaves out
SetRequest and write access (VACM views and groups), SNMPv1 traps and their different layout, InformRequest
retries, the BER encoding of messages (ASN.1 types, lengths), the many other MIB-II groups and the 64-bit
counters of ifXTable, counter wrap and rate calculation, real walks that run past
ip into the next group instead of ending, more than one agent, trap filtering and
notificationLog, the v3 time-window check with a drifted clock, and newer tools that stream
telemetry (gNMI, NETCONF / YANG) instead of polling.