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 NMS 10.0.0.5 on the left and router r1 10.0.0.1 with snmpd on the right. Arrows from the NMS to UDP port 161: GetRequest, GetNextRequest, GetBulkRequest; arrow back: Response. A separate arrow from the agent to UDP port 162 on the NMS: SNMPv2-Trap, with no answer.
The manager pulls with requests to port 161; the agent pushes traps to port 162.

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).

The OID tree from iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1). Under mib-2: system(1) with sysUpTime(3) and its instance .0, OID 1.3.6.1.2.1.1.3.0; interfaces(2) with ifTable(2), ifEntry(1) and the column ifOperStatus(8) with instances .1 and .2, OID 1.3.6.1.2.1.2.2.1.8.2 for eth1.
A scalar ends in .0; a table cell ends in its row index.
  • 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.2 is the status of interface 2 (eth1), up(1) or down(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, and sysName.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 ifOperStatus itself (…1.8, which has no value) and gets ifOperStatus.1. Then it asks for the next after that, and so on. This is a walk (the snmpwalk command). 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.

PDUSent byWhat it does
GetRequestmanagerread the exact OIDs listed
GetNextRequestmanagerread the next OID after each one listed
GetBulkRequestmanagerup to max-repetitions GETNEXTs in one request (v2c and v3)
SetRequestmanagerwrite a value (needs a read-write community or user)
Responseagentthe answer to any of the above, same request-id
SNMPv2-Trapagentan event, sent on its own, not acknowledged
InformRequestagent or managerlike a trap, but the receiver sends a Response
Reportagent (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):

  1. sysUpTime.0: when it happened;
  2. snmpTrapOID.0 = linkDown (1.3.6.1.6.3.1.1.5.3): which event (linkUp is …5.4);
  3. 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.

Two timelines from t = 0 to 130 s. Top, trap delivered: the link goes down at t = 75 and the trap reaches the NMS at t = 75, delay 0. Bottom, trap lost: polls at t = 60 (up) and t = 120 (down); the NMS learns at t = 120, 45 s late.
With polling alone, the delay is up to one poll interval; a trap makes it almost zero.
Polling (every 60 s)Traps
Who startsmanageragent
Detection delayup to one interval (60 s)about zero
Trafficsteady, even when nothing changesonly when something happens
Lost messagethe next poll asks againgone; nobody knows
Device deadpoll times out: you noticea 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.

v2cv3 (USM)
Who may askanyone who knows the communitynamed users, each with own keys
On the wirecommunity and values in clear textheader readable, PDU encrypted (authPriv)
Forged or changed messageacceptedrejected by the HMAC
Replayed old messageacceptedrejected outside the 150 s window
First contact1 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.

On the mind map

This page on the Networking mind map · 58 pages in 10 branches · open it to see the whole map and test yourself in recall mode.