snmp API reference
An SNMP v1 / v2c client and agent (RFC 1157 / RFC 3416), over UDP with community-string authentication. The client queries an agent with GET / GETNEXT / SET / walk; the agent (server) answers those for a MIB you supply - a hardware simulator, or a way to expose an app's metrics over SNMP. The wire messages are ASN.1 BER, built and parsed with the asn1 library; the transport is net UDP. No SNMPv3 / USM (that is the security model a later tier would add).
A failure throws an snmp.SnmpError (kind "snmp"); an agent that answers with an error carries its errorStatus (2 noSuchName, 6 noAccess, ...), its name and the errorIndex of the varbind at fault. An agent that does not answer within the timeout on any attempt is the generic kind "timeout" (an Error). A socket failure (an unresolvable address, a bind failure) is a net.NetError (kind "net").
Import with import "snmp.j" as snmp;. See the snmp guide for prose and examples.
Contents
| Name | What it is |
|---|---|
agent | Build an agent (server) that answers GET / GETNEXT / SET for a MIB. |
client | Construct a client for an agent, using UDP port 161, SNMP v2c, a 2s timeout, and one retry. |
clientWith | Construct a client with full control over the endpoint and timing. |
get | GET the values of one or more OIDs. |
getNext | GETNEXT: fetch the binding lexically following each OID (the walk primitive). |
intVar | Build an integer-valued binding for snmp.set. |
oidVar | Build an OID-valued binding for snmp.set. |
serve | Bind address ("host:port", e.g. |
serveOn | Serve requests on an already-bound UDP socket until a shutdown is signalled. |
set | SET one or more bindings (build them with intVar / stringVar / oidVar). |
stringVar | Build an octet-string-valued binding for snmp.set. |
varbind | Build a binding of any SNMP type (for a SET, or a MIB entry served by an agent). |
walk | Walk a subtree: repeated GETNEXT from rootOid until the returned OID leaves the subtree or the agent signal... |
Agent | struct: An SNMP agent (server): a community, a protocol version, and a MIB - the set of bindings it answers for. |
Client | struct: A configured SNMP agent endpoint. |
SnmpError | struct: A failed SNMP operation. |
Varbind | struct: One variable binding: an OID and its value. |
| constants | the module's constants |
Functions
agent
snmp.agent(community as string, version as int, bindings as list of Varbind)Build an agent (server) that answers GET / GETNEXT / SET for a MIB. Build the bindings with varbind / intVar / stringVar / oidVar; each is one OID the agent serves.
community- the community string the agent accepts
version- snmp.VERSION1 or snmp.VERSION2C
bindings- the MIB
- Returns
Agent - the configured agent
- Throws
SnmpError - kind "snmp" for a binding of an unsigned type whose number is out of range
client
snmp.client(host as string, community as string)Construct a client for an agent, using UDP port 161, SNMP v2c, a 2s timeout, and one retry.
host- the agent host or IP
community- the community string
- Returns
Client - the configured client
clientWith
snmp.clientWith(
address as string,
community as string,
version as int,
timeoutMs as int,
retries as int)Construct a client with full control over the endpoint and timing.
address- the agent as "host:port"
community- the community string
version- snmp.VERSION1 or snmp.VERSION2C
timeoutMs- the per-attempt receive timeout, in milliseconds
retries- the number of extra attempts after the first
- Returns
Client - the configured client
get
snmp.get(c as Client, oids as list of string)GET the values of one or more OIDs.
c- the agent client
oids- the OIDs to fetch
- Returns
list of Varbind - the returned bindings
- Throws
SnmpError - kind "snmp" if the agent reports an error; an
Errorof kind "timeout" if it does not answer
getNext
snmp.getNext(c as Client, oids as list of string)GETNEXT: fetch the binding lexically following each OID (the walk primitive).
c- the agent client
oids- the starting OIDs
- Returns
list of Varbind - the returned bindings
- Throws
SnmpError - kind "snmp" if the agent reports an error; an
Errorof kind "timeout" if it does not answer
intVar
snmp.intVar(oid as string, n as int)Build an integer-valued binding for snmp.set.
oid- the object identifier, dotted
n- the integer value
- Returns
Varbind - the binding
oidVar
snmp.oidVar(oid as string, target as string)Build an OID-valued binding for snmp.set.
oid- the object identifier, dotted
target- the OID value, dotted
- Returns
Varbind - the binding
serve
snmp.serve(a as Agent, address as string)Bind address ("host:port", e.g. ":161") and serve requests forever. Blocks; run it as the program's main loop, or in a spawn. A request with the wrong community, or a malformed datagram, is dropped silently (as a real agent does). To serve with a shutdown control, bind the socket yourself and use serveOn.
a- the agent to serve
address- the UDP bind address
- Throws
net.NetError - kind "net" if the address cannot be bound
serveOn
snmp.serveOn(a as Agent, socket as net.UDPSocket, stop as channel of bool)Serve requests on an already-bound UDP socket until a shutdown is signalled. Useful for embedding an agent beside a client in one program (bind first, then spawn this, so there is no bind race). To stop it, channel.send($stop, true) (a capacity >= 1 channel); the loop returns within SHUTDOWN_POLL_MS. task.wait the spawned handle afterwards for a graceful join. The caller owns the socket's lifetime; a request with the wrong community or a malformed datagram is dropped silently.
a- the agent to serve
socket- a bound UDP socket
stop- a value on this channel stops the loop
set
snmp.set(c as Client, varbinds as list of Varbind)SET one or more bindings (build them with intVar / stringVar / oidVar).
c- the agent client
varbinds- the bindings to write
- Returns
list of Varbind - the agent's echoed bindings
- Throws
SnmpError - kind "snmp" if the agent reports an error; an
Errorof kind "timeout" if it does not answer
stringVar
snmp.stringVar(oid as string, s as string)Build an octet-string-valued binding for snmp.set.
oid- the object identifier, dotted
s- the string value
- Returns
Varbind - the binding
varbind
snmp.varbind(oid as string, type as string, value as string, number as int)Build a binding of any SNMP type (for a SET, or a MIB entry served by an agent). type is a value type name (see Varbind); number is used for the numeric types, value for the string / oid / ipAddress types.
oid- the object identifier, dotted
type- the SNMP value type name
value- the string / oid / ipAddress rendering
number- the integer for a numeric type
- Returns
Varbind - the binding
- Throws
SnmpError - kind "snmp" for an unsigned type's number out of range (counter32 / gauge32 / timeTicks 0..2^32-1, counter64 >= 0)
walk
snmp.walk(c as Client, rootOid as string)Walk a subtree: repeated GETNEXT from rootOid until the returned OID leaves the subtree or the agent signals the end of its MIB (endOfMibView, or on SNMPv1 a noSuchName error, RFC 1157's end-of-MIB answer).
c- the agent client
rootOid- the subtree root OID, dotted
- Returns
list of Varbind - every binding under the subtree, in order
- Throws
SnmpError - kind "snmp" if the agent reports an error or does not advance; an
Errorof kind "timeout" if it does not answer
Structs
Agent
An SNMP agent (server): a community, a protocol version, and a MIB - the set of bindings it answers for. Build one with snmp.agent and serve it with snmp.serve.
def struct Agent {
community as string,
version as int,
bindings as list of Varbind
};community- the community string it accepts (others are dropped)
version- snmp.VERSION1 or snmp.VERSION2C
bindings- the MIB, one binding per OID it serves
Client
A configured SNMP agent endpoint.
def struct Client {
address as string,
community as string,
version as int,
timeoutMs as int,
retries as int
};address- the agent as "host:port"
community- the community string (the v1 / v2c credential)
version- the protocol version (snmp.VERSION1 or snmp.VERSION2C)
timeoutMs- the per-attempt receive timeout, in milliseconds
retries- the number of extra attempts after the first
SnmpError
A failed SNMP operation. Every error of kind "snmp" is one, so its fields are safe to read once the kind matches.
def struct SnmpError {
kind as string,
message as string,
errorStatus as int,
statusName as string,
errorIndex as int
};kind- always
"snmp" message- what went wrong, with the error-status name when the agent sent one
errorStatus- the agent's error-status (RFC 3416: 1 tooBig, 2 noSuchName, 3 badValue, 4 readOnly, 5 genErr, 6 noAccess, ... 18 inconsistentName); 0 when the agent sent none
statusName- the error-status's name (
noSuchName, ...), "" when none or unknown errorIndex- the 1-based index of the varbind at fault; 0 when none
Varbind
One variable binding: an OID and its value. On a returned binding, type is the SNMP value type ("integer", "octetString", "oid", "null", "counter32", "gauge32", "timeTicks", "ipAddress", "counter64", "opaque", or the exception "noSuchObject" / "noSuchInstance" / "endOfMibView"); value is a string rendering, and number holds the integer for a numeric type (0 otherwise).
def struct Varbind {
oid as string,
type as string,
value as string,
number as int
};oid- the object identifier, dotted
type- the value's SNMP type name
value- the value rendered as a string
number- the integer value for a numeric type (0 otherwise)
Constants
snmp.VERSION1 as int- SNMP version 1.
snmp.VERSION2C as int- SNMP version 2c.