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): ntf = ntforg.NotificationOriginator() err = ntf.sendNotification( ntforg.CommunityData(self.community), ntforg.UdpTransportTarget((self.manager_ip, 162)), 'trap', self.trapname, *self.binds) return err
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']))
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 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)
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()
# 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)) else: for name, val in varBinds: print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
# Various uses of the Notification Originator (TRAP/INFORM) 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
('1.3.6.1.4.1.14697.1.1.1.1', rfc1902.OctetString('1')), ('1.3.6.1.4.1.14697.1.1.1.2', rfc1902.OctetString('T12601')), ('1.3.6.1.4.1.14697.1.1.1.3', rfc1902.OctetString('docsis-snr-2')), ('1.3.6.1.4.1.14697.1.1.1.4', rfc1902.OctetString('3')), ('1.3.6.1.4.1.14697.1.1.1.5', rfc1902.OctetString('/Network/UBR01SHR/Docsis_Upstream/Cable6_2_4_upstream0/SNR')), ('1.3.6.1.4.1.14697.1.1.1.6', rfc1902.OctetString('2014-03-15T17:05:00.000-01:00')), ('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('4')), ('1.3.6.1.4.1.14697.1.1.1.9', rfc1902.OctetString('Signal/Noise-Ratio lower than 19dB')), ('1.3.6.1.4.1.14697.1.1.1.10', rfc1902.OctetString('Graenichen Ost INT, US A')) ) """ """ #sending BGP Notification ntfOrg.sendNotification( ntforg.CommunityData('tbsro'), ntforg.UdpTransportTarget(('10.10.10.202', 162)), 'trap', '1.3.6.1.4.1.9.9.41.2.0.1', ('1.3.6.1.4.1.9.9.41.1.2.3.1.5', rfc1902.OctetString('This BGP neighbor is Down')) ) """ #sending Link Down #1.3.6.1.6.3.1.1.5.3: linkDown #1.3.6.1.2.1.2.2.1.2: ifDescr ntfOrg.sendNotification( ntforg.CommunityData('tbsro'), ntforg.UdpTransportTarget( ('10.10.10.202', 162)), 'trap', '1.3.6.1.6.3.1.1.5.3', ('.1.3.6.1.2.1.2.2.1.2', rfc1902.OctetString('Gigabit')))
def security_public(self, community_string='public'): """ v1 snmp, insecure """ self.authData, = ntforg.CommunityData(community_string, mpModel=0), # authData