def get_or_create_test(self): self._cleanup() process_model.get_or_create(server_id=self.server_id, name='test') total_devices = process_model.collection.find().count() eq_(total_devices, 1) process = process_model.get_or_create(server_id=self.server_id, name='test') eq_(process['name'], 'test') eq_(process['server'], self.server_id) total_devices = process_model.collection.find().count() eq_(total_devices, 1) self._cleanup()
def setUp(self): self.account_id = 1 server_key = server_model.add('testserver', account_id=self.account_id) self.server = server_model.get_server_by_key(server_key) self.server_id = self.server['_id'] self.process = process_model.get_or_create(server_id=self.server_id, name='testprocess') self.process_id = self.process['_id']
def test_process_alert(self): self._cleanup() process_alert = { "above_below": "above", "rule_type": "process", "server": self.server_id, "process": self.process_id, "account_id": self.account_id, "period": 0, } cpu_alert_dict = {**process_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%"} alert_id = alerts_model.collection.insert(cpu_alert_dict) cpu_value = float(2) data = {'data': [{'p': self.process_id, 'c': cpu_value}]} process_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent() for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id assert trigger['average_value'] == cpu_value eq_(unsent_alerts['data'].count(), 1) self._cleanup() process_alert = { "above_below": "above", "rule_type": "process_global", "server": 'all', "process": 'mongo', "account_id": self.account_id, "period": 0, } process = process_model.get_or_create(server_id=self.server_id, name='mongo') global_process_id = process['_id'] cpu_alert_dict = {**process_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%"} alert_id = alerts_model.collection.insert(cpu_alert_dict) cpu_value = float(2) data = {'data': [{'p': global_process_id, 'c': cpu_value}]} process_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent() for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id assert trigger['average_value'] == cpu_value eq_(unsent_alerts['data'].count(), 1)
def get_or_create_test(self): self._cleanup() process_model.get_or_create(server_id=self.server_id, name='test') total_devices = process_model.collection.find().count() eq_(total_devices, 1) process = process_model.get_or_create(server_id=self.server_id, name='test') eq_(process['name'], 'test') eq_(process['server'], self.server_id) total_devices = process_model.collection.find().count() eq_(total_devices, 1) self._cleanup()
def setUp(self): User.objects.all().delete() self.alerter = server_alerter self.user = User.objects.create_user(password='******' , email='*****@*****.**') self.account_id = 1 self.server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(self.server_key) self.server_id = self.server['_id'] self.process = process_model.get_or_create(server_id=self.server_id, name='test') self.process_id = self.process['_id'] self.plugin = plugin_model.get_or_create(server_id=self.server_id, name='testplugin') self.plugin_id = self.plugin['_id'] self.gauge = plugin_model.get_or_create_gauge_by_name(plugin=self.plugin, name='gauge') self.gauge_id = self.gauge['_id']
def setUp(self): self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(server_key) self.server_id = self.server['_id'] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] self.emails = [x['email'] for x in notifications] self.process = process_model.get_or_create(server_id=self.server_id, name='testprocess') self.process_id = self.process['_id']
def setUp(self): User.objects.all().delete() self.alerter = server_alerter self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(self.server_key) self.server_id = self.server['_id'] self.process = process_model.get_or_create(server_id=self.server_id, name='test') self.process_id = self.process['_id'] self.plugin = plugin_model.get_or_create(server_id=self.server_id, name='testplugin') self.plugin_id = self.plugin['_id'] self.gauge = plugin_model.get_or_create_gauge_by_name( plugin=self.plugin, name='gauge') self.gauge_id = self.gauge['_id']
def setUp(self): self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(server_key) self.server_id = self.server['_id'] email_model.insert({'sent_from': '*****@*****.**'}) notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] self.emails = [x['email'] for x in notifications] self.process = process_model.get_or_create(server_id=self.server_id, name='testprocess') self.process_id = self.process['_id']
def get_global_metrics_test(self): self._cleanup() process_name = "alertest-process" plugin_name = "alertest-plugin" process = process_model.get_or_create(server_id=self.server_id, name=process_name) plugin = plugin_model.get_or_create(name=plugin_name, server_id=self.server_id) plugin_data = { 'count.count_first_key': 2, 'second.second_key': 4, 'more.more_key': 5, 'count.count_second_key': 4 } plugin_model.save_gauges(plugin=plugin, data=plugin_data, time=1) plugin_gauges_keys = plugin_model.get_gauge_keys_for_server(server_id=self.server_id) result = self.model.get_global_metrics() assert len(result) == 14 # 7 system + 3 process(cpu/memory/down) + 4 plugin(for every key) system_metrics = ['CPU', 'Memory', 'Loadavg', 'Disk', 'Network/inbound', 'Network/outbound', 'Not Sending Data'] process_metrics = ['cpu', 'memory', 'down'] for r in result: value = r.get('value') metric_dict = dict(v.split(":") for v in value.split(".")) assert metric_dict['rule_type'] assert metric_dict['rule_type'] in ['global', 'process_global', 'plugin_global'] alert_type = metric_dict.get('rule_type') if alert_type == 'system': assert r['metric'] in system_metrics if alert_type == 'process_global': assert r['metric'] in process_metrics
def get_server_metrics_test(self): self._cleanup() process_name = "alertest-process" plugin_name = "alertest-plugin" process = process_model.get_or_create(server_id=self.server_id, name=process_name) plugin = plugin_model.get_or_create(name=plugin_name, server_id=self.server_id) plugin_data = { 'count.count_first_key': 2, 'second.second_key': 4, 'more.more_key': 5, 'count.count_second_key': 4 } plugin_model.save_gauges(plugin=plugin, data=plugin_data, time=1) plugin_gauges_keys = plugin_model.get_gauge_keys_for_server(server_id=self.server_id) volumes_model.get_or_create(server_id=self.server_id, name='get_server_metrics_volume') interfaces_model.get_or_create(server_id=self.server_id, name='get_server_metrics_interface') result = self.model.get_server_metrics(server_id=self.server_id) system_metrics = ['CPU', 'Memory', 'Loadavg', 'Disk', 'Network/inbound', 'Network/outbound', 'Not Sending Data'] system_values = ["server:{0}.metric:{1}.rule_type:system".format(self.server_id, x.replace(" ", "")) for x in system_metrics] volumes = volumes_model.get_all_for_server(server_id=self.server_id) for v in volumes: value = "server:{0}.metric:Disk.rule_type:system.volume:{1}".format(self.server_id, v.get('name')) system_values.append(value) interfaces = interfaces_model.get_all_for_server(server_id=self.server_id) for i in interfaces: value = "server:{0}.metric:Network/inbound.rule_type:system.interface:{1}".format(self.server_id, i.get('name')) system_values.append(value) value = "server:{0}.metric:Network/outbound.rule_type:system.interface:{1}".format(self.server_id, i.get('name')) system_values.append(value) process_metrics = ['CPU', 'Memory'] process_alerts_names = ["{0}.{1}".format(process_name, x.replace(" ", "")) for x in process_metrics] process_values = ["server:{0}.process:{1}.metric:{2}.rule_type:process".format(self.server_id, process['_id'], x) for x in process_metrics] process_uptime = ['Down'] process_uptime_alerts_names = ["{0}.{1}".format(process_name, x.replace(" ", "")) for x in process_uptime] process_uptime_values = ["server:{0}.process:{1}.metric:{2}.rule_type:uptime".format(self.server_id, process['_id'], x) for x in process_uptime] plugin_alert_names = ["{0}.{1}".format(plugin_name, x) for x in plugin_data.keys()] plugin_values = [] for plugin_gauge_key in plugin_gauges_keys: gauge = plugin_gauge_key.get('gauge') key = plugin_gauge_key.get('key') _id = "server:{0}.plugin:{1}.gauge:{2}.key:{3}.rule_type:plugin".format( self.server_id, plugin['_id'], gauge['_id'], key ) plugin_values.append(_id) assert len(plugin_values) == 4 # All the keys from plugin_data for r in result: value = r.get('value') metric_dict = dict(v.split(":") for v in value.split(".")) alert_type = metric_dict.get('rule_type') assert alert_type != None assert alert_type in ['system', 'process', 'plugin', 'uptime'] if alert_type == 'system': assert r.get('metric') in system_metrics assert r.get('value') in system_values elif alert_type == 'process': assert r.get('metric') in ['CPU', 'Memory'] assert r.get('name') in process_alerts_names assert r.get('value') in process_values elif alert_type == 'uptime': assert r.get('metric') in ['Down'] assert r.get('name') in process_uptime_alerts_names assert r.get('value') in process_uptime_values elif alert_type == 'plugin': assert r.get('name') in plugin_alert_names assert r.get('value') in plugin_values else: assert False # Should not be here
def test_process_alert(self): self._cleanup() process_alert = { "above_below": "above", "rule_type": "process", "server": self.server_id, "process": self.process_id, "account_id": self.account_id, "period": 0, } cpu_alert_dict = { **process_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%" } alert_id = alerts_model.collection.insert(cpu_alert_dict) cpu_value = float(2) data = {'data': [{'p': self.process_id, 'c': cpu_value}]} process_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent() for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id assert trigger['average_value'] == cpu_value eq_(unsent_alerts['data'].count(), 1) self._cleanup() process_alert = { "above_below": "above", "rule_type": "process_global", "server": 'all', "process": 'mongo', "account_id": self.account_id, "period": 0, } process = process_model.get_or_create(server_id=self.server_id, name='mongo') global_process_id = process['_id'] cpu_alert_dict = { **process_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%" } alert_id = alerts_model.collection.insert(cpu_alert_dict) cpu_value = float(2) data = {'data': [{'p': global_process_id, 'c': cpu_value}]} process_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent() for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id assert trigger['average_value'] == cpu_value eq_(unsent_alerts['data'].count(), 1)