I would like to set a value defined in the Python script to an OID. However, when I execute my script. The message appears. That this OID does not exist. Later it is intended that the data will be displayed in real time using a DHT11 sensor. First, however, I wanted to familiarize myself with SNMP and thus output the value manually.
Python File
from pysnmp.hlapi import *
def set_snmp_value(oid, value):
"""
Setzt einen SNMP-Wert auf einen bestimmten Wert.
:param oid: Die OID des SNMP-Wertes, der gesetzt werden soll.
:param value: Der Wert, der gesetzt werden soll.
"""
iterator = setCmd(
SnmpEngine(),
CommunityData('public', mpModel=0), # 'public' ist die Community-String. Passen Sie dies an Ihre Konfiguration an.
UdpTransportTarget(('localhost', 161)), # 'localhost' und 161 sind die Standard-IP und der Port des SNMP-Agents.
ContextData(),
ObjectType(ObjectIdentity(oid), Integer32(value))
)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s bei %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print('Erfolg:', varBind)
# Beispiel: Setzt den Wert der OID '1.3.6.1.4.1.1.2' auf 5
set_snmp_value('1.3.6.1.4.1.1.2', 5)
MIB-File
DHT11-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises
FROM SNMPv2-SMI;
dht11MIB MODULE-IDENTITY
LAST-UPDATED "202401040000Z"
ORGANIZATION "Ihre Organisation"
CONTACT-INFO "Ihre Kontaktinformationen"
DESCRIPTION
"Eine MIB-Modul zur Darstellung von DHT11 Sensorwerten."
::= { enterprises 1 }
-- Hier definieren wir ein neues Objekt für den Sensorwert
dht11SensorValue OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Der Wert des DHT11-Sensors, der vom Python-Skript bereitgestellt wird."
::= { dht11MIB 2 }
END
SNMP Config
###########################################################################
#
# snmpd.conf
# An example configuration file for configuring the Net-SNMP agent ('snmpd')
# See snmpd.conf(5) man page for details
#
###########################################################################
# SECTION: System Information Setup
#
# syslocation: The [typically physical] location of the system.
# Note that setting this value here means that when trying to
# perform an snmp SET operation to the sysLocation.0 variable will make
# the agent return the "notWritable" error code. IE, including
# this token in the snmpd.conf file will disable write access to
# the variable.
# arguments: location_string
sysLocation Sitting on the Dock of the Bay
sysContact Me <[email protected]>
# sysservices: The proper value for the sysServices object.
# arguments: sysservices_number
sysServices 72
pass .1.3.6.1.4.1.1.2 /usr/bin/python /home/pi/Temperatur/test.py
###########################################################################
# SECTION: Agent Operating Mode
#
# This section defines how the agent will operate when it
# is running.
# master: Should the agent operate as a master agent or not.
# Currently, the only supported master agent type for this token
# is "agentx".
#
# arguments: (on|yes|agentx|all|off|no)
master agentx
# agentaddress: The IP address and port number that the agent will listen on.
# By default the agent listens to any and all traffic from any
# interface on the default SNMP port (161). This allows you to
# specify which address, interface, transport type and port(s) that you
# want the agent to listen on. Multiple definitions of this token
# are concatenated together (using ':'s).
# arguments: [transport:]port[@interface/address],...
agentaddress 0.0.0.0
###########################################################################
# SECTION: Access Control Setup
#
# This section defines who is allowed to talk to your running
# snmp agent.
# Views
# arguments viewname included [oid]
# system + hrSystem groups only
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1
# rocommunity: a SNMPv1/SNMPv2c read-only access community name
# arguments: community [default|hostname|network/bits] [oid | -V view]
# Read-only access to everyone to the systemonly view
rocommunity public default -V systemonly
rocommunity6 public default -V systemonly
# SNMPv3 doesn't use communities, but users with (optionally) an
# authentication and encryption string. This user needs to be created
# with what they can view with rouser/rwuser lines in this file.
#
# createUser username (MD5|SHA|SHA-512|SHA-384|SHA-256|SHA-224) authpassphrase [DES|AES] [privpassphrase]
# e.g.
# createuser authPrivUser SHA-512 myauthphrase AES myprivphrase
#
# This should be put into /var/lib/snmp/snmpd.conf
#
# rouser: a SNMPv3 read-only access username
# arguments: username [noauth|auth|priv [OID | -V VIEW [CONTEXT]]]
rouser authPrivUser authpriv -V systemonly
I integrated the MIB file into the SnmpB tool from another device and the folder structure was displayed