ich hab ein Problem mit einer SNMP Abfrage mittels pysnmp welches ich nicht ansatzweise verstehe.
Mein Script soll aktuell folgendes machen:
- MIB entPhysicalDescr mit bestimmten Strings abrufen und den Index des OID speichern. In "sensoren" steht dann folgender Inhalt:
- 21594
21595
21596
21597
21598
111021592
111021593
112021592
112021593
115021591
115021593
116021591
116021593
Code: Alles auswählen
entSensorThresholdValue=('1.3.6.1.4.1.9.9.91.1.2.1.1.4')
generator = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData('server', community, 1) # 1 means version SNMP v2c
transport = cmdgen.UdpTransportTarget((ip, 161))
real_fun = getattr(generator, 'nextCmd')
res = (errorIndication, errorStatus, errorIndex, varBinds)\
= real_fun(comm_data, transport, entPhysicalDescr)
for a in sensoren:
res = real_fun(comm_data, transport, entSensorThresholdValue + '.' + a)
Code: Alles auswählen
entSensorValue=('1.3.6.1.4.1.9.9.91.1.1.1.1.4')
generator = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData('server', community, 1) # 1 means version SNMP v2c
transport = cmdgen.UdpTransportTarget((ip, 161))
real_fun = getattr(generator, 'nextCmd')
res = (errorIndication, errorStatus, errorIndex, varBinds)\
= real_fun(comm_data, transport, entPhysicalDescr)
for a in sensoren:
res = real_fun(comm_data, transport, entSensorValue + '.' + a)
Leider klappt das nicht und ich erhalte eine leere Rückmeldung. Da beide SNMP-Walks jedoch komplett gleich aufgebaut sind, verstehe ich den Grund nicht. Die MIB ist vorhanden und liefert auf der Shell auch Inhalte zurück:
Debuggung im Python-Script hab ich bereits betrieben, die OID's werden korrekt gebildet und schauen in beiden Fällen auch gleich aus.xxxxx@XX:~$ snmpwalk -c xx-v 2c xx 1.3.6.1.4.1.9.9.91.1.2.1.1.4.21594
CISCO-ENTITY-SENSOR-MIB::entSensorThresholdValue.21594.1 = INTEGER: 70
CISCO-ENTITY-SENSOR-MIB::entSensorThresholdValue.21594.2 = INTEGER: 80
xxxxx@XX:~$ snmpwalk -c xx -v 2c xx 1.3.6.1.4.1.9.9.91.1.1.1.1.4.21594
CISCO-ENTITY-SENSOR-MIB::entSensorValue.21594 = INTEGER: 41
Nun weiß ich nicht mehr weiter.
Kann mir jemand helfen?
Vielen Dank!
Mein kompletter Code:
Code: Alles auswählen
#!/usr/bin/env python3
import os, sys, re
import socket
import random
from struct import pack, unpack
from datetime import datetime as dt
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto.rfc1902 import Integer, IpAddress, OctetString
ip='XXXXX'
community='XXXXX'
entPhysicalDescr=('1.3.6.1.2.1.47.1.1.1.1.2')
entSensorThresholdValue=('1.3.6.1.4.1.9.9.91.1.2.1.1.4')
entSensorValue=('1.3.6.1.4.1.9.9.91.1.1.1.1.4')
sensoren = []
threshold_min = []
threshold_max = []
sensor_value = []
generator = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData('server', community, 1)
transport = cmdgen.UdpTransportTarget((ip, 161))
real_fun = getattr(generator, 'nextCmd')
res = (errorIndication, errorStatus, errorIndex, varBinds)\
= real_fun(comm_data, transport, entPhysicalDescr)
if not errorIndication is None or errorStatus is True:
print "Error: %s %s %s %s" % res
else:
for a in res[3]:
if re.match(r"PowerSupply-. Sensor-.", str(a[0][1]))\
or re.match(r".*Module-. Inlet-.", str(a[0][1]))\
or re.match(r".*Module-. Die-.", str(a[0][1])):
output = str(a[0][0]).replace("1.3.6.1.2.1.47.1.1.1.1.2.", "")
sensoren.append(str(output))
for a in sensoren:
res = real_fun(comm_data, transport, entSensorThresholdValue + '.' + a)
threshold_min.append(str(res[3][1][0][0]))
threshold_max.append(str(res[3][1][0][1]))
res = real_fun(comm_data, transport, entSensorValue + '.' + a)
# sensor_value.append(str[res[3][1][0][0]))
print (res)