A client has requested that instead of email alerts that we send SNMP Traps to their Nagios server instead. The only thing I knew about SNMP before yesterday was that it sounded like an acronym, so please excuse (and correct me on) any misconceptions about it that I may have.
The only information that needs to be sent in the trap pertains to data about the event we are alerting our client about, which is just a couple of values pulled from our database. Needless to say these aren't in any sort of MIB, nor do they have any OIDs, and this is where I'm having trouble finding answers.
I can't figure out how I am meant to add our specific data to the trap without using MIB OIDs, which I don't have.
I'm using PySNMP to generate the request and have only incomplete code right now as I'm not sure how to go about incorporating our data into the packet.
from pysnmp.hlapi import *
def sendSNMP(destination, community_string, data):
community = CommunityData(community_string, mpModel = 0)
target = UdpTransportTarget((destination, 162))
notification_type = None
req = sendNotification(SnmpEngine(), community, target, ContextData(), 'trap', notification_type)
errorIndication, errorStatus, errorIndex, varBinds = next(req)
Any assistance is appreciated! Thanks.
On a purely technical level you could use any OID for any purpose. However, SNMP was designed to be a committee-managed protocol.
If your traps or their varbinds do not conform to standard messages/types your OIDs should begin with 1.3.6.1.4.1.YOUR_ENTERPRISE_NUMBER. If your company or your client do not have a registered Private Enterprise Number (PEN) you can request one from IANA without charge. If someone is managing your PEN you should ask them for an OID for your product domain.
The PEN list is full of individual's email addresses. There is an element of industry-level trust in this system. It is not unusual to work with someone who controls the enterprise number of a competitor. If you assume responsibility for a PEN then you assume the ethical responsibilities that go with it.
You do not have to write or publish MIBs for enterprise ranges though you may want to author them for your client's benefit.
SNMP is an old protocol. The preferred replacement is NETCONF, or so I am told.
In general, SNMP trap contents is well structured to facilitate data exchange between random systems. The contents is defined by MIB in a quite cumbersome way. However, if you are building an ad-hoc, custom system, nothing stops you from stuffing whatever OID-values into a trap.
Example code (with debugging enabled to give you a hint what is being sent out):
from pysnmp.hlapi import *
from pysnmp import debug
debug.setLogger(debug.Debug('msgproc'))
next(sendNotification(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 162)),
ContextData(),
'trap',
# sequence of custom OID-value pairs
[ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), OctetString('my string')),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'), Integer32(42))]))
来源:https://stackoverflow.com/questions/34654949/sending-snmp-traps-containing-custom-data