def send_trap(self): ntf = ntforg.NotificationOriginator() err = ntf.sendNotification( ntforg.CommunityData(self.community), ntforg.UdpTransportTarget((self.manager_ip, 162)), 'trap', self.trapname, *self.binds) return err
def error_check(error): #checks if the discovery or compilations returns with an error #if so a snmp trap is sent to yetnet and tbs zenoss if error == True: print ("- Houston we have a problem! -") ntfOrg = ntforg.NotificationOriginator() #send notification to yetnet ntfOrg.sendNotification( ntforg.CommunityData('tbsro'), ntforg.UdpTransportTarget(('10.10.10.207', 162)), 'trap', '1.3.6.1.4.1.14697.1', ('1.3.6.1.4.1.14697.1.1.1.7', rfc1902.OctetString('YETNET')), ('1.3.6.1.4.1.14697.1.1.1.8', rfc1902.OctetString('3')), ('1.3.6.1.4.1.14697.1.1.1.5', rfc1902.OctetString('////tor_auto_update.py')), ('1.3.6.1.4.1.14697.1.1.1.3', rfc1902.OctetString('')), ('1.3.6.1.4.1.14697.1.1.1.9', rfc1902.OctetString('Houston, we have a problem!')), ('1.3.6.1.4.1.14697.1.1.1.10', rfc1902.OctetString('tor_auto_update.py')) ) #send notification to tbs ntfOrg.sendNotification( ntforg.CommunityData('tbsro'), ntforg.UdpTransportTarget(('10.10.10.202', 162)), 'trap', '1.3.6.1.4.1.14697.1', ('1.3.6.1.4.1.14697.1.1.1.7', rfc1902.OctetString('TBS')), ('1.3.6.1.4.1.14697.1.1.1.8', rfc1902.OctetString('3')), ('1.3.6.1.4.1.14697.1.1.1.5', rfc1902.OctetString('////tor_auto_update.py')), ('1.3.6.1.4.1.14697.1.1.1.3', rfc1902.OctetString('')), ('1.3.6.1.4.1.14697.1.1.1.9', rfc1902.OctetString('Houston, we have a problem!')), ('1.3.6.1.4.1.14697.1.1.1.10', rfc1902.OctetString('tor_auto_update.py')) ) print("- snmp trap has been sent -") else: print("- no error - ")
def send_trap(self): """ Send a SNMP trap with id `trapid` to the IP address `manager` """ oid = self.options.eoid # cascade enterprise Object ID trapid = self.options.trapid # base string for trap indicators community = self.options.community manager_ip = self.options.manager_ip severity = self.options.severity description = self.trap_description url = self.options.trap_url alert_level = self.options.alert_level now = timeutils.datetime_to_seconds(datetime.datetime.now()) trapname = '.'.join([oid, trapid]) ntf = ntforg.NotificationOriginator() err = ntf.sendNotification(ntforg.CommunityData(community), ntforg.UdpTransportTarget((manager_ip, 162)), 'trap', trapname, ('1.3.6.1.2.1.1.3.0', rfc1902.Integer(0)), # Uptime ('1.3.6.1.4.1.7054.71.2.1.0', rfc1902.Integer(severity)), # Severity ('1.3.6.1.4.1.7054.71.2.3.0', rfc1902.OctetString(description)), ('1.3.6.1.4.1.7054.71.2.4.0', rfc1902.Integer(0)), # Event ID ('1.3.6.1.4.1.7054.71.2.5.0', rfc1902.OctetString(url)), ('1.3.6.1.4.1.7054.71.2.7.0', rfc1902.Integer(alert_level)), # Alert Level ('1.3.6.1.4.1.7054.71.2.8.0', rfc1902.Integer(now)), # Start Time ('1.3.6.1.4.1.7054.71.2.16.0', rfc1902.Integer(0)), # Source Count ('1.3.6.1.4.1.7054.71.2.18.0', rfc1902.Integer(0)), # Destination Count ('1.3.6.1.4.1.7054.71.2.20.0', rfc1902.Integer(0)), # Protocol Count ('1.3.6.1.4.1.7054.71.2.22.0', rfc1902.Integer(0)), # Port Count )
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 __init__(self, host, port = 162): self.host = host self.port = port self.authData = None #TODO: engine id customizable # engine id makes sense in v3 context, using USM # SNMPv3 with the User-Based Security Model (USM) # makes use of an EngineID identifier for the SNMPv3 application # that is authoritative (meaning the one who controls the flow of information). snmpEngineId = rfc1902.OctetString(hexValue='0000000000000000') self.ntfOrg = ntforg.NotificationOriginator(engine.SnmpEngine(snmpEngineId))
def notification_send(SITE_IP): ntfOrg = ntforg.NotificationOriginator() #ntforg.CommunityData('public', mpModel=0), errorIndication = ntfOrg.sendNotification( ntforg.CommunityData(COMMUNITY, mpModel=0), ntforg.UdpTransportTarget((NOC_SERVER, SNMP_PORT)), 'trap', '1.3.6.1.4.1.20408.4.1.1.2.0.432', ('1.3.6.1.2.1.1.3.0', 12345), ('1.3.6.1.6.3.18.1.3.0', SITE_IP), ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), ('1.3.6.1.2.1.1.1.0', "Down"), ) print "Sent trap to manager" if errorIndication: print('Notification not sent: %s' % errorIndication)
# # Notification Originator # # Send SNMP notification using the following options: # # * 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))
# -*- coding: utf-8 -*-
# # Sending SNMPv3 Notification in behalf of non-default ContextName # requires having a collection of Managed Objects registered under # the ContextName being used. # from pysnmp.entity import engine from pysnmp.entity.rfc3413 import context from pysnmp.entity.rfc3413.oneliner import ntforg snmpEngine = engine.SnmpEngine() snmpContext = context.SnmpContext(snmpEngine) # register default collection of Managed Objects under new contextName snmpContext.registerContextName('my-context', snmpContext.getMibInstrum()) ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext) errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( ntforg.UsmUserData('usr-md5-none', 'authkey1'), ntforg.UdpTransportTarget(('localhost', 162)), 'inform', '1.3.6.1.6.3.1.1.5.2', contextName='my-context') 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:
# * 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)
# Notification Originator (TRAP/INFORM) from pysnmp.entity.rfc3413.oneliner import ntforg from pysnmp.proto import rfc1902 errorIndication = ntforg.NotificationOriginator().sendNotification( # SNMP v1 # ntforg.CommunityData('test-manager', 'public', 0), # SNMP v2 # ntforg.CommunityData('test-manager', 'public'), # SNMP v3 ntforg.UsmUserData('test-user', 'authkey1', 'privkey1'), ntforg.UdpTransportTarget(('localhost', 162)), # Trap type # 'trap', 'inform', # Trap OID ( ('SNMPv2-MIB', 'coldStart'), ), # MIB symbol name, plain string value ((('SNMPv2-MIB', 'sysName'), 0), 'new name'), # Plain OID name, rfc1902 class instance value ((1, 3, 6, 1, 2, 1, 1, 5, 0), rfc1902.OctetString('new name'))) if errorIndication: print errorIndication