def send_notification(self, notify_type, notification_trap, notification_args): agent_host = self.config['snmp']['transport']['listen_address'] agent_port = self.config['snmp']['transport']['listen_port'] community = self.config['snmp']['auth']['version2']['community'] # Initialize Notification Originator notifier = ntforg.NotificationOriginator() # Build Tarp Argument List varbinds = [] for varName, val in notification_args.items(): varbinds.append((ntforg.MibVariable(*varName), val)) # Send Notification error = notifier.sendNotification(ntforg.CommunityData(community), ntforg.UdpTransportTarget((agent_host, agent_port)), notify_type, ntforg.MibVariable(*notification_trap), *varbinds) # Check if Notification was successfully sent if error: self.fail('Notification not sent: %s' % error) log.debug("Sent Trap: %s:%d %r" % (agent_host, agent_port, notification_trap)) # stupid hack for race condition time.sleep(1)
def send_trap(self, host, notification): ntfOrg = ntforg.NotificationOriginator() errorIndication = ntfOrg.sendNotification( ntforg.CommunityData(host['community']), ntforg.UdpTransportTarget( (host['hostname'], host['port'])), 'trap', ntforg.MibVariable('SNMPv2-MIB', host['oid']), (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')) if errorIndication: self.log('info', 'Notification did not sent: %s' % errorIndication)
def send_trap(self): self.generate() ntfOrg = ntforg.NotificationOriginator() ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget((self.cm.getTrapIp(), 5050)), 'trap', ntforg.MibVariable('SNMPv2-MIB', 'sysLocation'), ('1.3.6.1.6.3.1.1.5.4', v2c.OctetString(self.data)))
def send(self, mib, value, value_type='OctetString'): """ v1 snmp, public """ if not self.authData: raise ValueError('Credentials not set, use .security_XXX() methods') obj_class = getattr(rfc1902, value_type) errorIndication = self.ntfOrg.sendNotification(self.authData, ntforg.UdpTransportTarget((self.host, self.port)), #transportTarget 'trap', #notifyType ntforg.MibVariable('SNMPv2-MIB', 'snmpOutTraps'), #notificationType ((rfc1902.ObjectName(mib), obj_class(value)))) if errorIndication: raise RuntimeError('Notification not sent: %s' % errorIndication) print('Sent SNMP TRAP {} "{}" to {} {}'.format(mib, value, self.host, self.port))
def send_trap(ip_addr='127.0.0.1'): from pysnmp.entity.rfc3413.oneliner import ntforg from pysnmp.proto import rfc1902 def cbFun(sendRequestHandle, errorIndication, cbCtx): if errorIndication: print(errorIndication) else: print('INFORM %s delivered' % sendRequestHandle) ntfOrg = ntforg.AsynNotificationOriginator() ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget((ip_addr, 162)), 'inform', ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), (('1.3.6.1.2.1.1.5.0', rfc1902.OctetString('system name')), ), (cbFun, None)) ntfOrg.snmpEngine.transportDispatcher.runDispatcher()
# # * SNMPv2c # * with community name 'public' # * over IPv4/UDP # * send INFORM notification # * with TRAP ID 'coldStart' specified as a MIB symbol # * include managed object information specified as a MIB symbol # * perform response OIDs and values resolution at MIB # from pysnmp.entity.rfc3413.oneliner import ntforg ntfOrg = ntforg.NotificationOriginator() errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget(('localhost', 162)), 'inform', ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'my system'), lookupNames=True, lookupValues=True) if errorIndication: print('Notification not sent: %s' % errorIndication) elif errorStatus: print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) else: for name, val in varBinds: print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
from pysnmp.entity.rfc3413.oneliner import ntforg from pysnmp.proto import rfc1902 ntfOrg = ntforg.NotificationOriginator() ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget(('localhost', 162)), 'trap', ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')))
# # Notification Originator # # Send SNMP notification using the following options: # # * SNMPv2c # * with community name 'public' # * over IPv4/UDP # * send TRAP notification # * with TRAP ID 'coldStart' specified as a MIB symbol # * include managed object information specified as a MIB symbol # from pysnmp.entity.rfc3413.oneliner import ntforg ntfOrg = ntforg.NotificationOriginator() errorIndication = ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget(('localhost', 162)), 'trap', ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')) if errorIndication: print('Notification not sent: %s' % errorIndication)
def sendEricssonAlarm(alarm): errorIndication = ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget(('localhost', 162)), 'trap', ntforg.MibVariable('ERICSSON-ALARM-MIB', alarm['severity']), (ntforg.MibVariable( 'ERICSSON-ALARM-MIB', 'eriAlarmActiveManagedObject'), alarm['moClasses']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmActiveMajorType'), alarm['majorType']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmActiveMinorType'), alarm['minorType']), (ntforg.MibVariable( 'ERICSSON-ALARM-MIB', 'eriAlarmActiveLastSequenceNo'), alarm['lastSequenceNumber']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmActiveEventType'), alarm['eventType']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmActiveEventTime'), alarm['eventTime']), (ntforg.MibVariable( 'ERICSSON-ALARM-MIB', 'eriAlarmActiveProbableCause'), alarm['probableCause']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmActiveSpecificProblem'), alarm['specificProblem']), (ntforg.MibVariable( 'ERICSSON-ALARM-MIB', 'eriAlarmNObjAdditionalText'), alarm['additionalText']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmNObjMoreAdditionalText'), 'false'), (ntforg.MibVariable( 'ERICSSON-ALARM-MIB', 'eriAlarmActiveProbableCause'), alarm['probableCause']), (ntforg.MibVariable('ERICSSON-ALARM-MIB', 'eriAlarmNObjResourceId'), alarm['resourceId']))
from pysnmp.entity.rfc3413.oneliner import ntforg from pysnmp.proto import rfc1902 ntfOrg = ntforg.NotificationOriginator() # Using # SNMPv2c # over IPv4/UDP # send TRAP notification # with TRAP ID 'coldStart' specified as a MIB symbol # include managed object information specified as a MIB symbol errorIndication = ntfOrg.sendNotification( ntforg.CommunityData('public'), ntforg.UdpTransportTarget(('localhost', 162)), 'trap', ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), # MIB symbol: (mib-name, mib-symbol, instance-id), new-value (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')) if errorIndication: print('Notification not sent: %s' % errorIndication) # Using # SNMPv1 # over IPv4/UDP # send TRAP notification # with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432 # with Uptime value 12345 # with Agent Address '127.0.0.1' # with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2 # include managed object information '1.3.6.1.2.1.1.1.0' = 'my system'
# * with user 'usr-sha-aes128', auth: SHA, priv: AES128 # * over IPv4/UDP # * send TRAP notification # * with TRAP ID 'authenticationFailure' specified as a MIB symbol # * do not include any additional managed object information # # SNMPv3 TRAPs requires pre-sharing the Notification Originator's # value of SnmpEngineId with Notification Receiver. To facilitate that # we will use static (e.g. not autogenerated) version of snmpEngineId. # from pysnmp.entity import engine from pysnmp.entity.rfc3413.oneliner import ntforg from pysnmp.proto import rfc1902 # This SNMP Engine ID value should also be configured to TRAP receiver. snmpEngineId = rfc1902.OctetString(hexValue='8000000001020304') ntfOrg = ntforg.NotificationOriginator(engine.SnmpEngine(snmpEngineId)) errorIndication = ntfOrg.sendNotification( ntforg.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', authProtocol=ntforg.usmHMACSHAAuthProtocol, privProtocol=ntforg.usmAesCfb128Protocol), ntforg.UdpTransportTarget(('127.0.0.1', 162)), 'trap', ntforg.MibVariable('SNMPv2-MIB', 'authenticationFailure')) if errorIndication: print('Notification not sent: %s' % errorIndication)