def check(self, data, server): process_data_dict = data.get('data', None) rules = alerts_model.get_alerts(type='process', server=server) if len(rules) + len(process_data_dict) > 0: for rule in rules: process_id = rule['process'] self.check_rule_and_save(process_id=process_id, rule=rule, process_data_dict=process_data_dict, server_id=server['_id']) # Global alerts rules = alerts_model.get_alerts(type='process_global') if len(rules) + len(process_data_dict) > 0: all_processes = process_model.get_all_for_server(server['_id']) for rule in rules: valid_rule = self.check_tags(server=server, rule=rule) if valid_rule: process_name = rule.get('process') process_id = None # Check if this server has a process with this name for p in all_processes.clone(): if p.get('name') == process_name: process_id = p.get('_id') if process_id: self.check_rule_and_save( process_id=process_id, rule=rule, process_data_dict=process_data_dict, server_id=server['_id'])
def check(self, data, server): process_data_dict = data.get('data', None) rules = alerts_model.get_alerts(type='process', server=server) if len(rules) + len(process_data_dict) > 0: for rule in rules: process_id = rule['process'] self.check_rule_and_save(process_id=process_id, rule=rule, process_data_dict=process_data_dict, server_id=server['_id']) # Global alerts rules = alerts_model.get_alerts(type='process_global') if len(rules) + len(process_data_dict) > 0: all_processes = process_model.get_all_for_server(server['_id']) for rule in rules: valid_rule = self.check_tags(server=server, rule=rule) if valid_rule: process_name = rule.get('process') process_id = None # Check if this server has a process with this name for p in all_processes.clone(): if p.get('name') == process_name: process_id = p.get('_id') if process_id: self.check_rule_and_save(process_id=process_id, rule=rule, process_data_dict=process_data_dict, server_id=server['_id'])
def all(request): all_servers = server_model.get_all(account_id=request.account_id) alerts = [] if all_servers: for server in all_servers: types = ['system', 'process', 'uptime', 'plugin'] for alert_type in types: result = alerts_model.get_alerts(type=alert_type, server=server) if result: [alerts.append(x) for x in result] global_alerts = alerts_model.get_global_alerts_with_notifications(all_servers=all_servers, account_id=request.account_id, include_all_types=True) if global_alerts: [alerts.append(x) for x in global_alerts] global_health_check_alerts = alerts_model.get_alerts(type='health_check') if global_health_check_alerts: [alerts.append(x) for x in global_health_check_alerts] return render(request, 'alerts/all.html', { "alerts": alerts, "all_servers": all_servers, "server_metrics": settings.SERVER_METRICS, "common_metrics": settings.COMMON_METRICS, "total_alerts": len(alerts), })
def check(self, data=None, plugin=None, server=None): plugin_data = data.get('gauges', None) rules = alerts_model.get_alerts_for_plugin(plugin=plugin) if len(rules) > 0: for rule in rules: alert = plugin_alerts.check(data=plugin_data, rule=rule) if alert: alerts_model.save_occurence(alert) # Global alerts rules = alerts_model.get_alerts(type='plugin_global') if len(rules) > 0: all_plugins = plugin_model.get_for_server(server_id=server['_id']) for rule in rules: valid_rule = self.check_tags(server=server, rule=rule) if valid_rule: plugin_name = rule.get('plugin') plugin_id = None # Check if this server has a plugin with this name for p in all_plugins.clone(): if p.get('name') == plugin_name: plugin_id = p.get('_id') if plugin_id: alert = plugin_alerts.check(data=plugin_data, rule=rule) if alert: alerts_model.save_occurence( alert, server_id=server['_id'])
def check(self, data=None, plugin=None, server=None): plugin_data = data.get('gauges', None) rules = alerts_model.get_alerts_for_plugin(plugin=plugin) if len(rules) > 0: for rule in rules: alert = plugin_alerts.check(data=plugin_data, rule=rule) if alert: alerts_model.save_occurence(alert) # Global alerts rules = alerts_model.get_alerts(type='plugin_global') if len(rules) > 0: all_plugins = plugin_model.get_for_server(server_id=server['_id']) for rule in rules: valid_rule = self.check_tags(server=server, rule=rule) if valid_rule: plugin_name = rule.get('plugin') plugin_id = None # Check if this server has a plugin with this name for p in all_plugins.clone(): if p.get('name') == plugin_name: plugin_id = p.get('_id') if plugin_id: alert = plugin_alerts.check(data=plugin_data, rule=rule) if alert: alerts_model.save_occurence(alert, server_id=server['_id'])
def test_system_check(self): self._cleanup() # System alert system_alert = { "above_below": "above", "rule_type": "system", "server": self.server_id, "account_id": self.account_id, "period": 0, } # CPU alert cpu_alert_dict = {**system_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%"} alert_id = alerts_model.collection.insert(cpu_alert_dict) rules = alerts_model.get_alerts(type='system', server=self.server) eq_(len(rules), 1) data = {u'cpu': {u'system': u'1.30', u'idle': u'98.70', u'user': u'0.00', u'steal': u'0.00', u'nice': u'0.00'}} server_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent(server_id=self.server_id) for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id eq_(unsent_alerts['data'].count(), 1) self._cleanup()
def check(self, data=None, server=None): alerts = alerts_model.get_alerts(type='health_check') for alert in alerts: # Data is list for d in data: trigger = healthcheck_alert_checker.check(data=d, rule=alert) # Will scan all the data, check for relevancy and then check the specific entry if trigger: alerts_model.save_healtcheck_occurence(trigger=trigger, server_id=server['_id'])
def check(self, data=None, server=None): alerts = alerts_model.get_alerts(type='health_check') for alert in alerts: # Data is list for d in data: trigger = healthcheck_alert_checker.check(data=d, rule=alert) # Will scan all the data, check for relevancy and then check the specific entry if trigger: alerts_model.save_healtcheck_occurence( trigger=trigger, server_id=server['_id'])
def check(self, data, server): process_data_dict = data.get('data', None) rules = alerts_model.get_alerts(type='uptime', server=server) if len(rules) + len(process_data_dict) > 0: for rule in rules: process_id = rule['process'] process_data = next((item for item in process_data_dict if item["p"] == process_id), None) # Process is down if not process_data: alerts_model.save_uptime_occurence(rule, data=process_data)
def check(self, data, server): process_data_dict = data.get('data', None) rules = alerts_model.get_alerts(type='uptime', server=server) if len(rules) + len(process_data_dict) > 0: for rule in rules: process_id = rule['process'] process_data = next( (item for item in process_data_dict if item["p"] == process_id), None) # Process is down if not process_data: alerts_model.save_uptime_occurence(rule, data=process_data)
def test_system_check(self): self._cleanup() # System alert system_alert = { "above_below": "above", "rule_type": "system", "server": self.server_id, "account_id": self.account_id, "period": 0, } # CPU alert cpu_alert_dict = { **system_alert, 'metric': 'CPU', 'metric_value': 1, 'metric_type': "%" } alert_id = alerts_model.collection.insert(cpu_alert_dict) rules = alerts_model.get_alerts(type='system', server=self.server) eq_(len(rules), 1) data = { u'cpu': { u'system': u'1.30', u'idle': u'98.70', u'user': u'0.00', u'steal': u'0.00', u'nice': u'0.00' } } server_alerter.check(data, self.server) unsent_alerts = alerts_history_model.get_unsent( server_id=self.server_id) for trigger in unsent_alerts['data']: assert trigger['alert_id'] == alert_id eq_(unsent_alerts['data'].count(), 1) self._cleanup()
def check(self, data, server): alerts = False account_id = server.get('account_id', None) # System alerts rules = alerts_model.get_alerts(type='system', server=server) if rules: alerts = system_alerts.check(data=data, rules=rules, server=server) if alerts: alerts_model.save_system_occurence(alerts, server_id=server['_id']) # Global rules global_rules = alerts_model.get_global_alerts(account_id=account_id) if global_rules: alerts = system_alerts.check(data=data, rules=global_rules, server=server) if alerts: alerts_model.save_system_occurence(alerts, server_id=server['_id']) return alerts # For the test suite