SNMP

  • Versionen: v1, v2c und v3

  • snmpget: Einfacher GET Request

  • snmpwalk: Multiple GETNEXT Requests, bis zum Ende des MIB

  • eine Bulk-Operation ist effizienter als das Standard-Pendant, ist aber erst ab v2c verfügbar

  • Community Name (SNMP v1 und SNMP v2c): nichts anderes als eine Art Passwort. Oft wird für Get-Funktionen „public“ und für Set-Funktionen „internal“ verwendet.

  • SNMP Agent: der Server-Part (im Switch z.B. in der Firmware realisiert, auf Linux snmpd)

  • SNMP Manager: der Client-Part

  • MIB: Kompletter Information-Tree, Ablage unter /usr/share/snmp/mibs

    • Definitionen müssen mit einem String der Form FS-MIB DEFINITIONS ::= BEGIN beginnen

    • .iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr.1 ist die ausführliche Schreibweise für IF-MIB::ifDescr.1.

    • Leerzeichen im MIB-Namen unbedingt vermeiden!

  • SMI: Structure of Management Information, Beschreibungssprache für MIBs

yum -y install net-snmp-utils

Liste aller Geräte mit SNMP-Fähigkeit ermitteln. Es interessieren nur die Zeilen, die ein „161/udp open“ zurückliefern:

nmap -sU -p 161 --script default,snmp-sysdescr 192.168.0.0/24

Anschliessend:

snmpget -v2c -c public 192.0.2.109 .1.3.6.1.2.1.1.1.0
snmpget -v2c -c public 192.0.2.109 SNMPv2-MIB::sysDescr.0

# get OIDs instead of MIB names
snmpget -v2c -c public -On 192.0.2.109 .1.3.6.1

snmpget -v2c -c public -r 0 -t 7 -OSqtU -M $HOME/.snmp/mibs:/usr/share/snmp/mibs:./snmp-mibs -m +FS-MIB 192.0.2.109 OID1 OID2

snmpwalk [APPLICATION OPTIONS] [COMMON OPTIONS] AGENT [OID]
snmpwalk -v2c -c public 192.0.2.109 system

# much more efficient:
snmpbulkwalk -v2c -c public 192.0.2.109
snmpbulkwalk -v2c -c public 192.0.2.109 system
snmpbulkwalk -v2c -c public 192.0.2.109 IF-MIB::ifDescr
snmpbulkwalk -v2c -c public 192.0.2.109 FS-MIB::fs
snmpbulkwalk -v2c -c public -OS -m +FS-MIB 192.0.2.109 FS-MIB::switchMgt

snmptable -v2c ‐Os ‐Cb ‐c public 192.0.2.109 HOST‐RESOURCES‐MIB::hrFSTable

snmpwalk -v3 -u username -a SHA -A authPassword -l authNoPriv 192.0.2.109 .iso

SNMP-Walk per v3 und authNoPriv:

# -A: password, -u: Security-Name
snmpwalk -v3 -l authNoPriv -a MD5 -A 'linuxfabrik' -u snmp-admin2 192.0.2.36:161 .iso

Sind die MIBs ok, werden sie am richtigen Ort gesucht?

snmpget -Dparse-mibs -v 2c -c public -M +/usr/lib64/nagios/plugins/snmp-mibs/switch-fs-s3900 -m +FS-MIB 192.0.2.109 sysName.0

OID ermitteln:

snmptranslate -On SNMPv2-MIB::sysDescr.0
# .1.3.6.1.2.1.1.1.0

snmptranslate -m +FS-MIB 1.3.6.1.4.1.52642.2.1.45.1.39.2.1.0
# FS-MIB::cpuCurrentUti.0

Bezugsquellen für Public MIBs:

Unterschiede in der Ausgabe bei Abfrage mit und ohne MIB:

# without MIB
snmpbulkwalk -v2c -c public -OS 192.0.2.109 1.3.6.1.4.1.52642.2.1.45.1.39.2.1.0
# result:
# SNMPv2-SMI::enterprises.52642.2.1.45.1.39.2.1.0 = INTEGER: 1

