def get_notifications_list(self, alert_id=None, server=None, limit=0, skip=0): notifications_list = [] server_id = None if server: server_id = server.get('_id') # Could be none notifications = self.get_notifications(alert_id=alert_id, server_id=server_id, limit=limit, skip=skip) if notifications['count'] > 0: for notification in notifications['data']: notification_dict = { "period_from": notification['from'], "period_to": notification['time'], "average_value": notification['average_value'], "id": notification.get('_id') } # System notifications specific here server = server_model.get_by_id(notification.get('server_id')) if server: notification_dict['server_id'] = server['_id'] notification_dict['server'] = server['name'] notification_dict['last_check'] = server.get('last_check') volume = notification.get('volume') if volume: notification_dict['volume_data'] = volumes_model.get_by_id(volume) interface = notification.get('interface') if interface: notification_dict['interface_data'] = interfaces_model.get_by_id(interface) health_checks_data_id = notification.get("health_checks_data_id") if health_checks_data_id: health_check_data = health_checks_results_model.get_by_id(health_checks_data_id) if type(health_check_data) is dict: health_check_id = health_check_data.get('check_id') notification_dict['health_check'] = health_checks_model.get_by_id(health_check_id) notification_dict['health_check_data'] = health_check_data notifications_list.append(notification_dict) return notifications_list
def _fill_metrics_arrays(all_metrics=None): charts_list = [] health_checks_list = [] for m in all_metrics: metric_type = m.get('type') if metric_type == 'healthcheck': check_id = m.get('healthcheck_id') server = None if check_id: check = health_checks_model.get_by_id(check_id) server = server_model.get_by_id(check.get('server_id', None)) if server != None: check['server'] = server health_checks_list.append(check) else: charts_list.append(m) return {'charts': charts_list, 'health_checks': health_checks_list}
def _fill_metrics_arrays(all_metrics=None): charts_list = [] health_checks_list = [] for m in all_metrics: metric_type = m.get('type') if metric_type == 'healthcheck': check_id = m.get('healthcheck_id') server = None if check_id: check = health_checks_model.get_by_id(check_id) server = server_model.get_by_id(check.get('server_id', None)) if server != None: check['server'] = server health_checks_list.append(check) else: charts_list.append(m) return { 'charts': charts_list, 'health_checks': health_checks_list }
def generate_notifications(): notifications_list = [] unsent_alerts = alerts_history_model.get_unsent() for trigger in unsent_alerts.get('data'): result = AmonStruct() result.global_mute = False metadata = None timezone = 'UTC' try: alert = alerts_model.get_by_id(trigger['alert_id']) except: alert = None # Deleted alert here if alert: rule_type = alert.get('rule_type', 'system') metric_type = alert.get('metric', None) else: rule_type = 'alert-does-not-exist' if rule_type in ['global', 'process_global', 'plugin_global', 'process', 'system', 'plugin', 'uptime', 'health_check']: if rule_type in ['global', 'process_global', 'plugin_global', 'health_check']: server_id = trigger.get('server_id') else: server_id = alert.get('server') if server_id: server = server_model.get_by_id(server_id) result.server = server result.global_mute = alert_mute_servers_model.check_if_server_is_muted(server=server) if metric_type: metric_type = metric_type.lower() if metric_type in ['cpu', 'memory', 'loadavg']: trigger_period_from = trigger['from'] trigger_period_to = trigger['time'] metric_type = 'cpu' if metric_type == 'loadavg' else metric_type # Get CPU top consumers for Load average if server: metadata = process_model.get_top_consumers_for_period(date_from=trigger_period_from, date_to=trigger_period_to, server=server, metric_type=metric_type) # Overwrite rule_type for the new type if metric_type == 'notsendingdata': alert['rule_type'] = 'notsendingdata' if metric_type == 'disk': volume_id = trigger.get('volume') metadata = volumes_model.get_by_id(volume_id) if metric_type in ['network/inbound', 'network/outbound']: interface_id = trigger.get('interface') metadata = interfaces_model.get_by_id(interface_id) if rule_type == 'process_global': process_name = alert.get('process') result.process = process_model.get_by_name_and_server_id(server_id=server_id, name=process_name) if rule_type == 'plugin_global': gauge_name = alert.get('gauge') plugin_name = alert.get('plugin') result.plugin = plugin_model.get_by_name_and_server_id(server_id=server_id, name=plugin_name) result.gauge = plugin_model.get_gauge_by_name_and_plugin_id(plugin=result.plugin, name=gauge_name) # Process and Uptime alerts if rule_type == 'process' or rule_type == 'uptime': process_dict = alert.get('process') if process_dict: result.process = process_model.get_by_id(process_dict.get('_id')) if rule_type == 'plugin': result.plugin = alert.get('plugin') result.gauge = alert.get('gauge') if rule_type == 'health_check': health_check_result_id = trigger.get('health_checks_data_id') health_check_result = health_checks_results_model.get_by_id(health_check_result_id) if type(health_check_result) is dict: health_check_id = health_check_result.get('check_id') health_check = health_checks_model.get_by_id(health_check_id) result.healthcheck = health_check result.health_check_result = health_check_result if alert: result.alert = alert result.metadata = metadata result.timezone = timezone result.trigger = trigger result.mute = alert.get('mute', False) # Shortcut notifications_list.append(result) return notifications_list
def get_all(self, account_id=None, dashboard_id=None, public=None): result_list = [] query = [] params = {'dashboard_id': dashboard_id} params = self.keys_to_mongoid(data=params, keys=['dashboard_id']) if dashboard_id: query = super(DashboardMetricsModel, self).get(params=params) utc_now = unix_utc_now() for metric in query: mongo_id = metric.get('_id') server_id = metric.get('server_id') metric_type = metric.get('metric_type') unique_id = metric.get('unique_id') check = metric.get('check') order = metric.get('order', 0) tags = metric.get('tags', []) tags_list = tags_model.get_list_of_tags(tags_list=tags, to_dict=True) server = server_model.get_by_id(server_id) process = process_model.get_by_id(metric.get('process_id')) plugin = plugin_model.get_by_id(metric.get('plugin_id')) gauge = plugin_model.get_gauge_by_id(gauge_id=metric.get('gauge_id')) volume = volumes_model.get_by_id(metric.get('device_id')) interface = interfaces_model.get_by_id(metric.get('device_id')) healthcheck_metric = health_checks_model.get_by_id(metric.get('healthcheck_id')) append = False unit = yaxis(check) if metric_type == 'system_global' and check == 'memory': unit = "%" if metric_type == 'system_global' and check == 'disk': unit = '%' if public: url = reverse('public_dashboard_metric', kwargs={"metric_id": mongo_id}) else: url = reverse('dashboard_metric', kwargs={"metric_id": mongo_id}) result = { 'id': mongo_id, 'unique_id': unique_id, 'metric_type': metric_type, 'url': url, 'utcnow': utc_now, 'name': '', 'unit': unit, 'tags': tags_list, 'order': order } if server: result.update({'server_id': server_id, 'type': 'server_metric','server_name' :server.get('name')}) if metric_type == 'system': result['name'] = "{0}".format(check) if volume: result['name'] = u"{0}.{1}".format(result['name'], volume['name']) if interface: result['name'] = u"{0}.{1}".format(result['name'], interface['name']) append = True elif metric_type == 'process' and process: process_name = process.get('name') result['name'] = u"{0}.{1}".format(process_name, check) result['process_id'] = process['_id'] append = True elif metric_type == 'plugin' and plugin and gauge: result['name'] = u"{0}.{1}".format(plugin.get('name'), gauge.get('name')) result['plugin_id'] = plugin['_id'] result['gauge_id'] = gauge['_id'] append = True result['name'] = u"{0}.{1}".format(server.get('name'), result['name']) elif healthcheck_metric: result['healthcheck'] = healthcheck_metric result['healthcheck_id'] = healthcheck_metric.get('_id') try: del result['healthcheck']['_id'] del result['healthcheck']['server_id'] del result['healthcheck']['tags'] del result['healthcheck']['file_id'] # Custom scripts except: pass result['type'] = 'healthcheck' append = True else: key = metric.get('key') # Overwrite keys for better visual presentation if check == 'network': key = 'inbound' if key == 'i' else 'outbound' result['name'] = u"{0}.{1}".format(check, key) append = True if metric_type == 'plugin_global': result['name'] = u'{0}.{1}.{2}'.format(metric.get('plugin'), metric.get('gauge'), metric.get('key')) append = True result = self.mongoid_to_str(result, ['server_id', 'id', 'process_id', 'plugin_id', 'metric_id', 'gauge_id', 'healthcheck_id',]) if append: result_list.append(result) from operator import itemgetter sorted_list = sorted(result_list, key=itemgetter('order')) return sorted_list
def get_all(self, account_id=None, dashboard_id=None, public=None): result_list = [] query = [] params = {'dashboard_id': dashboard_id} params = self.keys_to_mongoid(data=params, keys=['dashboard_id']) if dashboard_id: query = super(DashboardMetricsModel, self).get(params=params) utc_now = unix_utc_now() for metric in query: mongo_id = metric.get('_id') server_id = metric.get('server_id') metric_type = metric.get('metric_type') unique_id = metric.get('unique_id') check = metric.get('check') order = metric.get('order', 0) tags = metric.get('tags', []) tags_list = tags_model.get_list_of_tags(tags_list=tags, to_dict=True) server = server_model.get_by_id(server_id) process = process_model.get_by_id(metric.get('process_id')) plugin = plugin_model.get_by_id(metric.get('plugin_id')) gauge = plugin_model.get_gauge_by_id( gauge_id=metric.get('gauge_id')) volume = volumes_model.get_by_id(metric.get('device_id')) interface = interfaces_model.get_by_id(metric.get('device_id')) healthcheck_metric = health_checks_model.get_by_id( metric.get('healthcheck_id')) append = False unit = yaxis(check) if metric_type == 'system_global' and check == 'memory': unit = "%" if metric_type == 'system_global' and check == 'disk': unit = '%' if public: url = reverse('public_dashboard_metric', kwargs={"metric_id": mongo_id}) else: url = reverse('dashboard_metric', kwargs={"metric_id": mongo_id}) result = { 'id': mongo_id, 'unique_id': unique_id, 'metric_type': metric_type, 'url': url, 'utcnow': utc_now, 'name': '', 'unit': unit, 'tags': tags_list, 'order': order } if server: result.update({ 'server_id': server_id, 'type': 'server_metric', 'server_name': server.get('name') }) if metric_type == 'system': result['name'] = "{0}".format(check) if volume: result['name'] = u"{0}.{1}".format( result['name'], volume['name']) if interface: result['name'] = u"{0}.{1}".format( result['name'], interface['name']) append = True elif metric_type == 'process' and process: process_name = process.get('name') result['name'] = u"{0}.{1}".format(process_name, check) result['process_id'] = process['_id'] append = True elif metric_type == 'plugin' and plugin and gauge: result['name'] = u"{0}.{1}".format(plugin.get('name'), gauge.get('name')) result['plugin_id'] = plugin['_id'] result['gauge_id'] = gauge['_id'] append = True result['name'] = u"{0}.{1}".format(server.get('name'), result['name']) elif healthcheck_metric: result['healthcheck'] = healthcheck_metric result['healthcheck_id'] = healthcheck_metric.get('_id') try: del result['healthcheck']['_id'] del result['healthcheck']['server_id'] del result['healthcheck']['tags'] del result['healthcheck']['file_id'] # Custom scripts except: pass result['type'] = 'healthcheck' append = True else: key = metric.get('key') # Overwrite keys for better visual presentation if check == 'network': key = 'inbound' if key == 'i' else 'outbound' result['name'] = u"{0}.{1}".format(check, key) append = True if metric_type == 'plugin_global': result['name'] = u'{0}.{1}.{2}'.format(metric.get('plugin'), metric.get('gauge'), metric.get('key')) append = True result = self.mongoid_to_str(result, [ 'server_id', 'id', 'process_id', 'plugin_id', 'metric_id', 'gauge_id', 'healthcheck_id', ]) if append: result_list.append(result) from operator import itemgetter sorted_list = sorted(result_list, key=itemgetter('order')) return sorted_list