def test_multiple_local_monitorings(self): config1 = {'metrics': ['mem', 'engine-loop']} config2 = {'metrics': ['cpu', 'mem']} obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({'local': [config1, config2]}) obj.prepare() self.assertEqual(1, len(obj.clients)) self.assertEqual({'mem', 'cpu', 'engine-loop'}, set(obj.clients[0].metrics)) self.assertTrue(isinstance(obj.clients[0].monitor, LocalMonitor)) data1 = obj.clients[0].get_data() data2 = obj.clients[0].get_data() for item1, item2 in zip(data1, data2): self.assertEqual(item1, item2) metrics = [] for element in data1: self.assertIn('source', element) self.assertIn('ts', element) for key in element: if key not in ('source', 'ts'): metrics.append(key) for config in (config1, config2): self.assertTrue(all(m in metrics for m in config['metrics']))
def test_multiple_local_monitorings(self): config1 = {'metrics': ['mem', 'engine-loop']} config2 = {'metrics': ['cpu', 'mem']} obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ 'local': [config1, config2]}) obj.prepare() self.assertEqual(1, len(obj.clients)) self.assertEqual({'mem', 'cpu', 'engine-loop'}, set(obj.clients[0].metrics)) self.assertTrue(isinstance(obj.clients[0].monitor, LocalMonitor)) data1 = obj.clients[0].get_data() data2 = obj.clients[0].get_data() for item1, item2 in zip(data1, data2): self.assertEqual(item1, item2) metrics = [] for element in data1: self.assertIn('source', element) self.assertIn('ts', element) for key in element: if key not in ('source', 'ts'): metrics.append(key) for config in (config1, config2): self.assertTrue(all(m in metrics for m in config['metrics']))
def test_server_agent_encoding(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "server-agent": [{ "address": "127.0.0.1:4444", "metrics": [ "cpu", "disks" ] }] }) obj.client_classes = {'server-agent': ServerAgentClientEmul} obj.prepare() self.assertEquals(b("test\n"), obj.clients[0].socket.sent_data)
def test_server_agent_encoding(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "server-agent": [{ "address": "127.0.0.1:4444", "metrics": ["cpu", "disks"] }] }) obj.client_classes = {'server-agent': ServerAgentClientEmul} obj.prepare() self.assertEquals(b("test\n"), obj.clients[0].socket.sent_data)
def test_simple(self): obj = Monitoring() obj.server_agent_class = ServerAgentClientEmul obj.engine = EngineEmul() obj.parameters.merge({ "server-agents": { "127.0.0.1:4444": { "metrics": [ "cpu", "disks" ] } } }) listener = LoggingMonListener() obj.add_listener(listener) widget = obj.get_widget() obj.add_listener(widget) crit_conf = BetterDict() crit_conf.merge({"threshold": 5, "subject": "127.0.0.1:4444/cpu"}) criteria = MonitoringCriteria(crit_conf, obj) obj.add_listener(criteria) obj.prepare() obj.startup() for _ in range(1, 10): obj.clients[0].socket.recv_data += "%s\t%s\n" % (random.random(), random.random()) obj.check() logging.debug("Criteria state: %s", criteria) time.sleep(1) obj.shutdown() obj.post_process() self.assertEquals("test\ninterval:1\nmetrics:cpu\tdisks\nexit\n", obj.clients[0].socket.sent_data)
def test_graphite(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "graphite": [{ "address": "people.com:1066", "label": "Earth", "metrics": ["body", "brain"] }, { "address": "http://spirits.net", "metrics": ["transparency", "usability"] }] }) obj.client_classes = {'graphite': GraphiteClientEmul} obj.prepare() obj.startup() obj.check() obj.clients[0]._last_check -= obj.clients[0].interval * 2 obj.check() obj.clients[0]._last_check -= obj.clients[0].interval * 2 obj.clients[0].prepared_data = "wrong data" obj.check() obj.shutdown() obj.post_process()
def test_server_agent(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "server-agent": [{ "address": "127.0.0.1:4444", "metrics": ["cpu", "disks"] }, { "address": "10.0.0.1", "metrics": ["something1", "something2"] }] }) listener = LoggingMonListener() obj.add_listener(listener) widget = obj.get_widget() obj.add_listener(widget) crit_conf = BetterDict.from_dict({ "threshold": 5, "subject": "127.0.0.1:4444/cpu" }) criteria = MonitoringCriteria(crit_conf, obj) obj.add_listener(criteria) obj.client_classes = {'server-agent': ServerAgentClientEmul} obj.prepare() obj.startup() for _ in range(1, 10): obj.clients[0].socket.recv_data += b( "%s\t%s\n" % (random.random(), random.random())) obj.check() ROOT_LOGGER.debug("Criteria state: %s", criteria) time.sleep(obj.engine.check_interval) obj.shutdown() obj.post_process() self.assertEquals(b("test\ninterval:1\nmetrics:cpu\tdisks\nexit\n"), obj.clients[0].socket.sent_data)
def test_graphite(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "graphite": [{ "address": "people.com:1066", "label": "Earth", "logging": True, "metrics": [ "body", "brain"]}, { "address": "http://spirits.net", "metrics": [ "transparency", "usability" ]}] }) obj.client_classes = {'graphite': GraphiteClientEmul} obj.prepare() obj.startup() obj.check() obj.clients[0]._last_check -= obj.clients[0].interval * 2 obj.check() obj.clients[0]._last_check -= obj.clients[0].interval * 2 obj.clients[0].prepared_data = "wrong data" obj.check() obj.shutdown() obj.post_process() if PY3: self.assertIsNotNone(obj.clients[0].logs_file) self.assertIsNone(obj.clients[1].logs_file) with open(obj.clients[0].logs_file) as graphite_logs: logs_reader = csv.reader(graphite_logs) logs_reader = list(logs_reader) self.assertEquals(['ts', 'body', 'brain'], logs_reader[0]) self.assertEquals(['2'], logs_reader[1][1:])
def test_server_agent(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "server-agent": [{ "address": "127.0.0.1:4444", "logging": "True", "metrics": [ "cpu", "disks" ] }, { "address": "10.0.0.1", "metrics": [ "something1", "something2" ] }] }) listener = LoggingMonListener() obj.add_listener(listener) widget = obj.get_widget() obj.add_listener(widget) crit_conf = BetterDict.from_dict({"condition": ">", "threshold": 5, "subject": "127.0.0.1:4444/cpu"}) criteria = MonitoringCriteria(crit_conf, obj) obj.add_listener(criteria) obj.client_classes = {'server-agent': ServerAgentClientEmul} obj.prepare() obj.startup() for i in range(1, 10): obj.clients[0].socket.recv_data += b("%s\t%s\t\n" % (i, i*10)) obj.check() ROOT_LOGGER.debug("Criteria state: %s", criteria) time.sleep(obj.engine.check_interval) obj.shutdown() obj.post_process() self.assertEquals(b("test\ninterval:1\nmetrics:cpu\tdisks\nexit\n"), obj.clients[0].socket.sent_data) if PY3: self.assertIsNotNone(obj.clients[0].logs_file) with open(obj.clients[0].logs_file) as serveragent_logs: logs_reader = csv.reader(serveragent_logs) logs_reader = list(logs_reader) self.assertEquals(['ts', 'cpu', 'disks'], logs_reader[0]) for i in range(1, 10): self.assertEquals([str(i), str(i * 10)], logs_reader[i][1:])
def test_graphite(self): obj = Monitoring() obj.engine = EngineEmul() obj.parameters.merge({ "graphite": [{ "address": "people.com:1066", "label": "Earth", "metrics": [ "body", "brain"]}, { "address": "http://spirits.net", "metrics": [ "transparency", "usability" ]}] }) obj.client_classes = {'graphite': GraphiteClientEmul} obj.prepare() obj.startup() obj.check() obj.clients[0].check_time -= obj.clients[0].interval * 2 obj.check() obj.clients[0].check_time -= obj.clients[0].interval * 2 obj.clients[0].prepared_data = "wrong data" obj.check() obj.shutdown() obj.post_process()
def test_simple(self): obj = Monitoring() obj.server_agent_class = ServerAgentClientEmul obj.engine = EngineEmul() obj.parameters.merge({ "server-agents": { "127.0.0.1:4444": { "metrics": ["cpu", "disks"] } } }) listener = LoggingMonListener() obj.add_listener(listener) widget = obj.get_widget() obj.add_listener(widget) crit_conf = BetterDict() crit_conf.merge({"threshold": 5, "subject": "127.0.0.1:4444/cpu"}) criteria = MonitoringCriteria(crit_conf, obj) obj.add_listener(criteria) obj.prepare() obj.startup() for _ in range(1, 10): obj.clients[0].socket.recv_data += "%s\t%s\n" % (random.random(), random.random()) obj.check() logging.debug("Criteria state: %s", criteria) time.sleep(1) obj.shutdown() obj.post_process() self.assertEquals("test\ninterval:1\nmetrics:cpu\tdisks\nexit\n", obj.clients[0].socket.sent_data)