# with MIB
snmpbulkwalk -v2c -c public -m +FS-MIB -OS 192.0.2.109 1.3.6.1.4.1.52642.2.1.45.1.39.2.1.0
# result
# FS-MIB::cpuCurrentUti.0 = INTEGER: 1 %
Links

Tipp

Wer den Standort/die Location für Geräte eindeutig und konsistent beschreiben möchte, folgt am besten dem Schema Rack, Room, Building, City, Country [Lat, Lon].

OIDs

.1.3.6.1                        SNMPv2-SMI
.1.3.6.1.2.1.1.1.0              SNMPv2-MIB::sysDescr.0 - mgmt-Teilbaum (.2)
.1.3.6.1.4.1.11.2.3.9.1.1.1.0   SNMPv2-SMI::enterprises.11.2.3.9.1.1.1.0 - private-Teilbaum (.4), herstellerspezifisch

Auf Linux nützliche OIDs:

Network Interface Statistics
.1.3.6.1.2.1.2.2.1.2            List NIC names
.1.3.6.1.2.1.2.2.1.10           Get Bytes IN
.1.3.6.1.2.1.2.2.1.10.4         Get Bytes IN for NIC 4
.1.3.6.1.2.1.2.2.1.16           Get Bytes OUT
.1.3.6.1.2.1.2.2.1.16.4         Get Bytes OUT for NIC 4

Load
.1.3.6.1.4.1.2021.10.1.3.1      1 minute Load
.1.3.6.1.4.1.2021.10.1.3.2      5 minute Load
.1.3.6.1.4.1.2021.10.1.3.3      15 minute Load

CPU times
.1.3.6.1.4.1.2021.11.9.0        percentage of user CPU time
.1.3.6.1.4.1.2021.11.10.0       percentages of system CPU time
.1.3.6.1.4.1.2021.11.11.0       percentages of idle CPU time
.1.3.6.1.4.1.2021.11.50.0       raw user cpu time
.1.3.6.1.4.1.2021.11.51.0       raw nice cpu time
.1.3.6.1.4.1.2021.11.52.0       raw system cpu time
.1.3.6.1.4.1.2021.11.53.0       raw idle cpu time

Memory Statistics
.1.3.6.1.4.1.2021.4.3.0         Total Swap Size
.1.3.6.1.4.1.2021.4.4.0         Available Swap Space
.1.3.6.1.4.1.2021.4.5.0         Total RAM in machine
.1.3.6.1.4.1.2021.4.6.0         Total RAM used
.1.3.6.1.4.1.2021.4.11.0        Total RAM Free
.1.3.6.1.4.1.2021.4.13.0        Total RAM Shared
.1.3.6.1.4.1.2021.4.14.0        Total RAM Buffered
.1.3.6.1.4.1.2021.4.15.0        Total Cached Memory

Disks
.1.3.6.1.4.1.2021.9.1.2.1       Path where the disk is mounted
.1.3.6.1.4.1.2021.9.1.3.1       Path of the device for the partition
.1.3.6.1.4.1.2021.9.1.6.1       Total size of the disk/partion (kBytes)
.1.3.6.1.4.1.2021.9.1.7.1       Available space on the disk
.1.3.6.1.4.1.2021.9.1.8.1       Used space on the disk
.1.3.6.1.4.1.2021.9.1.9.1       Percentage of space used on disk
.1.3.6.1.4.1.2021.9.1.10.1      Percentage of inodes used on disk

Processes
.1.3.6.1.2.1.1.3.0              System Uptime

Datentypen in SMIv2

Typ

Art

MIB

Counter32

positiver 32-Bit-Zählerwert

SNMPv2‑SMI

Counter64

positiver 64-Bit-Zählerwert

SNMPv2‑SMI

DisplayString

ASCII-String von 0 bis 255 Zeichen Länge

SNMPv2‑TC

Gauge32

positiver 32-Bit-Messwert

SNMPv2‑SMI

Integer32

positive oder negative 32-Bit-Ganzzahl

SNMPv2‑SMI

IpAddress

IPv4-Adresse

