def record(oid, tag, value, **context): if 'ready' not in moduleContext: raise error.SnmpsimError('module not initialized') if 'started' not in moduleContext: moduleContext['started'] = time.time() if context['stopFlag']: if 'file' in moduleContext: moduleContext['file'].close() del moduleContext['file'] else: moduleContext['filenum'] = 0 if 'iterations' in moduleContext and moduleContext['iterations']: log.msg('multiplex: %s iterations remaining' % moduleContext['iterations']) moduleContext['started'] = time.time() moduleContext['iterations'] -= 1 moduleContext['filenum'] += 1 wait = max(0, moduleContext['period'] - (time.time() - moduleContext['started'])) raise error.MoreDataNotification(period=wait) else: raise error.NoDataNotification() if 'file' not in moduleContext: if 'filenum' not in moduleContext: moduleContext['filenum'] = 0 snmprecfile = os.path.join(moduleContext['dir'], '%.5d%ssnmprec' % (moduleContext['filenum'], os.path.extsep)) moduleContext['file'] = open(snmprecfile, 'wb') log.msg('multiplex: writing into %s file...' % snmprecfile) moduleContext['file'].write( SnmprecRecord().format(context['origOid'], context['origValue']) ) if not context['total']: settings = { 'dir': moduleContext['dir'].replace(os.path.sep, '/') } if 'period' in moduleContext: settings['period'] = '%.2f' % float(moduleContext['period']) if 'addon' in moduleContext: settings.update( dict([split(x, '=') for x in moduleContext['addon']]) ) value = ','.join(['%s=%s' % (k, v) for k, v in settings.items()]) return str(context['startOID']), ':multiplex', value else: raise error.NoDataNotification()
def formatValue(self, oid, value, **context): oid, snmp_type, snmp_value = SnmprecRecord.formatValue( self, oid, value) if 'stopFlag' in context and context['stopFlag']: raise NoDataNotification() type = self.SNMP_TYPE_MAP.get(re.sub("\D*", "", snmp_type)) if not type: print "Failed to parse record: {}, {}, {}".format( oid, snmp_type, snmp_value) if "string" in type.lower() and "x" not in snmp_type: snmp_value = snmp_value.encode("hex") if "64" in snmp_type: ip = snmp_value if "x" in snmp_type: ip = snmp_value.decode("hex") value = socket.inet_ntoa(ip) else: value = self.get_snmp_value(type, snmp_value) return oid, type, value
def variate(oid, tag, value, **context): if 'settings' not in recordContext: recordContext['settings'] = dict( [split(x, '=') for x in split(value, ',')]) if 'dir' not in recordContext['settings']: log.msg('multiplex: snapshot directory not specified') return context['origOid'], tag, context['errorStatus'] recordContext['settings']['dir'] = recordContext['settings'][ 'dir'].replace('/', os.path.sep) if recordContext['settings']['dir'][0] != os.path.sep: for x in confdir.data: d = os.path.join(x, recordContext['settings']['dir']) if os.path.exists(d): break else: log.msg('multiplex: directory %s not found' % recordContext['settings']['dir']) return context['origOid'], tag, context['errorStatus'] else: d = recordContext['settings']['dir'] recordContext['dirmap'] = dict([ (int(os.path.basename(x).split(os.path.extsep)[0]), os.path.join(d, x)) for x in os.listdir(d) if x[-7:] == 'snmprec' ]) recordContext['keys'] = list(recordContext['dirmap'].keys()) recordContext['bounds'] = (min(recordContext['keys']), max(recordContext['keys'])) if 'period' in recordContext['settings']: recordContext['settings']['period'] = float( recordContext['settings']['period']) else: recordContext['settings']['period'] = 60.0 if 'wrap' in recordContext['settings']: recordContext['settings']['wrap'] = bool( recordContext['settings']['wrap']) else: recordContext['settings']['wrap'] = False if 'control' in recordContext['settings']: recordContext['settings']['control'] = rfc1902.ObjectName( recordContext['settings']['control']) log.msg( 'multiplex: using control OID %s for subtree %s, time-based multiplexing disabled' % (recordContext['settings']['control'], oid)) recordContext['ready'] = True if 'ready' not in recordContext: return context['origOid'], tag, context['errorStatus'] if oid not in moduleContext: moduleContext[oid] = {} if context['setFlag']: if 'control' in recordContext['settings'] and \ recordContext['settings']['control'] == context['origOid']: fileno = int(context['origValue']) if fileno >= len(recordContext['keys']): log.msg('multiplex: .snmprec file number %s over limit of %s' % (fileno, len(recordContext['keys']))) return context['origOid'], tag, context['errorStatus'] moduleContext[oid]['fileno'] = fileno log.msg('multiplex: switched to file #%s (%s)' % (recordContext['keys'][fileno], recordContext['dirmap'][recordContext['keys'][fileno]])) return context['origOid'], tag, context['origValue'] else: return context['origOid'], tag, context['errorStatus'] if 'control' in recordContext['settings']: if 'fileno' not in moduleContext[oid]: moduleContext[oid]['fileno'] = 0 if (not context['nextFlag'] and recordContext['settings']['control'] == context['origOid']): return context['origOid'], tag, rfc1902.Integer32( moduleContext[oid]['fileno']) else: timeslot = (time.time() - moduleContext['booted']) % ( recordContext['settings']['period'] * len(recordContext['dirmap'])) fileslot = int( timeslot / recordContext['settings']['period']) + recordContext['bounds'][0] fileno = bisect.bisect(recordContext['keys'], fileslot) - 1 if ('fileno' not in moduleContext[oid] or moduleContext[oid]['fileno'] < fileno or recordContext['settings']['wrap']): moduleContext[oid]['fileno'] = fileno datafile = recordContext['dirmap'][recordContext['keys'][moduleContext[oid] ['fileno']]] if ('datafile' not in moduleContext[oid] or moduleContext[oid]['datafile'] != datafile): if 'datafileobj' in moduleContext[oid]: moduleContext[oid]['datafileobj'].close() moduleContext[oid]['datafileobj'] = RecordIndex( datafile, SnmprecRecord()).create() moduleContext[oid]['datafile'] = datafile log.msg('multiplex: switching to data file %s for %s' % (datafile, context['origOid'])) text, db = moduleContext[oid]['datafileobj'].getHandles() textOid = str( rfc1902.OctetString('.'.join(['%s' % x for x in context['origOid']]))) try: line = moduleContext[oid]['datafileobj'].lookup(textOid) except KeyError: offset = searchRecordByOid(context['origOid'], text, SnmprecRecord()) exactMatch = False else: offset, subtreeFlag, prevOffset = line.split(str2octs(',')) exactMatch = True text.seek(int(offset)) line, _, _ = getRecord(text) # matched line if context['nextFlag']: if exactMatch: line, _, _ = getRecord(text) else: if not exactMatch: return context['origOid'], tag, context['errorStatus'] if not line: return context['origOid'], tag, context['errorStatus'] try: oid, value = SnmprecRecord().evaluate(line) except error.SnmpsimError: oid, value = context['origOid'], context['errorStatus'] return oid, tag, value
moduleContext['period'] = float(options['period']) else: moduleContext['period'] = 60.0 redisScript = options.get('evalsha') if redisScript: log.msg('redis: using server-side script %s' % redisScript) elif context['mode'] == 'variating': moduleContext['booted'] = time.time() moduleContext['ready'] = True unpackTag = SnmprecRecord().unpackTag def variate(oid, tag, value, **context): if 'dbConn' in moduleContext: dbConn = moduleContext['dbConn'] else: raise error.SnmpsimError('variation module not initialized') if 'settings' not in recordContext: settings = recordContext['settings'] = dict( [split(x, '=') for x in split(value, ',')]) if 'key-spaces-id' not in settings: log.msg('redis:mandatory key-spaces-id option is missing')
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 '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 typeTag, _ = SnmprecRecord.unpackTag(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.msg('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] = {} typeTag, _ = SnmprecRecord.unpackTag(tag) moduleContext[oid]['type'] = SnmprecGrammar.TAG_MAP[typeTag]() textOid = 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'][textOid] = 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 textOid in moduleContext['cache']: return oid, tag, moduleContext['cache'][textOid] 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']
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 '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) typeTag, _ = SnmprecRecord.unpackTag(tag) v = SnmprecGrammar.TAG_MAP[typeTag](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.msg('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.msg('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.msg('delay: dropping response for %s' % oid) raise error.NoDataNotification() log.msg('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 '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) typeTag, _ = SnmprecRecord.unpackTag(tag) v = SnmprecGrammar.TAG_MAP[typeTag](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.msg('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.msg('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.msg('delay: dropping response for %s' % oid) raise error.NoDataNotification() log.msg('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 '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 typeTag, _ = SnmprecRecord.unpackTag(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.msg('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] = {} typeTag, _ = SnmprecRecord.unpackTag(tag) moduleContext[oid]['type'] = SnmprecGrammar.TAG_MAP[typeTag]() textOid = 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'][textOid] = 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 textOid in moduleContext['cache']: return oid, tag, moduleContext['cache'][textOid] 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']
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.unpackTag(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.msg('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.msg('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'])