python-netsnmpagent icon indicating copy to clipboard operation
python-netsnmpagent copied to clipboard

Add support for traps

Open j123b567 opened this issue 4 years ago • 0 comments

Inspired by #22 but done completely differently. It is not using pdu so it is not affected by incompatibility introduced in Net-SNMP v5.8.

Function parameters are different and simplified. You can just send numeric trap for SNMPv1 or OID trap for SNMPv2c and SNMPv3.

Actual version of the trap is configured via snmpd.conf.

Variables are not send by raw values but must be encapsulated in variable type. This help serializing with already defined code.

To send trap, you can reuse already registred variables


simpleUnsignedRO = agent.Unsigned32(
	oidstr   = "SIMPLE-MIB::simpleUnsignedRO",
	writable = False
)

...

agent.send_trap(
	trap='SIMPLE-MIB::simpleUnsignedROChange',
	varlist={
		'SIMPLE-MIB::simpleUnsignedRO.0': simpleUnsignedRO,
	}
)

Or send completely unrelated new value

agent.send_trap(
	trap='SIMPLE-MIB::simpleUnsignedROChange',
	varlist={
		'SIMPLE-MIB::simpleUnsignedRO.0': agent.Unsigned32(123456),
	}
)

Function read_objid is extracted, because it is used many times. This function can handle correctly numeric OID representations like ".1.3.6.1.2.1.1.3.0" so there is no need to reimplement this in python. This is the reason, why this part is heavily simplified.

j123b567 avatar Sep 09 '20 14:09 j123b567