def cfgNtfOrg(self, authData, transportTarget, notifyType, tagList): addrName, paramsName = self.cfgCmdGen( authData, transportTarget, tagList ) k = paramsName, tagList, notifyType if self.__knownNotifyNames.has_key(k): notifyName, _ = self.__knownNotifyNames[k] else: notifyName = 'n%s' % cmdgen.nextID() config.addNotificationTarget( self.snmpEngine, notifyName, paramsName, tagList, notifyType ) self.__knownNotifyNames[k] = notifyName, paramsName if not self.__knownAuths.has_key(authData): subTree = (1,3,6) config.addTrapUser( self.snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, subTree ) self.__knownAuths[authData] = subTree if self.snmpContext is None: self.snmpContext = context.SnmpContext(self.snmpEngine) config.addContext( self.snmpEngine, '' # this is leaky ) return notifyName
def cfgNtfOrg(self, snmpEngine, authData, transportTarget, notifyType): cache = self._getNtfCache(snmpEngine) addrName, paramsName = self.cfgCmdGen(snmpEngine, authData, transportTarget) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: notifyNameKey = paramsName, tag, notifyType if notifyNameKey in cache['name']: notifyName, paramsName, useCount = cache['name'][notifyNameKey] cache['name'][ notifyNameKey] = notifyName, paramsName, useCount + 1 else: notifyName = 'n%s' % nextID() config.addNotificationTarget(snmpEngine, notifyName, paramsName, tag, notifyType) cache['name'][notifyNameKey] = notifyName, paramsName, 1 authDataKey = authData.securityName, authData.securityModel if authDataKey in cache['auth']: authDataX, subTree, useCount = cache['auth'][authDataKey] cache['auth'][authDataKey] = authDataX, subTree, useCount + 1 else: subTree = (1, 3, 6) config.addTrapUser(snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, subTree) cache['auth'][authDataKey] = authData, subTree, 1 return notifyName
def cfgNtfOrg(self, authData, transportTarget, notifyType): addrName, paramsName = self.cfgCmdGen(authData, transportTarget) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: k = paramsName, tag, notifyType if k in self.__knownNotifyNames: notifyName, _ = self.__knownNotifyNames[k] else: notifyName = 'n%s' % nextID() config.addNotificationTarget(self.snmpEngine, notifyName, paramsName, tag, notifyType) self.__knownNotifyNames[k] = notifyName, paramsName k = (authData.securityModel, authData.securityName, authData.securityLevel) if k not in self.__knownAuths: subTree = (1, 3, 6) config.addTrapUser(self.snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, subTree) self.__knownAuths[k] = subTree if self.snmpContext is None: self.snmpContext = context.SnmpContext(self.snmpEngine) config.addContext( self.snmpEngine, '' # this is leaky ) return notifyName
def configure(self, snmpEngine, authData, transportTarget, notifyType, contextName, **options): cache = self._getCache(snmpEngine) notifyName = None # Create matching transport tags if not given by user. Not good! if not transportTarget.tagList: transportTarget.tagList = str( hash((authData.securityName, transportTarget.transportAddr))) if isinstance(authData, CommunityData) and not authData.tag: authData.tag = transportTarget.tagList.split()[0] addrName, paramsName = self._cmdGenLcdCfg.configure( snmpEngine, authData, transportTarget, contextName, **options) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: notifyNameKey = paramsName, tag, notifyType if notifyNameKey in cache['name']: notifyName, paramsName, useCount = cache['name'][notifyNameKey] cache['name'][ notifyNameKey] = notifyName, paramsName, useCount + 1 else: notifyName = 'n%s' % self.nextID() config.addNotificationTarget(snmpEngine, notifyName, paramsName, tag, notifyType) cache['name'][notifyNameKey] = notifyName, paramsName, 1 authDataKey = authData.securityName, authData.securityModel, authData.securityLevel, contextName if authDataKey in cache['auth']: authDataX, subTree, useCount = cache['auth'][authDataKey] cache['auth'][authDataKey] = authDataX, subTree, useCount + 1 else: subTree = (1, 3, 6) config.addVacmUser(snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, (), (), subTree, contextName=contextName) cache['auth'][authDataKey] = authData, subTree, 1 return notifyName
def configure(self, snmpEngine, authData, transportTarget, notifyType, contextName, **options): cache = self._getCache(snmpEngine) notifyName = None # Create matching transport tags if not given by user. Not good! if not transportTarget.tagList: transportTarget.tagList = str( hash((authData.securityName, transportTarget.transportAddr)) ) if isinstance(authData, CommunityData) and not authData.tag: authData.tag = transportTarget.tagList.split()[0] addrName, paramsName = self._cmdGenLcdCfg.configure( snmpEngine, authData, transportTarget, contextName, **options) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: notifyNameKey = paramsName, tag, notifyType if notifyNameKey in cache['name']: notifyName, paramsName, useCount = cache['name'][notifyNameKey] cache['name'][notifyNameKey] = notifyName, paramsName, useCount + 1 else: notifyName = 'n%s' % self.nextID() config.addNotificationTarget( snmpEngine, notifyName, paramsName, tag, notifyType) cache['name'][notifyNameKey] = notifyName, paramsName, 1 authDataKey = authData.securityName, authData.securityModel, authData.securityLevel, contextName if authDataKey in cache['auth']: authDataX, subTree, useCount = cache['auth'][authDataKey] cache['auth'][authDataKey] = authDataX, subTree, useCount + 1 else: subTree = (1, 3, 6) config.addVacmUser( snmpEngine,authData.securityModel, authData.securityName, authData.securityLevel, (), (), subTree, contextName=contextName) cache['auth'][authDataKey] = authData, subTree, 1 return notifyName
def setTrapReceiver(self, host, community): config.addV1System(self._snmpEngine, 'nms-area', community) config.addVacmUser(self._snmpEngine, 2, 'nms-area', 'noAuthNoPriv', notifySubTree=(1, 3, 6, 1, 4, 1)) config.addTargetParams(self._snmpEngine, 'nms-creds', 'nms-area', 'noAuthNoPriv', 1) config.addTargetAddr(self._snmpEngine, 'my-nms', udp.domainName, (host, 162), 'nms-creds', tagList='all-my-managers') # set last parameter to 'notification' to have it send # informs rather than unacknowledged traps config.addNotificationTarget( self._snmpEngine, 'test-notification', 'my-filter', 'all-my-managers', 'trap')
def setTrapReceiver(self, host, community): config.addV1System(self._snmpEngine, 'nms-area', community) config.addVacmUser(self._snmpEngine, 2, 'nms-area', 'noAuthNoPriv', notifySubTree=(1, 3, 6, 1, 4, 1)) config.addTargetParams(self._snmpEngine, 'nms-creds', 'nms-area', 'noAuthNoPriv', 1) config.addTargetAddr(self._snmpEngine, 'my-nms', udp.domainName, (host, 162), 'nms-creds', tagList='all-my-managers') config.addNotificationTarget(self._snmpEngine, 'test-notification', 'my-filter', 'all-my-managers', 'trap')
def setTrapReceiver(self, host, community): """Send traps to the host using community string community """ config.addV1System(self._snmpEngine, 'nms-area', community) config.addVacmUser(self._snmpEngine, 2, 'nms-area', 'noAuthNoPriv', notifySubTree=(1,3,6,1,4,1)) config.addTargetParams(self._snmpEngine, 'nms-creds', 'nms-area', 'noAuthNoPriv', 1) config.addTargetAddr(self._snmpEngine, 'my-nms', udp.domainName, (host, 162), 'nms-creds', tagList='all-my-managers') #set last parameter to 'notification' to have it send #informs rather than unacknowledged traps config.addNotificationTarget( self._snmpEngine, 'test-notification', 'my-filter', 'all-my-managers', 'trap')
def initTarget(host='127.0.0.1', port=162, community='LIC_OSS'): #global snmpEngine, snmpContext, ntfOrg # Create SNMP engine instance snmpEngine = engine.SnmpEngine() # SecurityName <-> CommunityName mapping config.addV1System(snmpEngine, 'my-area', community) # Specify security settings per SecurityName (SNMPv2c -> 1) config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) # Setup transport endpoint and bind it with security settings yielding # a target name config.addSocketTransport( snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode() ) config.addTargetAddr( snmpEngine, 'my-nms', udp.domainName, (host, port), 'my-creds', tagList='all-my-managers' ) # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget( snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap' ) # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** # Create default SNMP context where contextEngineId == SnmpEngineId snmpContext = context.SnmpContext(snmpEngine) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator(snmpContext) return snmpEngine, ntfOrg
def cfgNtfOrg(self, authData, transportTarget, notifyType): addrName, paramsName = self.cfgCmdGen(authData, transportTarget) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: notifyNameKey = paramsName, tag, notifyType if notifyNameKey in self.__knownNotifyNames: notifyName, paramsName, useCount = self.__knownNotifyNames[notifyNameKey] self.__knownNotifyNames[notifyNameKey] = notifyName, paramsName, useCount + 1 else: notifyName = 'n%s' % nextID() config.addNotificationTarget( self.snmpEngine, notifyName, paramsName, tag, notifyType ) self.__knownNotifyNames[notifyNameKey] = notifyName, paramsName, 1 authDataKey = authData.securityName, authData.securityModel if authDataKey in self.__knownAuths: authDataX, subTree, useCount = self.__knownAuths[authDataKey] self.__knownAuths[authDataKey] = authDataX, subTree, useCount + 1 else: subTree = (1,3,6) config.addTrapUser( self.snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, subTree ) self.__knownAuths[authDataKey] = authData, subTree, 1 if self.snmpContext is None: self.snmpContext = context.SnmpContext(self.snmpEngine) config.addContext( self.snmpEngine, '' # this is leaky ) return notifyName
def cfgNtfOrg(self, snmpEngine, authData, transportTarget, notifyType): cache = self._getNtfCache(snmpEngine) addrName, paramsName = self.cfgCmdGen( snmpEngine, authData, transportTarget ) tagList = transportTarget.tagList.split() if not tagList: tagList = [''] for tag in tagList: notifyNameKey = paramsName, tag, notifyType if notifyNameKey in cache['name']: notifyName, paramsName, useCount = cache['name'][notifyNameKey] cache['name'][notifyNameKey] = notifyName, paramsName, useCount + 1 else: notifyName = 'n%s' % nextID() config.addNotificationTarget( snmpEngine, notifyName, paramsName, tag, notifyType ) cache['name'][notifyNameKey] = notifyName, paramsName, 1 authDataKey = authData.securityName, authData.securityModel if authDataKey in cache['auth']: authDataX, subTree, useCount = cache['auth'][authDataKey] cache['auth'][authDataKey] = authDataX, subTree, useCount + 1 else: subTree = (1,3,6) config.addTrapUser( snmpEngine, authData.securityModel, authData.securityName, authData.securityLevel, subTree ) cache['auth'][authDataKey] = authData, subTree, 1 return notifyName
def setTrapTypeForTag(snmpEngine, tag, kind='trap'): """Configure SNMP notification type per tag. Args: snmpEngine (object): pysnmp `SnmpEngine` class instance tag (str): SNMP tag to add to the list of tags used for issuing SNMP notifications. kind (str): SNMP notification type to use. Known values are 'trap' and 'inform'. Returns: str: Group name to refer to all tagged configuration entries at once for selecting suitable ones for sending SNMP notifications. """ targets = 'all-targets' config.addNotificationTarget( snmpEngine, targets, 'filter', tag, kind) config.addContext(snmpEngine, '') return targets
udp.domainName, udp.UdpTwistedTransport().openClientMode() ) config.addTargetAddr( snmpEngine, 'my-nms-1', udp.domainName, ('127.0.0.1', 162), 'my-creds-1', tagList='all-my-managers' ) # # Specify what kind of notification should be sent (TRAP or INFORM) # to what targets (chosen by tag) and with what credentials. # config.addNotificationTarget( snmpEngine, 'my-notification', 'my-creds', 'all-my-managers', doInform and 'inform' or 'trap' ) # # Notifications carry potentially confidential information from # the Agent. Therefore access control is to be setup allowing # NotificationOriginator access to certain portions of Agent MIB. # config.addContext(snmpEngine, '') # SNMPv1 config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # SNMPv2c config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # SNMPv3 config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (), (), (1,3,6)) config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authPriv', (), (), (1,3,6))
# UDP/IPv4 config.addSocketTransport(snmpEngine, udp.domainName, udp.UdpTwistedTransport().openClientMode()) config.addTargetAddr(snmpEngine, 'my-nms-1', udp.domainName, ('127.0.0.1', 162), 'my-creds-1', tagList='all-my-managers') # # Specify what kind of notification should be sent (TRAP or INFORM) # to what targets (chosen by tag) and with what credentials. # config.addNotificationTarget(snmpEngine, 'my-notification', 'my-creds', 'all-my-managers', doInform and 'inform' or 'trap') # # Notifications carry potentially confidential information from # the Agent. Therefore access control is to be setup allowing # NotificationOriginator access to certain portions of Agent MIB. # config.addContext(snmpEngine, '') # SNMPv1 config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6)) # SNMPv2c config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6)) # SNMPv3 config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (), (), (1, 3, 6)) config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authPriv', (), (),
udp.domainName, ('127.0.0.2', 162), 'my-creds', tagList='all-my-managers' ) # Third target config.addTargetAddr( snmpEngine, 'my-nms-3', udp.domainName, ('127.0.0.3', 162), 'my-creds', tagList='all-my-managers' ) # Specify what kind of notification should be sent (TRAP or INFORM) # to what targets (chosen by tag) and with what credentials. config.addNotificationTarget( snmpEngine, 'my-notification', 'my-creds', 'all-my-managers', 'trap' ) # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine,
# First target config.addTargetAddr( snmpEngine, "my-nms-1", udp.domainName, ("195.218.195.228", 162), "my-creds", tagList="all-my-managers" ) # Second target config.addTargetAddr( snmpEngine, "my-nms-2", udp.domainName, ("195.218.195.228", 162), "my-creds", tagList="all-my-managers" ) # Third target config.addTargetAddr( snmpEngine, "my-nms-3", udp.domainName, ("195.218.195.228", 162), "my-creds", tagList="all-my-managers" ) # Specify what kind of notification should be sent (TRAP or INFORM) # to what targets (chosen by tag) and with what credentials. config.addNotificationTarget(snmpEngine, "my-notification", "my-creds", "all-my-managers", "trap") # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(snmpEngine, "") config.addVacmUser(snmpEngine, 2, "my-area", "noAuthNoPriv", (), (), (1, 3, 6)) # *** SNMP engine configuration is complete by this line *** # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets
config.usmDESPrivProtocol, 'privkey1' ) # Transport params config.addTargetParams(snmpEngine, 'myParams', 'test-user', 'authPriv') #config.addTargetParams(snmpEngine, 'myParams', 'test-agent', 'noAuthNoPriv', 0) # Transport addresses config.addTargetAddr( snmpEngine, 'myNMS', config.snmpUDPDomain, ('127.0.0.1', 162), 'myParams', tagList='myManagementStations' ) # Notification targets config.addNotificationTarget( # snmpEngine, 'myNotifyName', 'myParams', 'myManagementStations', 'trap' snmpEngine, 'myNotifyName', 'myParams', 'myManagementStations', 'inform' ) # Setup transport endpoint config.addSocketTransport( snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode() ) # Agent-side VACM setup config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 1, 'test-agent', 'noAuthNoPriv', (), (), (1,3,6)) # v1 config.addVacmUser(snmpEngine, 2, 'test-agent', 'noAuthNoPriv', (), (), (1,3,6)) # v2c
def setTrapReceiver(self, host, community): config.addV1System(self._snmpEngine, 'nms-area',community) config.addVacmUser(self._snmpEngine, 2, 'nms-area','noAuthNoPriv', notifySubTree=(1,3,6,1,4,1)) config.addTargetParams(self._snmpEngine, 'nms-creds', 'nms-area','noAuthNoPriv',1) config.addTargetAddr(self._snmpEngine, 'my-nms', udp.domainName, (host, 162), 'nms-creds',tagList = 'all-my-managers') config.addNotificationTarget(self._snmpEngine, 'test-notification', 'my-filter', 'all-my-managers', 'trap')
def __init__(self, mibPath, temperatureValue, snmpRelays, criticalStatus=True): from types import ListType, TupleType,StringTypes from re import compile,search from socket import gethostbyname extractPaths=compile(r'[,:]') checkIP=compile(r'(\d{1,3}\.){3}\d{1,3}') # Create SNMP engine instance self.snmpEngine = engine.SnmpEngine() if not temperatureValue: raise ValueError, 'A temperature must be provided' self.temperature=temperatureValue #print "============>mibPath type: %s" %type(mibPath) if type(mibPath) in StringTypes: mibPathTuple=tuple(extractPaths.split(mibPath)) elif type(mibPath) in (ListType, TupleType): mibPathTuple=tuple(mibPath) else: mibPathTuple=('/usr/local/share/snmp/python/',) mibBuilder = self.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder #print mibPathTuple mibSources = mibBuilder.getMibPath() + mibPathTuple mibBuilder.setMibPath(*mibSources) mibBuilder.loadModules( 'USC-IGFAE-MIB' ) if type(snmpRelays) in StringTypes: snmpRelays=snmpRelays.split(',') elif not type(snmpRelays) in (ListType,TupleType): raise TypeError, 'The list of SNMP relays must be a string or a list or tuple of strings' (temperatureCritical, temperatureOK, self.roomTemp) = mibBuilder.importSymbols('USC-IGFAE-MIB','temperatureCritical', 'temperatureOK', 'roomTemp' ) # SecurityName <-> CommunityName mapping config.addV1System(self.snmpEngine, 'Arduino', 'ups') # Specify security settings per SecurityName (SNMPv2c -> 1) config.addTargetParams(self.snmpEngine, 'creds', 'Arduino', 'noAuthNoPriv', 0) # Setup transport endpoint and bind it with security settings yielding # a target name config.addSocketTransport( self.snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode() ) index=0 for machine in snmpRelays: index=index+1 if not checkIP.match(machine): try: machine=gethostbyname(machine) except: continue #print "==============>SNMP relay IP: %s" % machine config.addTargetAddr( self.snmpEngine, 'NMS%s' % index, udp.domainName, (machine, 162), 'creds', tagList='managers' ) # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget( self.snmpEngine, 'sendShutdownTrap', 'my-filter', 'managers', 'trap' ) # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(self.snmpEngine, '') config.addVacmUser(self.snmpEngine, 1, 'Arduino', 'noAuthNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** # Create default SNMP context where contextEngineId == SnmpEngineId snmpContext = context.SnmpContext(self.snmpEngine) if criticalStatus: self.trap=temperatureCritical else: self.trap=temperatureOK # Create Notification Originator App instance. ntforg.NotificationOriginator.__init__(self,snmpContext)
inTableFlag = 1 if not inTableFlag: return # stop on end-of-table return 1 # continue walking snmpContext = context.SnmpContext(snmpEngine) # Agent-side VACM setup config.addContext(snmpEngine, '') config.addTrapUser(snmpEngine, 1, ctx['securityName'], 'noAuthNoPriv', (1, 3, 6)) # v1 config.addTrapUser(snmpEngine, 2, ctx['securityName'], 'noAuthNoPriv', (1, 3, 6)) # v2c config.addTrapUser(snmpEngine, 3, ctx['securityName'], 'authPriv', (1, 3, 6)) # v3 ctx['notificationName'] = 'myNotifyName' config.addNotificationTarget(snmpEngine, ctx['notificationName'], ctx['paramsName'], ctx['transportTag'], 'trap') ntforg.NotificationOriginator(snmpContext).sendNotification( snmpEngine, ctx['notificationName'], ('SNMPv2-MIB', ctx['GenericTrap']), ctx['varBinds'], cbFun, ctx) try: snmpEngine.transportDispatcher.runDispatcher() except error.PySnmpError, why: sys.stderr.write('Error: %s\n' % why) sys.exit(-1)
snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode() ) config.addTargetAddr( snmpEngine, 'my-nms', udp.domainName, ('104.236.166.95', 162), 'my-creds', tagList='all-my-managers' ) # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget( snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'inform' ) # Allow NOTIFY access to Agent's MIB by this SNMP model (3), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (), (), (1, 3, 6)) # *** SNMP engine configuration is complete by this line *** # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() # Error/confirmation receiver # noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
# Setup transport endpoint and bind it with security settings yielding # a target name config.addTransport(snmpEngine, udp.DOMAIN_NAME, udp.UdpSocketTransport().openClientMode()) config.addTargetAddr(snmpEngine, 'my-nms', udp.DOMAIN_NAME, ('104.236.166.95', 162), 'my-creds', tagList='all-my-managers') # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget(snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'inform') # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6)) # *** SNMP engine configuration is complete by this line *** # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() # Error/confirmation receiver # noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus,
def sendTheTrap(): uptime = getUptime() # Create SNMP engine instance with specific (and locally unique) # SnmpEngineId -- it must also be known to the receiving party # and configured at its VACM users table. snmpEngine = engine.SnmpEngine(snmpEngineID=v2c.OctetString( hexValue='0102030405060708')) # Add USM user config.addV3User(snmpEngine, userConfig['DEFAULT']['SNMPUSER'], config.usmHMAC128SHA224AuthProtocol, userConfig['DEFAULT']['SNMPAUTH'], config.usmAesCfb192Protocol, userConfig['DEFAULT']['SNMPPRIV']) config.addTargetParams(snmpEngine, userConfig['DEFAULT']['SNMPAUTH'], userConfig['DEFAULT']['SNMPUSER'], 'authPriv') # Setup transport endpoint and bind it with security settings yielding # a target name config.addTransport(snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode()) config.addTargetAddr(snmpEngine, 'my-nms', udp.domainName, (userConfig['DEFAULT']['SNMPMANAGERIP'], int(userConfig['DEFAULT']['SNMPMANAGERPORT'])), userConfig['DEFAULT']['SNMPAUTH'], tagList='all-my-managers') # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget(snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap') # Allow NOTIFY access to Agent's MIB by this SNMP model (3), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 3, userConfig['DEFAULT']['SNMPUSER'], 'authPriv', (), (), (1, 3, 6), 'aContextName') # *** SNMP engine configuration is complete by this line *** # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets 'my-notification', # notification targets None, 'aContextName', # contextEngineId, contextName # var-binds [((1, 3, 6, 1, 2, 1, 1, 3, 0), v2c.OctetString(uptime)), ((1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0), v2c.ObjectIdentifier((1, 3, 6, 1, 6, 3, 1, 1, 5, 1))), ((1, 3, 6, 1, 2, 1, 1, 5, 0), v2c.OctetString(socket.getfqdn())), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 10, 0), v2c.OctetString('Application')), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 11, 0), v2c.OctetString('Performance')), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 12, 0), v2c.OctetString('critical')), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 19, 0), v2c.OctetString('health')), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 20, 0), v2c.OctetString('vROpsExternalMonitorService.py')), ((1, 3, 6, 1, 4, 1, 6876, 4, 50, 1, 2, 50, 0), v2c.OctetString( 'vROps services are having issues, please check nodes'))]) print('Notification is scheduled to be sent') # Run I/O dispatcher which would send pending message and process response snmpEngine.transportDispatcher.runDispatcher()
def sendTrap(self, version, enterprise, varList, community=False, destPort=False): if destPort: trapPort = destPort else: trapPort = TRAP_PORT if community: comm = community else: comm = DEFAULT_COMM snmpEngine = engine.SnmpEngine() # v1/2 setup config.addV1System(snmpEngine, TEST_AGENT, comm) # v3 setup config.addV3User(snmpEngine, TEST_USER, config.usmHMACMD5AuthProtocol, 'authKey1', config.usmDESPrivProtocol, 'privKey1') # Transport params config.addTargetParams(snmpEngine, PARAM, TEST_USER, AUTHPRIV) #config.addTargetParams(snmpEngine, 'myParams', 'test-agent', 'noAuthNoPriv', 0) # Transport addresses config.addTargetAddr(snmpEngine, NMS, config.snmpUDPDomain, (self.dataCollector, trapPort), PARAM, tagList='myManagementStations') # Notification targets config.addNotificationTarget( # snmpEngine, 'myNotifyName', 'myParams', 'myManagementStations', 'trap' snmpEngine, 'myNotifyName', PARAM, 'myManagementStations', 'inform') # Setup transport endpoint config.addSocketTransport(snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode()) # Agent-side VACM setup config.addContext(snmpEngine, '') config.addTrapUser(snmpEngine, 1, 'test-agent', 'noAuthNoPriv', (1, 3, 6)) # v1 config.addTrapUser(snmpEngine, 2, 'test-agent', 'noAuthNoPriv', (1, 3, 6)) # v2c config.addTrapUser(snmpEngine, 3, 'test-user', 'authPriv', (1, 3, 6)) # v3 # SNMP context snmpContext = context.SnmpContext(snmpEngine) def cbFun(sendRequestHandle, errorIndication, cbCtx): if errorIndication: print errorIndication ntforg.NotificationOriginator(snmpContext).sendNotification( snmpEngine, 'myNotifyName', ('SNMPv2-MIB', 'coldStart'), (((1, 3, 6, 1, 2, 1, 1, 5), v2c.OctetString('Example Notificator')), ), cbFun) snmpEngine.transportDispatcher.runDispatcher()
def __init__(self, mibPath, temperatureValue, snmpRelays, criticalStatus=True): from types import ListType, TupleType, StringTypes from re import compile, search from socket import gethostbyname extractPaths = compile(r'[,:]') checkIP = compile(r'(\d{1,3}\.){3}\d{1,3}') # Create SNMP engine instance self.snmpEngine = engine.SnmpEngine() if not temperatureValue: raise ValueError, 'A temperature must be provided' self.temperature = temperatureValue #print "============>mibPath type: %s" %type(mibPath) if type(mibPath) in StringTypes: mibPathTuple = tuple(extractPaths.split(mibPath)) elif type(mibPath) in (ListType, TupleType): mibPathTuple = tuple(mibPath) else: mibPathTuple = ('/usr/local/share/snmp/python/', ) mibBuilder = self.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder #print mibPathTuple mibSources = mibBuilder.getMibPath() + mibPathTuple mibBuilder.setMibPath(*mibSources) mibBuilder.loadModules('USC-IGFAE-MIB') if type(snmpRelays) in StringTypes: snmpRelays = snmpRelays.split(',') elif not type(snmpRelays) in (ListType, TupleType): raise TypeError, 'The list of SNMP relays must be a string or a list or tuple of strings' (temperatureCritical, temperatureOK, self.roomTemp) = mibBuilder.importSymbols('USC-IGFAE-MIB', 'temperatureCritical', 'temperatureOK', 'roomTemp') # SecurityName <-> CommunityName mapping config.addV1System(self.snmpEngine, 'Arduino', 'ups') # Specify security settings per SecurityName (SNMPv2c -> 1) config.addTargetParams(self.snmpEngine, 'creds', 'Arduino', 'noAuthNoPriv', 0) # Setup transport endpoint and bind it with security settings yielding # a target name config.addSocketTransport(self.snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode()) index = 0 for machine in snmpRelays: index = index + 1 if not checkIP.match(machine): try: machine = gethostbyname(machine) except: continue #print "==============>SNMP relay IP: %s" % machine config.addTargetAddr(self.snmpEngine, 'NMS%s' % index, udp.domainName, (machine, 162), 'creds', tagList='managers') # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget(self.snmpEngine, 'sendShutdownTrap', 'my-filter', 'managers', 'trap') # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(self.snmpEngine, '') config.addVacmUser(self.snmpEngine, 1, 'Arduino', 'noAuthNoPriv', (), (), (1, 3, 6)) # *** SNMP engine configuration is complete by this line *** # Create default SNMP context where contextEngineId == SnmpEngineId snmpContext = context.SnmpContext(self.snmpEngine) if criticalStatus: self.trap = temperatureCritical else: self.trap = temperatureOK # Create Notification Originator App instance. ntforg.NotificationOriginator.__init__(self, snmpContext)
#) #config.addTargetAddr( # snmpEngine, 'my-nms', # unix.domainName, '/tmp/snmp-manager', # 'my-creds', # tagList='all-my-managers' #) # # Specify what kind of notification should be sent (TRAP or INFORM) # to what targets (chosen by tag) and with what credentials. # config.addNotificationTarget( # snmpEngine, 'my-notification', 'my-creds', 'all-my-managers', 'trap' snmpEngine, 'my-notification', 'my-creds', 'all-my-managers', 'inform') # # Notifications carry potentially confidential information from # the Agent. Therefore access control is to be setup allowing # NotificationOriginator access to certain portions of Agent MIB. # config.addContext(snmpEngine, '') # SNMPv1 config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6)) # SNMPv2c config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6)) # SNMPv3
def trigger_trap(temp): # Create SNMP engine instance snmpEngine = engine.SnmpEngine() # SecurityName <-> CommunityName mapping config.addV1System(snmpEngine, 'my-area', 'federated') # Specify security settings per SecurityName (SNMPv2c -> 1) config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) # Setup transport endpoint and bind it with security settings yielding # a target name config.addSocketTransport( snmpEngine, udp.domainName, udp.UdpSocketTransport().openClientMode() ) config.addTargetAddr( snmpEngine, 'my-nms', udp.domainName, ('34.215.95.184', 162), 'my-creds', tagList='all-my-managers' ) # Specify what kind of notification should be sent (TRAP or INFORM), # to what targets (chosen by tag) and what filter should apply to # the set of targets (selected by tag) config.addNotificationTarget( snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap' ) # Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel # and SecurityName config.addContext(snmpEngine, '') config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** # Create default SNMP context where contextEngineId == SnmpEngineId snmpContext = context.SnmpContext(snmpEngine) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator(snmpContext) # Build and submit notification message to dispatcher ntfOrg.sendNotification( snmpEngine, # Notification targets 'my-notification', # Trap OID (SNMPv2-MIB::coldStart) #(1,3,6,1,6,3,1,1,5,1), (1,3,6,1,4,1,8072,2,7,1,1,1,1,3,1), # ( (oid, value), ... ) ( ((1,3,6,1,4,1,8072,2,7,1,1,1,1,3,1), v2c.OctetString(temp)), ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Reason: Admin brought down')) ) ) print('Notification is scheduled to be sent') # Run I/O dispatcher which would send pending message and process response snmpEngine.transportDispatcher.runDispatcher()