def get_all_for_server_test(self): self._cleanup() volumes_model.get_or_create(server_id=self.server_id, name='test') volumes_model.get_or_create(server_id=self.server_id, name='test1') volumes_model.get_or_create(server_id=self.server_id, name='test2') result = volumes_model.get_all_for_server(server_id=self.server_id) eq_(result.count(), 3) self._cleanup()
def get_server_metrics(self, account_id=None, server_id=None): data = [] constants = ['cpu', 'memory', 'loadavg', 'network'] process_charts = ['cpu', 'memory'] processes = process_model.get_all_for_server(server_id) plugins = plugin_model.get_for_server(server_id=server_id) volumes = volumes_model.get_all_for_server(server_id=server_id) interfaces = interfaces_model.get_all_for_server(server_id=server_id) for v in volumes: name = "Disk.{0}".format(v['name']) _id = "server_id:{0}.check:disk.device_id:{1}".format( server_id, v['_id']) data.append([_id, name, 'System metrics']) for v in interfaces: name = "Adapter.{0}".format(v['name']) _id = "server_id:{0}.check:network.device_id:{1}".format( server_id, v['_id']) data.append([_id, name, 'System metrics']) for check in constants: name = "{0}".format(check.title()) _id = "server_id:{0}.check:{1}".format(server_id, check) data.append([_id, name, 'System metrics']) for p in processes: for check in process_charts: name = "{0}.{1}".format(p['name'], check) _id = "server_id:{0}.process_id:{1}.check:{2}".format( server_id, p['_id'], check) data.append([_id, name, 'Process Metrics']) for p in plugins: gauges = plugin_model.get_gauges_cursor(plugin=p) for g in gauges: name = "{0}.{1}".format(p.get('name'), g.get('name')) _id = "server_id:{0}.plugin_id:{1}.gauge_id:{2}.check:plugin".format( server_id, p['_id'], g['_id']) data.append([_id, name, 'Plugin Metrics']) data = sorted(data, key=itemgetter(1)) return data
def get_server_metrics(self, account_id=None, server_id=None): data = [] constants = ['cpu', 'memory', 'loadavg', 'network'] process_charts = ['cpu', 'memory'] processes = process_model.get_all_for_server(server_id) plugins = plugin_model.get_for_server(server_id=server_id) volumes = volumes_model.get_all_for_server(server_id=server_id) interfaces = interfaces_model.get_all_for_server(server_id=server_id) for v in volumes: name = "Disk.{0}".format(v['name']) _id = "server_id:{0}.check:disk.device_id:{1}".format(server_id, v['_id']) data.append([_id, name, 'System metrics']) for v in interfaces: name = "Adapter.{0}".format(v['name']) _id = "server_id:{0}.check:network.device_id:{1}".format(server_id, v['_id']) data.append([_id, name, 'System metrics']) for check in constants: name = "{0}".format(check.title()) _id = "server_id:{0}.check:{1}".format(server_id, check) data.append([_id, name, 'System metrics']) for p in processes: for check in process_charts: name = "{0}.{1}".format(p['name'], check) _id = "server_id:{0}.process_id:{1}.check:{2}".format(server_id, p['_id'], check) data.append([_id, name, 'Process Metrics']) for p in plugins: gauges = plugin_model.get_gauges_cursor(plugin=p) for g in gauges: name = "{0}.{1}".format(p.get('name'), g.get('name')) _id = "server_id:{0}.plugin_id:{1}.gauge_id:{2}.check:plugin".format(server_id, p['_id'], g['_id']) data.append([_id, name, 'Plugin Metrics']) data = sorted(data, key=itemgetter(1)) return data
def system_view(request, server_id): enddate = request.GET.get('enddate') charts = request.GET.get('charts', 'all') duration = request.GET.get('duration', 10800) duration = int(duration) server = server_model.get_by_id(server_id) tags = server_model.get_tags(server=server) if tags: server['tags'] = tags first_check_date = system_model.get_first_check_date(server) now_unix = datetime_to_unixtime(request.now) max_date = now_unix * 1000 if enddate: date_to = int(enddate) else: date_to = now_unix date_from = date_to - int(duration) data_url = reverse('ajax_get_data_after') data_url = "{0}?server_id={1}".format(data_url, server.get('_id')) charts_metadata = { 'cpu': { 'id': 'cpu', 'check': 'cpu', 'name': 'CPU', 'url': data_url, }, 'loadavg': { 'id': 'loadavg', 'check': 'loadavg', 'name': 'Load Average', 'url': data_url }, 'memory': { 'id': 'memory', 'check': 'memory', 'name': 'Memory', 'url': data_url, 'type': 'area' }, 'network': [{ 'id': 'network.inbound', 'check': 'network.inbound', 'name': 'Network - KB/s Received', 'url': data_url, 'unit': 'kb/s' }, { 'id': 'network.outbound', 'check': 'network.outbound', 'name': 'Network - KB/s Sent', 'url': data_url, 'unit': 'kb/s' }] } if charts == 'all': active_checks = ['cpu', 'memory', 'loadavg', 'disk', 'network'] else: active_checks = [charts] selected_charts = [] for check in active_checks: if check in ['network']: chart_meta = charts_metadata.get(check) for i in chart_meta: selected_charts.append(i) elif check != 'disk': chart_meta = charts_metadata.get(check) selected_charts.append(chart_meta) volumes = volumes_model.get_all_for_server(server_id) if 'disk' in active_checks: unit = get_disk_unit(server) for device in volumes.clone(): device_id, name = device.get('_id'), device.get('name') url = "{0}&device_id={1}".format(data_url, device_id) meta = { 'id': device_id, 'check': 'disk', 'name': name, 'url': url, 'unit': unit } last_update = device.get('last_update') if last_update > date_from: selected_charts.append(meta) all_plugins = plugin_model.get_for_server(server_id=server['_id'], last_check_after=date_from) all_processes = process_model.get_all_for_server( server_id, last_check_after=date_from) breadcrumb_url = reverse('server_system', kwargs={'server_id': server['_id']}) breadcrumb_url = "{0}?charts={1}".format(breadcrumb_url, charts) return render( request, 'system/view.html', { "enddate": enddate, "duration": duration, "all_processes": all_processes, "all_plugins": all_plugins, "now": now_unix, "charts": charts, "selected_charts": selected_charts, "date_from": date_from, "date_to": date_to, "first_check_date": first_check_date, "server": server, "max_date": max_date, "server_id": server_id, "breadcrumb_url": breadcrumb_url })
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 get_server_metrics(self, server_id=None): data = [] system_alerts = ['CPU', 'Memory', 'Loadavg', 'Disk', 'Network/inbound', 'Network/outbound', 'Not Sending Data'] process_alerts = ['CPU', 'Memory', 'Down'] processes = process_model.get_all_for_server(server_id) plugin_gauges = plugin_model.get_gauge_keys_for_server(server_id) volumes = volumes_model.get_all_for_server(server_id) interfaces = interfaces_model.get_all_for_server(server_id) for metric in system_alerts: spaceless_metric = metric.replace(" ", "") _id = "server:{0}.metric:{1}.rule_type:system".format(server_id, spaceless_metric) data.append({'value': _id, 'name': metric, 'metric': metric}) if metric == 'Disk': for volume in volumes.clone(): name = "Disk.{0}".format(volume.get('name')) _id = "server:{0}.metric:Disk.rule_type:system.volume:{1}".format(server_id, volume.get('name')) data.append({'value': _id, 'name': name, 'metric': metric, }) if metric.startswith('Network'): for interface in interfaces.clone(): name = "{0}.{1}".format(metric, interface.get('name')) _id = "server:{0}.metric:{1}.rule_type:system.interface:{2}".format(server_id, metric, interface.get('name')) data.append({ 'value': _id, 'name': name, 'metric': metric, }) if processes: for p in processes: for metric in process_alerts: name = "{0}.{1}".format(p['name'], metric) rule_type = 'process' if metric != 'Down' else 'uptime' _id = "server:{0}.process:{1}.metric:{2}.rule_type:{3}".format(server_id, p['_id'], metric, rule_type) data.append({'value': _id, 'name': name, 'metric': metric}) if len(plugin_gauges) > 0: for g in plugin_gauges: plugin = g.get('plugin') gauge = g.get('gauge') key = g.get('key') name = "{0}.{1}.{2}".format(plugin['name'], gauge['name'], key) _id = "server:{0}.plugin:{1}.gauge:{2}.key:{3}.rule_type:plugin".format( server_id, plugin['_id'], gauge['_id'], key ) data.append({'value': _id, 'name': name}) return data
def system_view(request, server_id): enddate = request.GET.get('enddate') charts = request.GET.get('charts', 'all') duration = request.GET.get('duration', 10800) duration = int(duration) server = server_model.get_by_id(server_id) tags = server_model.get_tags(server=server) if tags: server['tags'] = tags first_check_date = system_model.get_first_check_date(server) now_unix = datetime_to_unixtime(request.now) max_date = now_unix * 1000 if enddate: date_to = int(enddate) else: date_to = now_unix date_from = date_to - int(duration) data_url = reverse('ajax_get_data_after') data_url = "{0}?server_id={1}".format(data_url, server.get('_id')) charts_metadata = { 'cpu': {'id': 'cpu', 'check': 'cpu', 'name': 'CPU', 'url': data_url, }, 'loadavg': {'id': 'loadavg', 'check': 'loadavg', 'name': 'Load Average','url': data_url}, 'memory': {'id': 'memory', 'check': 'memory', 'name': 'Memory', 'url': data_url, 'type': 'area'}, 'network': [ {'id': 'network.inbound', 'check': 'network.inbound', 'name': 'Network - KB/s Received', 'url': data_url, 'unit': 'kb/s'}, {'id': 'network.outbound', 'check': 'network.outbound', 'name': 'Network - KB/s Sent', 'url': data_url, 'unit': 'kb/s'} ] } if charts == 'all': active_checks = ['cpu', 'memory', 'loadavg', 'disk', 'network'] else: active_checks = [charts] selected_charts = [] for check in active_checks: if check in ['network']: chart_meta = charts_metadata.get(check) for i in chart_meta: selected_charts.append(i) elif check != 'disk': chart_meta = charts_metadata.get(check) selected_charts.append(chart_meta) volumes = volumes_model.get_all_for_server(server_id) if 'disk' in active_checks: unit = get_disk_unit(server) for device in volumes.clone(): device_id, name = device.get('_id'), device.get('name') url = "{0}&device_id={1}".format(data_url, device_id) meta = {'id': device_id, 'check': 'disk', 'name': name, 'url': url , 'unit': unit} last_update = device.get('last_update') if last_update > date_from: selected_charts.append(meta) all_plugins = plugin_model.get_for_server(server_id=server['_id'], last_check_after=date_from) all_processes = process_model.get_all_for_server(server_id, last_check_after=date_from) breadcrumb_url = reverse('server_system', kwargs={'server_id': server['_id']}) breadcrumb_url = "{0}?charts={1}".format(breadcrumb_url, charts) return render(request, 'system/view.html', { "enddate": enddate, "duration": duration, "all_processes": all_processes, "all_plugins": all_plugins, "now": now_unix, "charts":charts, "selected_charts" : selected_charts, "date_from" : date_from, "date_to" : date_to, "first_check_date" : first_check_date, "server" : server, "max_date" : max_date, "server_id": server_id, "breadcrumb_url": breadcrumb_url })