def variate(oid, tag, value, **context): if 'snmpEngine' in context and context['snmpEngine']: snmpEngine = context['snmpEngine'] if snmpEngine not in moduleContext: moduleContext[snmpEngine] = {} if context['transportDomain'] not in moduleContext[snmpEngine]: # register this SNMP Engine to handle our transports' # receiver IDs (which we build by outbound and simulator # transportDomains concatenation) snmpEngine.registerTransportDispatcher( snmpEngine.transportDispatcher, UdpTransportTarget.transportDomain + context['transportDomain']) snmpEngine.registerTransportDispatcher( snmpEngine.transportDispatcher, Udp6TransportTarget.transportDomain + context['transportDomain']) moduleContext[snmpEngine][context['transportDomain']] = 1 else: raise error.SnmpsimError( 'Variation module is not given snmpEngine. ' 'Make sure you are not running in --v2c-arch mode') if not context['nextFlag'] and not context['exactMatch']: return context['origOid'], tag, context['errorStatus'] if 'settings' not in recordContext: recordContext['settings'] = dict( [split(x, '=') for x in split(value, ',')]) for k, v in MODULE_OPTIONS: recordContext['settings'].setdefault(k, v) if 'hexvalue' in recordContext['settings']: recordContext['settings']['value'] = [ int(recordContext['settings']['hexvalue'][x:x + 2], 16) for x in range( 0, len(recordContext['settings']['hexvalue']), 2) ] if 'vlist' in recordContext['settings']: vlist = {} recordContext['settings']['vlist'] = split( recordContext['settings']['vlist'], ':') while recordContext['settings']['vlist']: o, v = recordContext['settings']['vlist'][:2] vlist = recordContext['settings']['vlist'][2:] recordContext['settings']['vlist'] = vlist typeTag, _ = SnmprecRecord.unpack_tag(tag) v = SnmprecGrammar.TAG_MAP[typeTag](v) if o not in vlist: vlist[o] = set() if o == 'eq': vlist[o].add(v) elif o in ('lt', 'gt'): vlist[o] = v else: log.info('notification: bad vlist syntax: ' '%s' % recordContext['settings']['vlist']) recordContext['settings']['vlist'] = vlist args = recordContext['settings'] if context['setFlag'] and 'vlist' in args: if ('eq' in args['vlist'] and context['origValue'] in args['vlist']['eq']): pass elif ('lt' in args['vlist'] and context['origValue'] < args['vlist']['lt']): pass elif ('gt' in args['vlist'] and context['origValue'] > args['vlist']['gt']): pass else: return oid, tag, context['origValue'] if args['op'] not in ('get', 'set', 'any', '*'): log.info('notification: unknown SNMP request type configured: ' '%s' % args['op']) return context['origOid'], tag, context['errorStatus'] if (args['op'] == 'get' and not context['setFlag'] or args['op'] == 'set' and context['setFlag'] or args['op'] in ('any', '*')): if args['version'] in ('1', '2c'): authData = CommunityData(args['community'], mpModel=args['version'] == '2c' and 1 or 0) elif args['version'] == '3': if args['authproto'] == 'md5': authProtocol = usmHMACMD5AuthProtocol elif args['authproto'] == 'sha': authProtocol = usmHMACSHAAuthProtocol elif args['authproto'] == 'none': authProtocol = usmNoAuthProtocol else: log.info('notification: unknown auth proto ' '%s' % args['authproto']) return context['origOid'], tag, context['errorStatus'] if args['privproto'] == 'des': privProtocol = usmDESPrivProtocol elif args['privproto'] == 'aes': privProtocol = usmAesCfb128Protocol elif args['privproto'] == 'none': privProtocol = usmNoPrivProtocol else: log.info('notification: unknown privacy proto ' '%s' % args['privproto']) return context['origOid'], tag, context['errorStatus'] authData = UsmUserData(args['user'], args['authkey'], args['privkey'], authProtocol=authProtocol, privProtocol=privProtocol) else: log.info('notification: unknown SNMP version %s' % args['version']) return context['origOid'], tag, context['errorStatus'] if 'host' not in args: log.info('notification: target hostname not configured for ' 'OID %s' % (oid, )) return context['origOid'], tag, context['errorStatus'] if args['proto'] == 'udp': target = UdpTransportTarget((args['host'], int(args['port']))) elif args['proto'] == 'udp6': target = Udp6TransportTarget((args['host'], int(args['port']))) else: log.info('notification: unknown transport %s' % args['proto']) return context['origOid'], tag, context['errorStatus'] localAddress = None if 'bindaddr' in args: localAddress = args['bindaddr'] else: transportDomain = context['transportDomain'][:len(target. transportDomain)] if transportDomain == target.transportDomain: localAddress = snmpEngine.transportDispatcher.getTransport( context['transportDomain']).getLocalAddress()[0] else: log.info( 'notification: incompatible network transport types used by ' 'CommandResponder vs NotificationOriginator') if 'bindaddr' in args: localAddress = args['bindaddr'] if localAddress: log.info('notification: binding to local address %s' % localAddress) target.setLocalAddress((localAddress, 0)) # this will make target objects different based on their bind address target.transportDomain = target.transportDomain + context[ 'transportDomain'] varBinds = [] if 'uptime' in args: varBinds.append((ObjectIdentifier('1.3.6.1.2.1.1.3.0'), TimeTicks(args['uptime']))) if args['version'] == '1': if 'agentaddress' in args: varBinds.append((ObjectIdentifier('1.3.6.1.6.3.18.1.3.0'), IpAddress(args['agentaddress']))) if 'enterprise' in args: varBinds.append((ObjectIdentifier('1.3.6.1.6.3.1.1.4.3.0'), ObjectIdentifier(args['enterprise']))) if 'varbinds' in args: vbs = split(args['varbinds'], ':') while vbs: varBinds.append( (ObjectIdentifier(vbs[0]), TYPE_MAP[vbs[1]](vbs[2]))) vbs = vbs[3:] notificationType = NotificationType(ObjectIdentity( args['trapoid'])).addVarBinds(*varBinds) sendNotification(snmpEngine, authData, target, ContextData(), args['ntftype'], notificationType, cbFun=_cbFun, cbCtx=(oid, value)) log.info('notification: sending Notification to %s with credentials ' '%s' % (authData, target)) if context['setFlag'] or 'value' not in args: return oid, tag, context['origValue'] else: return oid, tag, args['value']
def variate(oid, tag, value, **context): if not context['nextFlag'] and not context['exactMatch']: return context['origOid'], tag, context['errorStatus'] if 'settings' not in recordContext: recordContext['settings'] = dict( [split(x, '=') for x in split(value, ',')]) if 'hexvalue' in recordContext['settings']: recordContext['settings']['value'] = [ int(recordContext['settings']['hexvalue'][x:x + 2], 16) for x in range(0, len(recordContext['settings']['hexvalue']), 2)] if 'wait' in recordContext['settings']: recordContext['settings']['wait'] = float( recordContext['settings']['wait']) else: recordContext['settings']['wait'] = 500.0 if 'deviation' in recordContext['settings']: recordContext['settings']['deviation'] = float( recordContext['settings']['deviation']) else: recordContext['settings']['deviation'] = 0.0 if 'vlist' in recordContext['settings']: vlist = {} recordContext['settings']['vlist'] = split( recordContext['settings']['vlist'], ':') while recordContext['settings']['vlist']: o, v, d = recordContext['settings']['vlist'][:3] recordContext['settings']['vlist'] = recordContext['settings']['vlist'][3:] d = int(d) type_tag, _ = SnmprecRecord.unpack_tag(tag) v = SnmprecGrammar.TAG_MAP[type_tag](v) if o not in vlist: vlist[o] = {} if o == 'eq': vlist[o][v] = d elif o in ('lt', 'gt'): vlist[o] = v, d else: log.info('delay: bad vlist syntax: ' '%s' % recordContext['settings']['vlist']) recordContext['settings']['vlist'] = vlist if 'tlist' in recordContext['settings']: tlist = {} recordContext['settings']['tlist'] = split( recordContext['settings']['tlist'], ':') while recordContext['settings']['tlist']: o, v, d = recordContext['settings']['tlist'][:3] recordContext['settings']['tlist'] = recordContext['settings']['tlist'][3:] v = int(v) d = int(d) if o not in tlist: tlist[o] = {} if o == 'eq': tlist[o][v] = d elif o in ('lt', 'gt'): tlist[o] = v, d else: log.info('delay: bad tlist syntax: ' '%s' % recordContext['settings']['tlist']) recordContext['settings']['tlist'] = tlist if context['setFlag'] and 'vlist' in recordContext['settings']: if ('eq' in recordContext['settings']['vlist'] and context['origValue'] in recordContext['settings']['vlist']['eq']): delay = recordContext['settings']['vlist']['eq'][context['origValue']] elif ('lt' in recordContext['settings']['vlist'] and context['origValue'] < recordContext['settings']['vlist']['lt'][0]): delay = recordContext['settings']['vlist']['lt'][1] elif ('gt' in recordContext['settings']['vlist'] and context['origValue'] > recordContext['settings']['vlist']['gt'][0]): delay = recordContext['settings']['vlist']['gt'][1] else: delay = recordContext['settings']['wait'] elif 'tlist' in recordContext['settings']: now = int(time.time()) if ('eq' in recordContext['settings']['tlist'] and now == recordContext['settings']['tlist']['eq']): delay = recordContext['settings']['tlist']['eq'][now] elif ('lt' in recordContext['settings']['tlist'] and now < recordContext['settings']['tlist']['lt'][0]): delay = recordContext['settings']['tlist']['lt'][1] elif ('gt' in recordContext['settings']['tlist'] and now > recordContext['settings']['tlist']['gt'][0]): delay = recordContext['settings']['tlist']['gt'][1] else: delay = recordContext['settings']['wait'] else: delay = recordContext['settings']['wait'] if recordContext['settings']['deviation']: delay += random.randrange( -recordContext['settings']['deviation'], recordContext['settings']['deviation']) if delay < 0: delay = 0 elif delay > 99999: log.info('delay: dropping response for %s' % oid) raise error.NoDataNotification() log.info('delay: waiting %d milliseconds for %s' % (delay, oid)) time.sleep(delay / 1000) # ms if context['setFlag'] or 'value' not in recordContext['settings']: return oid, tag, context['origValue'] else: return oid, tag, recordContext['settings']['value']
def variate(oid, tag, value, **context): if not context['nextFlag'] and not context['exactMatch']: return context['origOid'], tag, context['errorStatus'] if 'settings' not in recordContext: recordContext['settings'] = dict( [split(x, '=') for x in split(value, ',')]) if 'hexvalue' in recordContext['settings']: recordContext['settings']['value'] = [ int(recordContext['settings']['hexvalue'][x:x + 2], 16) for x in range(0, len(recordContext['settings']['hexvalue']), 2)] if 'status' in recordContext['settings']: recordContext['settings']['status'] = recordContext['settings']['status'].lower() if 'op' not in recordContext['settings']: recordContext['settings']['op'] = 'any' if 'vlist' in recordContext['settings']: vlist = {} recordContext['settings']['vlist'] = split(recordContext['settings']['vlist'], ':') while recordContext['settings']['vlist']: o, v, e = recordContext['settings']['vlist'][:3] recordContext['settings']['vlist'] = recordContext['settings']['vlist'][3:] typeTag, _ = SnmprecRecord.unpack_tag(tag) v = SnmprecGrammar.TAG_MAP[typeTag](v) if o not in vlist: vlist[o] = {} if o == 'eq': vlist[o][v] = e elif o in ('lt', 'gt'): vlist[o] = v, e else: log.info('error: bad vlist syntax: %s' % recordContext['settings']['vlist']) recordContext['settings']['vlist'] = vlist e = None if context['setFlag']: if 'vlist' in recordContext['settings']: if ('eq' in recordContext['settings']['vlist'] and context['origValue'] in recordContext['settings']['vlist']['eq']): e = recordContext['settings']['vlist']['eq'][context['origValue']] elif ('lt' in recordContext['settings']['vlist'] and context['origValue'] < recordContext['settings']['vlist']['lt'][0]): e = recordContext['settings']['vlist']['lt'][1] elif ('gt' in recordContext['settings']['vlist'] and context['origValue'] > recordContext['settings']['vlist']['gt'][0]): e = recordContext['settings']['vlist']['gt'][1] elif recordContext['settings']['op'] in ('set', 'any'): if 'status' in recordContext['settings']: e = recordContext['settings']['status'] else: if recordContext['settings']['op'] in ('get', 'any'): if 'status' in recordContext['settings']: e = recordContext['settings']['status'] if e and e in ERROR_TYPES: log.info('error: reporting %s for %s' % (e, oid)) raise ERROR_TYPES[e]( name=oid, idx=max(0, context['varsTotal'] - context['varsRemaining'] - 1)) if context['setFlag']: recordContext['settings']['value'] = context['origValue'] return oid, tag, recordContext['settings'].get('value', context['errorStatus'])
def variate(oid, tag, value, **context): if not context['nextFlag'] and not context['exactMatch']: return context['origOid'], tag, context['errorStatus'] if 'settings' not in recordContext: recordContext['settings'] = dict([split(x, '=') for x in split(value, ',')]) if 'vlist' in recordContext['settings']: vlist = {} recordContext['settings']['vlist'] = split( recordContext['settings']['vlist'], ':') while recordContext['settings']['vlist']: o, v, e = recordContext['settings']['vlist'][:3] vl = recordContext['settings']['vlist'][3:] recordContext['settings']['vlist'] = vl type_tag, _ = SnmprecRecord.unpack_tag(tag) v = SnmprecGrammar.TAG_MAP[type_tag](v) if o not in vlist: vlist[o] = {} if o == 'eq': vlist[o][v] = e elif o in ('lt', 'gt'): vlist[o] = v, e else: log.info('writecache: bad vlist syntax: ' '%s' % recordContext['settings']['vlist']) recordContext['settings']['vlist'] = vlist if 'status' in recordContext['settings']: st = recordContext['settings']['status'].lower() recordContext['settings']['status'] = st if oid not in moduleContext: moduleContext[oid] = {} type_tag, _ = SnmprecRecord.unpack_tag(tag) moduleContext[oid]['type'] = SnmprecGrammar.TAG_MAP[type_tag]() text_oid = str(oid) if context['setFlag']: if 'vlist' in recordContext['settings']: if ('eq' in recordContext['settings']['vlist'] and context['origValue'] in recordContext['settings']['vlist']['eq']): e = recordContext['settings']['vlist']['eq'][context['origValue']] elif ('lt' in recordContext['settings']['vlist'] and context['origValue'] < recordContext['settings']['vlist']['lt'][0]): e = recordContext['settings']['vlist']['lt'][1] elif ('gt' in recordContext['settings']['vlist'] and context['origValue'] > recordContext['settings']['vlist']['gt'][0]): e = recordContext['settings']['vlist']['gt'][1] else: e = None if e in ERROR_TYPES: idx = max(0, context['varsTotal'] - context['varsRemaining'] - 1) raise ERROR_TYPES[e](name=oid, idx=idx) if moduleContext[oid]['type'].isSameTypeWith(context['origValue']): moduleContext['cache'][text_oid] = context['origValue'] else: return context['origOid'], tag, context['errorStatus'] if 'status' in recordContext['settings']: if ('op' not in recordContext['settings'] or recordContext['settings']['op'] == 'any' or recordContext['settings']['op'] == 'set' and context['setFlag'] or recordContext['settings']['op'] == 'get' and not context['setFlag']): e = recordContext['settings']['status'] if e in ERROR_TYPES: idx = max(0, context['varsTotal'] - context['varsRemaining'] - 1) raise ERROR_TYPES[e](name=oid, idx=idx) if text_oid in moduleContext['cache']: return oid, tag, moduleContext['cache'][text_oid] elif 'hexvalue' in recordContext['settings']: return oid, tag, moduleContext[oid]['type'].clone( hexValue=recordContext['settings']['hexvalue']) elif 'value' in recordContext['settings']: return oid, tag, moduleContext[oid]['type'].clone( recordContext['settings']['value']) else: return oid, tag, context['errorStatus']