MIB Support
Query a device by name instead of by number.
Give the Manager a MibTree and every method that takes an OID will also take a MIB name. Numeric OIDs are also possible.
from snmpkit.manager import Manager
from snmpkit.mib import MibTree
tree = MibTree()
tree.load_dir("/usr/share/snmp/mibs")
async with Manager("192.0.2.1", community="public", mib=tree) as m:
print(await m.get("sysDescr.0"))
print(await m.get("IF-MIB::ifNumber.0"))
async for oid, value in m.walk("ifDescr"):
print(m.translate(oid), "=", value)
# IF-MIB::ifDescr.1 = lo
# IF-MIB::ifDescr.2 = eth0Load the tree once at startup and keep it.
What accepts a name
Every OID argument: get, get_many, get_next, get_bulk, set, set_many, walk, bulk_walk, get_table, send_trap and send_inform. Names may be bare (ifDescr), module-qualified (IF-MIB::ifDescr), or carry an instance suffix (sysUpTime.0).
A name that is not in the loaded MIBs raises ValueError.
Three helpers are available directly on the Manager:
| Method | Purpose |
|---|---|
resolve(oid) | Name to numeric OID. Numeric input passes through. |
translate(oid) | Numeric OID to MODULE::name.instance. |
format(oid, value) | Render a value per its enumeration or DISPLAY-HINT. |
SyncManager takes the same mib= argument and has the same three helpers.
Formatting what comes back
format() applies the object’s enumeration or DISPLAY-HINT.
async with Manager("192.0.2.1", community="public", mib=tree) as m:
status = await m.get("ifAdminStatus.1")
print(m.format("ifAdminStatus.1", status)) # 'up'
mac = await m.get("ifPhysAddress.1")
print(m.format("ifPhysAddress.1", mac)) # '00:1a:2b:3c:4d:5e'Naming what a walk returns
translate() names an OID and keeps its instance suffix.
async with Manager("192.0.2.1", community="public") as m:
async for oid, value in m.walk(tree["ifDescr"].oid):
print(tree.translate(oid), "=", value)
# IF-MIB::ifDescr.1 = lo
# IF-MIB::ifDescr.2 = eth0Naming a table’s columns
get_table() takes the row OID and keys each row by column number. The MIB tree
maps those numbers back to names.
entry = tree["ifEntry"]
columns = {c.numeric_oid[-1]: c for c in entry.columns}
async with Manager("192.0.2.1", community="public") as m:
rows = await m.get_table(entry.oid)
for index, row in sorted(rows.items()):
for number, value in sorted(row.items()):
column = columns.get(number)
if column is not None:
print(index, column.name, column.format(value))
# (1,) ifDescr lo
# (1,) ifAdminStatus up
# (2,) ifDescr eth0Common OIDs
If you would rather not load MIBs at all, these are the ones you are most likely to want.
System (SNMPv2-MIB)
| Name | OID |
|---|---|
| sysDescr | 1.3.6.1.2.1.1.1.0 |
| sysObjectID | 1.3.6.1.2.1.1.2.0 |
| sysUpTime | 1.3.6.1.2.1.1.3.0 |
| sysContact | 1.3.6.1.2.1.1.4.0 |
| sysName | 1.3.6.1.2.1.1.5.0 |
| sysLocation | 1.3.6.1.2.1.1.6.0 |
Interfaces (IF-MIB::ifTable)
| Name | OID (table entry) |
|---|---|
| ifIndex | 1.3.6.1.2.1.2.2.1.1 |
| ifDescr | 1.3.6.1.2.1.2.2.1.2 |
| ifType | 1.3.6.1.2.1.2.2.1.3 |
| ifMtu | 1.3.6.1.2.1.2.2.1.4 |
| ifSpeed | 1.3.6.1.2.1.2.2.1.5 |
| ifPhysAddress | 1.3.6.1.2.1.2.2.1.6 |
| ifAdminStatus | 1.3.6.1.2.1.2.2.1.7 |
| ifOperStatus | 1.3.6.1.2.1.2.2.1.8 |
| ifInOctets | 1.3.6.1.2.1.2.2.1.10 |
| ifOutOctets | 1.3.6.1.2.1.2.2.1.16 |
64-bit counters (IF-MIB::ifXTable)
| Name | OID (table entry) |
|---|---|
| ifName | 1.3.6.1.2.1.31.1.1.1.1 |
| ifHCInOctets | 1.3.6.1.2.1.31.1.1.1.6 |
| ifHCOutOctets | 1.3.6.1.2.1.31.1.1.1.10 |
| ifHighSpeed | 1.3.6.1.2.1.31.1.1.1.15 |
| ifAlias | 1.3.6.1.2.1.31.1.1.1.18 |
Next Steps
- MIB — the full MIB tree API
- Tables — querying tables
- Operations — full API reference