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 format_value(self, oid, value, **context): (text_oid, text_tag, text_value) = snmprec.SnmprecRecord.format_value(self, oid, value) if context['variationModule']: (plain_oid, plain_tag, plain_value) = snmprec.SnmprecRecord.format_value(self, oid, value, nohex=True) if plain_tag != text_tag: context['hextag'], context['hexvalue'] = text_tag, text_value else: text_tag, text_value = plain_tag, plain_value handler = context['variationModule']['record'] text_oid, text_tag, text_value = handler(text_oid, text_tag, text_value, **context) elif 'stopFlag' in context and context['stopFlag']: raise error.NoDataNotification() return text_oid, text_tag, text_value
def record(oid, tag, value, **context): if 'dbConn' in moduleContext: db_conn = moduleContext['dbConn'] else: raise error.SnmpsimError('variation module not initialized') db_table = moduleContext['dbTable'] if context['stopFlag']: raise error.NoDataNotification() sql_oid = '.'.join(['%10s' % x for x in oid.split('.')]) if 'hexvalue' in context: text_tag = context['hextag'] text_value = context['hexvalue'] else: text_tag = SnmprecGrammar().get_tag_by_type(context['origValue']) text_value = str(context['origValue']) cursor = db_conn.cursor() cursor.execute('select oid from %s where oid=\'%s\' ' 'limit 1' % (db_table, sql_oid)) if cursor.fetchone(): cursor.execute('update %s set tag=\'%s\',value=\'%s\' where ' 'oid=\'%s\'' % (db_table, text_tag, text_value, sql_oid)) else: cursor.execute('insert into %s values (\'%s\', \'%s\', \'%s\', ' '\'read-write\')' % (db_table, sql_oid, text_tag, text_value)) cursor.close() if not context['count']: return str(context['startOID']), ':sql', db_table else: raise error.NoDataNotification()
def record(oid, tag, value, **context): if context['stopFlag']: raise error.NoDataNotification() tag += ':delay' if 'hexvalue' in context: textValue = 'hexvalue=' + context['hexvalue'] else: textValue = 'value=' + value textValue += ',wait=%d' % int((time.time() - context['reqTime']) * 1000) # ms if 'options' in context: textValue += ',' + context['options'] return oid, tag, textValue
def formatValue(self, oid, value, **context): textOid, textTag, textValue = snmprec.SnmprecRecord.formatValue( self, oid, value) # invoke variation module if context['variationModule']: plainOid, plainTag, plainValue = snmprec.SnmprecRecord.formatValue( self, oid, value, nohex=True) if plainTag != textTag: context['hextag'], context['hexvalue'] = textTag, textValue else: textTag, textValue = plainTag, plainValue textOid, textTag, textValue = context['variationModule']['record']( textOid, textTag, textValue, **context) elif 'stopFlag' in context and context['stopFlag']: raise error.NoDataNotification() return textOid, textTag, textValue
def record(oid, tag, value, **context): if 'ready' not in moduleContext: raise error.SnmpsimError('module not initialized') if 'dbConn' in moduleContext: dbConn = moduleContext['dbConn'] else: raise error.SnmpsimError('variation module not initialized') if 'started' not in moduleContext: moduleContext['started'] = time.time() redisScript = moduleContext.get('evalsha') keySpace = '%.10d' % (moduleContext['key-spaces-id'] + moduleContext.get('iterations', 0)) if context['stopFlag']: dbConn.sort(keySpace + '-' + 'temp_oids_ordering', store=keySpace + '-' + 'oids_ordering', alpha=True) dbConn.delete(keySpace + '-' + 'temp_oids_ordering') dbConn.rpush(moduleContext['key-spaces-id'], keySpace) log.msg('redis: done with key-space %s' % keySpace) if 'iterations' in moduleContext and moduleContext['iterations']: log.msg('redis: %s iterations remaining' % moduleContext['iterations']) moduleContext['started'] = time.time() moduleContext['iterations'] -= 1 runtime = time.time() - moduleContext['started'] wait = max(0, moduleContext['period'] - runtime) raise error.MoreDataNotification(period=wait) else: raise error.NoDataNotification() dbOid = '.'.join(['%10s' % x for x in oid.split('.')]) if 'hexvalue' in context: textTag = context['hextag'] textValue = context['hexvalue'] else: textTag = SnmprecGrammar().getTagByType(context['origValue']) textValue = str(context['origValue']) dbConn.lpush(keySpace + '-temp_oids_ordering', keySpace + '-' + dbOid) if redisScript: dbConn.evalsha(redisScript, 1, keySpace + '-' + dbOid, textTag + '|' + textValue) else: dbConn.set(keySpace + '-' + dbOid, textTag + '|' + textValue) if not context['count']: settings = {'key-spaces-id': moduleContext['key-spaces-id']} 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']), ':redis', value else: raise error.NoDataNotification()
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) v = SnmprecGrammar.tagMap[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.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 record(oid, tag, value, **context): if 'started' not in moduleContext: moduleContext['started'] = time.time() if 'iterations' not in moduleContext: moduleContext['iterations'] = min( 1, moduleContext['settings'].get('iterations', 0)) # single-run recording if 'iterations' not in moduleContext[ 'settings'] or not moduleContext['settings']['iterations']: if context['origValue'].tagSet not in (rfc1902.Counter32.tagSet, rfc1902.Counter64.tagSet, rfc1902.TimeTicks.tagSet, rfc1902.Gauge32.tagSet, rfc1902.Integer.tagSet): if 'hextag' in context: tag = context['hextag'] if 'hexvalue' in context: value = context['hexvalue'] return oid, tag, value if 'taglist' not in moduleContext['settings'] or \ tag not in moduleContext['settings']['taglist']: return oid, tag, value value = 'initial=%s' % value if context['origValue'].tagSet == rfc1902.TimeTicks.tagSet: value += ',rate=100' elif context['origValue'].tagSet == rfc1902.Integer.tagSet: value += ',rate=0' return oid, tag + ':numeric', value # multiple-iteration recording if oid not in moduleContext: settings = {'initial': value} if context['origValue'].tagSet == rfc1902.TimeTicks.tagSet: settings['rate'] = 100 elif context['origValue'].tagSet == rfc1902.Integer.tagSet: settings['rate'] = 0 # may be constants if 'addon' in moduleContext['settings']: settings.update( dict([ split(x, '=') for x in moduleContext['settings']['addon'] ])) moduleContext[oid] = {} moduleContext[oid]['settings'] = settings if moduleContext['iterations']: if context['stopFlag']: # switching to final iteration log.msg('numeric: %s iterations remaining' % moduleContext['settings']['iterations']) moduleContext['iterations'] -= 1 moduleContext['started'] = time.time() wait = max( 0, float(moduleContext['settings']['period']) - (time.time() - moduleContext['started'])) raise error.MoreDataNotification(period=wait) else: # storing values on first iteration moduleContext[oid]['time'] = time.time() moduleContext[oid]['value'] = context['origValue'] if 'hexvalue' in moduleContext[oid]: moduleContext[oid]['hexvalue'] = context['hexvalue'] if 'hextag' in moduleContext[oid]: moduleContext[oid]['hextag'] = context['hextag'] raise error.NoDataNotification() else: if context['stopFlag']: raise error.NoDataNotification() if 'value' in moduleContext[oid]: if context['origValue'].tagSet not in (rfc1902.Counter32.tagSet, rfc1902.Counter64.tagSet, rfc1902.TimeTicks.tagSet, rfc1902.Gauge32.tagSet, rfc1902.Integer.tagSet): if 'hextag' in moduleContext[oid]: tag = moduleContext[oid]['hextag'] if 'hexvalue' in moduleContext[oid]: value = moduleContext[oid]['hexvalue'] return oid, tag, value if tag not in moduleContext['settings']['taglist']: return oid, tag, moduleContext[oid]['value'] moduleContext[oid]['settings']['rate'] = (int( context['origValue']) - int(moduleContext[oid]['value'])) / ( time.time() - moduleContext[oid]['time']) tag += ':numeric' value = ','.join([ '%s=%s' % (k, v) for k, v in moduleContext[oid]['settings'].items() ]) return oid, tag, value else: raise error.NoDataNotification()