def test_rate(self): """ Verify that rates are calculated and that the metric name has "_rate" appended to it """ setup_config(self.writer, CONFIG_RATE) values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868136, 10.0, [10]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals( result, [] ) # First value so can't calculate rate so no data is sent to Kairos values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # Verify that the original metric is not sent self.assertMetric(result[0], "collectd.cpu.0.cpu.softirq.value_rate", 1.0) values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868138, 10.0, [13]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # Verify that the original metric is not sent self.assertMetric(result[0], "collectd.cpu.0.cpu.softirq.value_rate", 2.0) # Not in the rate regex values = Values('load', '', 'load_type', '', 'localhost', 1442868138, 10.0, [13, 15, 20]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals( len(result), 3) # Verify that the original metric is sent (not a rate) self.assertEquals(result[0]['name'], "collectd.load_type.load.shortterm") self.assertEquals(result[0]['datapoints'][0][1], 13) self.assertEquals(result[1]['datapoints'][0][1], 15) self.assertEquals(result[2]['datapoints'][0][1], 20)
def test_not_in_typesDB(self): """ Verify that the type for the metric name does not exist in Types.db """ setup_config(self.writer, CONFIG_DEFAULT) values = Values('foo', 'softirq', 'MycpuMetric', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) self.assertIsNone(self.server.get_data())
def test_rate_zero_time_difference(self): """ Verify that rates that have zero time difference don't get reported (prevents divide by zero error) """ setup_config(self.writer, CONFIG_RATE) values = Values('mysql_handler', '', 'mysql_handler', '', 'localhost', 1442868136, 10.0, [10]) self.writer.kairosdb_write(values, collectd.get_data()) result = self.server.get_data() self.assertEquals(result, None) # First value so can't calculate rate so no data is sent to Kairos values = Values('mysql_handler', '', 'mysql_handler', '', 'localhost', 1442868136, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = self.server.get_data() self.assertEquals(result, None)
def test_rate_multiple_values(self): """ Verify that rates are calculated and that multiple metrics are sent """ setup_config(self.writer, CONFIG_RATE) values = Values('if_packets', 'eth0', 'interface', '', 'localhost', 1442868136, 10.0, [10, 11]) self.writer.kairosdb_write(values, collectd.get_data()) result = self.server.get_data() self.assertEquals(result, None) # First value so can't calculate rate so no data is sent to Kairos values = Values('if_packets', 'eth0', 'interface', '', 'localhost', 1442868137, 10.0, [11, 13]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertMetric(result[0], "collectd.interface.if_packets.eth0.rx_rate", 1.0)
def test_rate(self): """ Verify that rates are calculated and that the metric name has "_rate" appended to it """ setup_config(self.writer, CONFIG_RATE) values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868136, 10.0, [10]) self.writer.kairosdb_write(values, collectd.get_data()) result = self.server.get_data() self.assertEquals(result, None) # First value so can't calculate rate so no data is sent to Kairos values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # Verify that the original metric is not sent self.assertMetric(result[0], "collectd.cpu.0.cpu.softirq.value_rate", 1.0) values = Values('cpu', 'softirq', 'cpu', '0', 'localhost', 1442868138, 10.0, [13]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # Verify that the original metric is not sent self.assertMetric(result[0], "collectd.cpu.0.cpu.softirq.value_rate", 2.0) # Not in the rate regex values = Values('load', '', 'load_type', '', 'localhost', 1442868138, 10.0, [13, 15, 20]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 3) # Verify that the original metric is sent (not a rate) self.assertEquals(result[0]['name'], "collectd.load_type.load.shortterm") self.assertEquals(result[0]['datapoints'][0][1], 13) self.assertEquals(result[1]['datapoints'][0][1], 15) self.assertEquals(result[2]['datapoints'][0][1], 20)
def test_rate_zero_time_difference(self): """ Verify that rates that have zero time difference don't get reported (prevents divide by zero error) """ setup_config(self.writer, CONFIG_RATE) values = Values('mysql_handler', '', 'mysql_handler', '', 'localhost', 1442868136, 10.0, [10]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals( result, [] ) # First value so can't calculate rate so no data is sent to Kairos values = Values('mysql_handler', '', 'mysql_handler', '', 'localhost', 1442868136, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(result, [])
def test_rate_multiple_values(self): """ Verify that rates are calculated and that multiple metrics are sent """ setup_config(self.writer, CONFIG_RATE) values = Values('if_packets', 'eth0', 'interface', '', 'localhost', 1442868136, 10.0, [10, 11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals( result, [] ) # First value so can't calculate rate so no data is sent to Kairos values = Values('if_packets', 'eth0', 'interface', '', 'localhost', 1442868137, 10.0, [11, 13]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertMetric(result[0], "collectd.interface.if_packets.eth0.rx_rate", 1.0)
def test_rate_mixed_types(self): """ Verify that non-counter metrics are sent and not filtered """ setup_config(self.writer, CONFIG_RATE) values = Values('mysql_qcache', '', 'mysql_qcache', '', 'localhost', 1442868136, 10.0, [10, 11, 12, 13, 14]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # There is one non-counter value self.assertMetric( result[0], "collectd.mysql_qcache.mysql_qcache.queries_in_cache", 14) values = Values('mysql_qcache', '', 'mysql_qcache', '', 'localhost', 1442868137, 10.0, [11, 13, 14, 15, 16]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 5) self.assertMetric(result[0], "collectd.mysql_qcache.mysql_qcache.hits_rate", 1.0) self.assertMetric(result[1], "collectd.mysql_qcache.mysql_qcache.inserts_rate", 2.0) self.assertMetric( result[2], "collectd.mysql_qcache.mysql_qcache.not_cached_rate", 2.0) self.assertMetric( result[3], "collectd.mysql_qcache.mysql_qcache.lowmem_prunes_rate", 2.0) self.assertMetric( result[4], "collectd.mysql_qcache.mysql_qcache.queries_in_cache", 16) # Not a rate
def test_plugin_formatter(self): """ Verify that plugin formatter is called """ setup_config(self.writer, CONFIG_WITH_FORMATTER) values = Values('cpu', 'softirq', 'a', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(result[0]['name'], "metric1Formatter.value") self.assertEquals(result[0]['datapoints'][0][0], 1442868137000) self.assertEquals(result[0]['datapoints'][0][1], 11) self.assertEquals(result[0]['tags']["tag1"], "a") self.assertEquals(result[0]['tags']["tag2"], "b")
def test_default_formatter(self): """ Verify that default formatter is called for all plugins that have no plugin formatters """ setup_config(self.writer, CONFIG_WITH_FORMATTER) values = Values('cpu', 'softirq', 'MycpuMetric', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(result[0]['name'], "defaultFormatterMetric.value") self.assertEquals(result[0]['datapoints'][0][0], 1442868137000) self.assertEquals(result[0]['datapoints'][0][1], 11) self.assertEquals(result[0]['tags']["df1"], "a") self.assertEquals(result[0]['tags']["df2"], "b")
def test_rate_mixed_types(self): """ Verify that non-counter metrics are sent and not filtered """ setup_config(self.writer, CONFIG_RATE) values = Values('mysql_qcache', '', 'mysql_qcache', '', 'localhost', 1442868136, 10.0, [10, 11, 12, 13, 14]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 1) # There is one non-counter value self.assertMetric(result[0], "collectd.mysql_qcache.mysql_qcache.queries_in_cache", 14) values = Values('mysql_qcache', '', 'mysql_qcache', '', 'localhost', 1442868137, 10.0, [11, 13, 14, 15, 16]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(len(result), 5) self.assertMetric(result[0], "collectd.mysql_qcache.mysql_qcache.hits_rate", 1.0) self.assertMetric(result[1], "collectd.mysql_qcache.mysql_qcache.inserts_rate", 2.0) self.assertMetric(result[2], "collectd.mysql_qcache.mysql_qcache.not_cached_rate", 2.0) self.assertMetric(result[3], "collectd.mysql_qcache.mysql_qcache.lowmem_prunes_rate", 2.0) self.assertMetric(result[4], "collectd.mysql_qcache.mysql_qcache.queries_in_cache", 16) # Not a rate
def test_basic(self): """ Verify that the correct values are sent for a metric """ setup_config(self.writer, CONFIG_DEFAULT) values = Values('cpu', 'softirq', 'MycpuMetric', '0', 'localhost', 1442868137, 10.0, [11]) self.writer.kairosdb_write(values, collectd.get_data()) result = json.loads(self.server.get_data()) self.assertEquals(result[0]['name'], "collectd.MycpuMetric.0.cpu.softirq.value") self.assertEquals(result[0]['datapoints'][0][0], 1442868137000) self.assertEquals(result[0]['datapoints'][0][1], 11) self.assertEquals(result[0]['tags']["host"], "localhost") self.assertEquals(result[0]['tags']["role"], "web01") self.assertEquals(result[0]['tags']["environment"], "lab")