SNMPv2‑SMI

Object Identifier

definiert einen neuen Zwei

SNMPv2‑SMI

Unsigned32

positive 32-Bit-Ganzzahl

SNMPv2‑SMI

TimeTicks

positiver 32-Bit-Zeitwert

SNMPv2‑SMI

Floats sind nicht definiert, es gibt nur Ganzzahlen. Der Unterschied zwischen Gauge und Integer: ein Integer wird beim Überlauf auf 0 gesetzt, Gauge-Werte verändern ihren Wert bei Erreichen des maximalen Wertes beim weiteren Inkrementieren nicht.

SNMP-Traps

SNMP-Traps und -Notifications werden unter CentOS mit snmptrapd empfangen. Der Daemon findet sich im net-snmp-Package.

Empfangene Nachrichten können dann mit Hilfe von SNMPTT (SNMP Trap Translator) gefiltert, umgeschrieben und per EXEC-Anweisung weitergegeben werden, z.B. per curl an ein externes REST-API. SNMPTT findet sich im EPEL-Repo.

snmpd auf Linux

yum -y install net-snmp
systemctl enable --now snmpd

Beispiel-Konfiguration:

/etc/snmp/snmpd.conf
# Map 'U8QN52xqETidcaOq4' community to the 'ConfigUser'
# Map 'mM5HtU5clIjaFnBw6' community to the 'AllUser'

##  sec.name        source          community
## ---------------------------------------------------------
com2sec ConfigUser  default         U8QN52xqETidcaOq4
com2sec AllUser     default         mM5HtU5clIjaFnBw6


# Map 'ConfigUser' to 'ConfigGroup' for SNMP Version 2c
# Map 'AllUser' to 'AllGroup' for SNMP Version 2c
#                       sec.model   sec.name

##  Access.group.name   sec.model   sec.name
## ---------------------------------------------------------
group   ConfigGroup     v2c         ConfigUser
group   AllGroup        v2c         AllUser


# Define 'SystemView', which includes everything under .1.3.6.1.2.1.1 (or .1.3.6.1.2.1.25.1)
# Define 'AllView', which includes everything under .1

##      MIB.view.name   incl/excl   MIB.subtree     mask
## ---------------------------------------------------------
view    SystemView     included    .1.3.6.1.2.1
view    SystemView     included    .1.3.6.1.2.1.25.1.1
view    AllView        included    .1


# Give 'ConfigGroup' read access to objects in the view 'SystemView'
# Give 'AllGroup' read access to objects in the view 'AllView'

## MIB.group.name context   sec.model sec.level  rite  notif    prefix read
## ------------------------------------------------------------------------
access  ConfigGroup     ""  v2c       noauth     exact SystemView none none
access  AllGroup        ""  v2c       noauth     exact AllView    none none

syslocation Rack 13, Room 00.42, Witikonerstrasse 15, 8032 Zurich, CH
syscontact Linuxfabrik <info@linuxfabrik.ch>

#OS Distribution Detection
extend distro /usr/bin/distro

#Hardware Detection
extend manufacturer '/bin/cat /sys/devices/virtual/dmi/id/sys_vendor'
extend hardware '/bin/cat /sys/devices/virtual/dmi/id/product_name'
extend serial '/bin/cat /sys/devices/virtual/dmi/id/product_serial'

Troubleshooting

add_mibdir: strings scanned in from /root/.snmp/mibs/.index are too large. count = 49

Irgendeine MIB-Datei macht Ärger (Syntax defekt), es liegt eine Datei mit Leerzeichen im Dateinamen herum, oder Bug in net-snmp. Was meistens für einen einzigen Aufruf hilft:

rm -rf /var/lib/net-snmp/mib_indexes/*
Expected „(“ (_): At line 16700 in /root/.snmp/mibs/mymib.mib, Should be ACCESS (plus): At line 16700 in /root/.snmp/mibs/mymib.mib, Bad parse of OBJECT-TYPE: At line 16700 in /root/.snmp/mibs/mymib.mib

„_“ durch „-“ ersetzen.

Built on 2023-09-21