def test_opentsdb_walk_metric_with_ranges(self): mdata = self.walkmetric mdata["min_val"] = 10 mdata["max_val"] = 19 mdata["device"] = self.mockdevice m = Metric(**mdata) walkdata = m._get_walk(self.mockdevice.snmp) result = m._process_walk_data(walkdata, self.time) eq_(1, len(result))
def test_opentsdb_walk_metric_with_ignore_zeros(self): mdata = self.walkmetric mdata["ignore_zeros"] = True mdata["device"] = self.mockdevice m = Metric(**mdata) walkdata = m._get_walk(self.mockdevice.snmp) result = m._process_walk_data(walkdata, self.time) eq_(2, len(result))
def test_opentsdb_get_metric(self, mocktime): mocktime.return_value = self.time mdata = { 'metric': 'cpmCPUTotal5minRev', 'oid': '.1.3.6.1.4.1.9.9.109.1.1.1.1.8', 'type': 'get', 'tags': {}, 'device': self.mockdevice, } m = Metric(**mdata) result = m.get_opentsdb_commands(self.mockdevice.snmp, self.time)[0] eq_("put cpmCPUTotal5minRev " + str(int(self.time)) + " 123 host=foo", result)
def __init__(self, data, resolvers, mods, metrics): self.hostname = data["hostname"] self.community = data["community"] self.snmp_version = data["snmp_version"] if "snmp_timeout" in data: self.snmp_timeout = data["snmp_timeout"] else: self.snmp_timeout = 2000000 if "snmp_retries" in data: self.snmp_retries = data["snmp_retries"] else: self.snmp_retries = 0 if "snmp_max_repetitions" in data: self.snmp_max_repetitions = data["snmp_max_repetitions"] else: self.snmp_max_repetitions = 49 self.metrics = [] self.resolvers = resolvers self.value_modifiers = mods for m in data["metrics"]: logging.info("trying to initialize metric %s", m) if not m in metrics: logging.info("WARNING: can't find metric %s for %s", m, self.hostname) continue logging.info("found metric %s", m) try: metric = Metric(device=self, **metrics[m]) except Exception as e: logging.info("Exception while initializing metric: %s", e) logging.info("Initialized metric %s", metric.name) self.metrics.append(metric)
def test_opentsdb_get_metric(self, mocktime): mocktime.return_value = self.time mdata = { 'metric': 'cpmCPUTotal5minRev', 'oid': '.1.3.6.1.4.1.9.9.109.1.1.1.1.8', 'type': 'get', 'tags': {}, } m = Metric( data=mdata, device=self.mockdevice ) result = m.get_opentsdb_commands()[0] eq_( "put cpmCPUTotal5minRev " + str(int(self.time)) + " 123 host=foo", result )
def test_opentsdb_walk_metric(self, mocktime): mocktime.return_value = self.time mdata = { 'metric': 'interface.packets', 'oid': '.1.3.6.1.2.1.31.1.1.1.9', 'type': 'walk', 'tags': { 'direction': "in", 'type': 'broadcast' }, 'resolver': 'cisco_ifname' } m = Metric( data=mdata, device=self.mockdevice ) #test _tags_to_str with empty tags eq_("", m._tags_to_str({})) walkdata = m._get_walk() eq_(10, walkdata["1"]) eq_(20, walkdata["2"]) eq_( "put interface.packets " + str(int(self.time)) + " 20 index=2 direction=in type=broadcast host=foo", m._process_dp(20, 2) ) result = m._process_walk_data(walkdata) eq_(2, len(result)) eq_( 'put interface.packets ' + str(int(self.time)) + ' 10 index=1 direction=in type=broadcast host=foo', result[0] ) result = m.get_opentsdb_commands() eq_(2, len(result)) eq_( 'put interface.packets ' + str(int(self.time)) + ' 10 index=1 direction=in type=broadcast host=foo', result[0] )
def test_opentsdb_walk_metric(self, mocktime): mocktime.return_value = self.time metric = self.walkmetric metric["device"] = self.mockdevice m = Metric(**metric) #test _tags_to_str with empty tags eq_("", m._tags_to_str({})) walkdata = m._get_walk(self.mockdevice.snmp) eq_(10, walkdata["1"]) eq_(20, walkdata["2"]) eq_("0", walkdata["3"]) eq_( "put interface.packets.in.foo " + str(int(self.time)) + " 2.0 index=2 type=broadcast", m._process_dp(20, self.time, key=2)) result = m._process_walk_data(walkdata, self.time) eq_(3, len(result)) eq_( 'put interface.packets.in.foo ' + str(int(self.time)) + ' 1.0 index=1 type=broadcast', result[0]) result = m.get_opentsdb_commands(self.mockdevice.snmp, self.time) eq_(3, len(result)) eq_( 'put interface.packets.in.foo ' + str(int(self.time)) + ' 1.0 index=1 type=broadcast', result[0])