Skip to content

Instantly share code, notes, and snippets.

@PabloCastellano
Created May 29, 2020 11:16
Show Gist options
  • Save PabloCastellano/ae3a8f23b00e0586a3f81358ecb67977 to your computer and use it in GitHub Desktop.
Save PabloCastellano/ae3a8f23b00e0586a3f81358ecb67977 to your computer and use it in GitHub Desktop.
Lost changes
From be14a14de0409ef2b3627c04dcbd25660103c455 Mon Sep 17 00:00:00 2001
From: Pablo Castellano <[email protected]>
Date: Mon, 25 May 2020 19:43:33 +0200
Subject: [PATCH 1/3] Last changes
---
openwisp_monitoring/device/api/views.py | 9 ++++++---
openwisp_monitoring/monitoring/charts.py | 19 +++++++++++--------
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/openwisp_monitoring/device/api/views.py b/openwisp_monitoring/device/api/views.py
index e1cc361..4d64bf5 100644
--- a/openwisp_monitoring/device/api/views.py
+++ b/openwisp_monitoring/device/api/views.py
@@ -205,8 +205,9 @@ class DeviceMetricView(GenericAPIView):
return
if 'load' in data['resources']:
extra_values = {
- 'load_5': data['resources']['load'][1],
- 'load_15': data['resources']['load'][2],
+ 'cpus': data['resources'].get('cpus', 1),
+ 'load_5': float(data['resources']['load'][1]),
+ 'load_15': float(data['resources']['load'][2]),
}
metric, created = Metric._get_or_create(
object_id=pk,
@@ -216,7 +217,7 @@ class DeviceMetricView(GenericAPIView):
name='Resources: Load',
)
metric.write(
- data['resources']['load'][0], extra_values=extra_values,
+ float(data['resources']['load'][0]), extra_values=extra_values,
)
if created:
self._create_resources_graph(metric, resource='load')
@@ -247,6 +248,8 @@ class DeviceMetricView(GenericAPIView):
'total_memory': memory['total'],
'buffered_memory': memory['buffered'],
'shared_memory': memory['shared'],
+ 'available_memory': memory['available'],
+ 'cached_memory': memory['cached'],
}
metric, created = Metric._get_or_create(
object_id=pk,
diff --git a/openwisp_monitoring/monitoring/charts.py b/openwisp_monitoring/monitoring/charts.py
index f5542c3..2b38958 100644
--- a/openwisp_monitoring/monitoring/charts.py
+++ b/openwisp_monitoring/monitoring/charts.py
@@ -136,15 +136,15 @@ DEFAULT_CHARTS = {
'title': _('Memory'),
'label': _('Memory'),
'description': _('Displays percentage of device memory being used'),
- 'summary_labels': [_('Buffered Memory'), _('Shared Memory'), _('Used Memory')],
- 'unit': '%',
+ 'summary_labels': [_('Total Memory'), _('Free Memory'), _('Available Memory')],
+ 'unit': f' {_("MB")}',
'order': 250,
'query': {
'influxdb': (
- "SELECT 100*(1-((MEDIAN(free_memory)+MEDIAN(buffered_memory))/MEDIAN(total_memory))) "
- "AS Used_Memory, 100*MEAN(buffered_memory)/MEAN(total_memory) AS Buffered_Memory, "
- "100*MEAN(shared_memory)/MEAN(total_memory) AS Shared_Memory FROM {key} WHERE "
- "time >= '{time}' AND content_type = '{content_type}' AND "
+ "SELECT MEAN(total_memory) / 1024 / 1024 AS Total_Memory, "
+ "MEAN(free_memory) / 1024 / 1024 AS Free_Memory, "
+ "MEAN(available_memory) / 1024 / 1024 AS Available_Memory "
+ "FROM {key} WHERE time >= '{time}' AND content_type = '{content_type}' AND "
"object_id = '{object_id}' GROUP BY time(1d)"
)
},
@@ -154,12 +154,15 @@ DEFAULT_CHARTS = {
'title': _('CPU Load'),
'label': _('CPU Load'),
'description': _('Displays average CPU Load of the device'),
- 'summary_labels': [_('CPU Load')],
+ 'summary_labels': [_('Average 1m'), _('Average 5m'), _('Average 15m')],
'unit': '%',
'order': 260,
'query': {
'influxdb': (
- "SELECT 100*MEAN(load_1) AS CPU_Load FROM {key} WHERE "
+ "SELECT 100*MEAN(load_1) / MEAN(cpus) AS Average_1m, "
+ "100*MEAN(load_5) / MEAN(cpus) AS Average_5m, "
+ "100*MEAN(load_15) / MEAN(cpus) AS Average_15m "
+ "FROM {key} WHERE "
"time >= '{time}' AND content_type = '{content_type}' AND "
"object_id = '{object_id}' GROUP BY time(1d)"
)
--
2.17.1
From e488e90d3143ba9b40937a102d9826bdcf817d97 Mon Sep 17 00:00:00 2001
From: Pablo Castellano <[email protected]>
Date: Mon, 25 May 2020 20:15:59 +0200
Subject: [PATCH 2/3] Write used percentage in the memory metric and fix tests
---
openwisp_monitoring/device/api/views.py | 6 ++++--
openwisp_monitoring/device/tests/__init__.py | 1 +
openwisp_monitoring/device/tests/test_api.py | 18 +++++++++++-------
3 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/openwisp_monitoring/device/api/views.py b/openwisp_monitoring/device/api/views.py
index 4d64bf5..ebcd8aa 100644
--- a/openwisp_monitoring/device/api/views.py
+++ b/openwisp_monitoring/device/api/views.py
@@ -245,6 +245,7 @@ class DeviceMetricView(GenericAPIView):
if 'memory' in data['resources']:
memory = data['resources']['memory']
extra_values = {
+ 'free_memory': memory['free'],
'total_memory': memory['total'],
'buffered_memory': memory['buffered'],
'shared_memory': memory['shared'],
@@ -255,10 +256,11 @@ class DeviceMetricView(GenericAPIView):
object_id=pk,
content_type=ct,
key='memory',
- field_name='free_memory',
+ field_name='used_percent',
name='Resources: Memory',
)
- metric.write(memory['free'], extra_values=extra_values)
+ used_percent = memory['free'] / memory['total']
+ metric.write(used_percent, extra_values=extra_values)
if created:
self._create_resources_graph(metric, resource='memory')
self._create_resources_threshold(metric, resource='memory')
diff --git a/openwisp_monitoring/device/tests/__init__.py b/openwisp_monitoring/device/tests/__init__.py
index 58ba4ed..ef5beac 100644
--- a/openwisp_monitoring/device/tests/__init__.py
+++ b/openwisp_monitoring/device/tests/__init__.py
@@ -43,6 +43,7 @@ class DeviceMonitoringTestCase(TestDeviceMonitoringMixin, TestCase):
'shared': 86016,
'free': 224497664,
'cached': 6774784,
+ 'available': 223397664,
'buffered': 974848,
},
'load': [0, 0, 0],
diff --git a/openwisp_monitoring/device/tests/test_api.py b/openwisp_monitoring/device/tests/test_api.py
index d5da660..0e1bec2 100644
--- a/openwisp_monitoring/device/tests/test_api.py
+++ b/openwisp_monitoring/device/tests/test_api.py
@@ -338,10 +338,12 @@ class TestDeviceApi(DeviceMonitoringTestCase):
'upload - Traffic: wlan0',
'download - Traffic: wlan1',
'upload - Traffic: wlan1',
- 'Buffered_Memory - Memory',
- 'Shared_Memory - Memory',
- 'Used_Memory - Memory',
- 'CPU_Load - CPU Load',
+ 'Available_Memory - Memory',
+ 'Free_Memory - Memory',
+ 'Total_Memory - Memory',
+ 'Average_15m - CPU Load',
+ 'Average_1m - CPU Load',
+ 'Average_5m - CPU Load',
'Disk_Usage - Disk Usage',
],
)
@@ -356,9 +358,11 @@ class TestDeviceApi(DeviceMonitoringTestCase):
'0.6',
'3',
'1.5',
- '0.39',
- '0.03',
- '9.73',
+ '213.05',
+ '214.1',
+ '238.2',
+ '0',
+ '0',
'0',
'8.27',
],
--
2.17.1
From 4a41f9db7ad43c75749d6a387f5a1ab71efb2f04 Mon Sep 17 00:00:00 2001
From: Pablo Castellano <[email protected]>
Date: Mon, 25 May 2020 20:40:16 +0200
Subject: [PATCH 3/3] Improve disk graph
---
openwisp_monitoring/device/api/views.py | 9 +++++++--
openwisp_monitoring/device/tests/test_api.py | 6 ++++--
openwisp_monitoring/monitoring/charts.py | 8 +++++---
3 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/openwisp_monitoring/device/api/views.py b/openwisp_monitoring/device/api/views.py
index ebcd8aa..2f7ebcc 100644
--- a/openwisp_monitoring/device/api/views.py
+++ b/openwisp_monitoring/device/api/views.py
@@ -227,10 +227,11 @@ class DeviceMetricView(GenericAPIView):
for disk in data['resources']['disk']:
used_percent += disk['used_percent']
used_percent = used_percent / len(data['resources']['disk']) / 100
- used_bytes, size_bytes = 0, 0
+ used_bytes, size_bytes, available_bytes = 0, 0, 0
for disk in data['resources']['disk']:
used_bytes += disk['used_bytes']
size_bytes += disk['size_bytes']
+ available_bytes = disk['available_bytes']
metric, created = Metric._get_or_create(
object_id=pk,
content_type=ct,
@@ -238,7 +239,11 @@ class DeviceMetricView(GenericAPIView):
field_name='used_disk',
name='Resources: Disk',
)
- metric.write(used_bytes / size_bytes)
+ extra_values = {
+ 'available_bytes': available_bytes,
+ 'size_bytes': size_bytes,
+ }
+ metric.write(used_bytes / size_bytes, extra_values=extra_values)
if created:
self._create_resources_graph(metric, resource='disk')
self._create_resources_threshold(metric, resource='disk')
diff --git a/openwisp_monitoring/device/tests/test_api.py b/openwisp_monitoring/device/tests/test_api.py
index 0e1bec2..dc625a4 100644
--- a/openwisp_monitoring/device/tests/test_api.py
+++ b/openwisp_monitoring/device/tests/test_api.py
@@ -344,7 +344,8 @@ class TestDeviceApi(DeviceMonitoringTestCase):
'Average_15m - CPU Load',
'Average_1m - CPU Load',
'Average_5m - CPU Load',
- 'Disk_Usage - Disk Usage',
+ 'Available_Disk - Disk Usage',
+ 'Total_Disk - Disk Usage',
],
)
last_line = rows[-1].strip().split(',')
@@ -364,7 +365,8 @@ class TestDeviceApi(DeviceMonitoringTestCase):
'0',
'0',
'0',
- '8.27',
+ '11.64',
+ '267.7',
],
)
diff --git a/openwisp_monitoring/monitoring/charts.py b/openwisp_monitoring/monitoring/charts.py
index 2b38958..7950ce6 100644
--- a/openwisp_monitoring/monitoring/charts.py
+++ b/openwisp_monitoring/monitoring/charts.py
@@ -173,12 +173,14 @@ DEFAULT_CHARTS = {
'title': _('Disk Usage'),
'label': _('Disk Usage'),
'description': _('Displays flash disk usage of the device'),
- 'summary_labels': [_('Disk Usage')],
- 'unit': '%',
+ 'summary_labels': [_('Total Disk'), _('Available Disk')],
+ 'unit': f' {_("MB")}',
'order': 270,
'query': {
'influxdb': (
- "SELECT 100*MEAN(used_disk) AS Disk_Usage FROM {key} WHERE "
+ "SELECT MEAN(size_bytes) / 1024 AS Total_Disk, "
+ "MEAN(available_bytes) / 1024 AS Available_Disk "
+ "FROM {key} WHERE "
"time >= '{time}' AND content_type = '{content_type}' AND "
"object_id = '{object_id}' GROUP BY time(1d)"
)
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment