From bad881d723bf64ab6ec38d521a6a21a0db42e8f0 Mon Sep 17 00:00:00 2001 From: Aryamanz29 Date: Mon, 13 Jun 2022 17:41:03 +0530 Subject: [PATCH] [feature] Added charts and metric (TCP) --- openwisp_monitoring/check/classes/iperf.py | 60 ++++++++++++------- openwisp_monitoring/check/settings.py | 2 + .../db/backends/influxdb/queries.py | 23 +++++++ .../monitoring/configuration.py | 50 ++++++++++++++++ 4 files changed, 114 insertions(+), 21 deletions(-) diff --git a/openwisp_monitoring/check/classes/iperf.py b/openwisp_monitoring/check/classes/iperf.py index 637ba89f1..d56280174 100644 --- a/openwisp_monitoring/check/classes/iperf.py +++ b/openwisp_monitoring/check/classes/iperf.py @@ -36,13 +36,14 @@ def check(self, store=True): self.store_result( { 'iperf_result': 0, - 'sum_sent_bps': 0.0, - 'sum_rec_bps': 0.0, - 'sum_sent_bytes': 0.0, - 'sum_rec_bytes': 0.0, - 'sum_sent_retransmits': 0, + 'sent_bps': 0.0, + 'received_bps': 0.0, + 'sent_bytes': 0.0, + 'received_bytes': 0.0, + 'retransmits': 0, } ) + device_connection.disconnect() return else: result_dict = self._get_iperf_result(res) @@ -50,6 +51,7 @@ def check(self, store=True): print(result_dict) if store: self.store_result(result_dict) + device_connection.disconnect() return result_dict else: print( @@ -88,21 +90,27 @@ def _get_iperf_result(self, res, mode=None): """ res_dict = json.loads(res) if mode is None: + # Gbps = Gigabits per second + # GB = GigaBytes + sent_json = res_dict['end']['sum_sent'] + recv_json = res_dict['end']['sum_received'] + sent_bytes = sent_json['bytes'] + sent_bytes_GB = sent_bytes / 1000000000 + sent_bps = sent_json['bits_per_second'] + sent_Gbps = sent_bps / 1000000000 + received_bytes = recv_json['bytes'] + received_bytes_GB = received_bytes / 1000000000 + received_bps = recv_json['bits_per_second'] + received_Gbps = received_bps / 1000000000 + retransmits = sent_json['retransmits'] + result = { 'iperf_result': 1, - 'sum_sent_bps': round( - res_dict['end']['sum_sent']['bits_per_second'] / 1000000000, 2 - ), - 'sum_rec_bps': round( - res_dict['end']['sum_received']['bits_per_second'] / 1000000000, 2 - ), - 'sum_sent_bytes': round( - res_dict['end']['sum_sent']['bytes'] / 1000000000, 2 - ), - 'sum_rec_bytes': round( - res_dict['end']['sum_received']['bytes'] / 1000000000, 2 - ), - 'sum_sent_retransmits': res_dict['end']['sum_sent']['retransmits'], + 'sent_bps': round(sent_Gbps, 2), + 'received_bps': round(received_Gbps, 2), + 'sent_bytes': round(sent_bytes_GB, 2), + 'received_bytes': round(received_bytes_GB, 2), + 'retransmits': retransmits, } return result # For UDP @@ -113,19 +121,29 @@ def store_result(self, result): """ store result in the DB """ - pass + metric = self._get_metric() + copied = result.copy() + iperf_result = copied.pop('iperf_result') + metric.write(iperf_result, extra_values=copied) def _get_metric(self): """ Gets or creates metric """ - pass + metric, created = self._get_or_create_metric() + if created: + self._create_charts(metric) + return metric def _create_charts(self, metric): """ Creates iperf related charts (Bandwith/Jitter) """ - pass + charts = ['bitrate', 'transfer', 'retransmits'] + for chart in charts: + chart = Chart(metric=metric, configuration=chart) + chart.full_clean() + chart.save() def _create_alert_settings(self, metric): pass diff --git a/openwisp_monitoring/check/settings.py b/openwisp_monitoring/check/settings.py index 3bda6e0fb..46f913acc 100644 --- a/openwisp_monitoring/check/settings.py +++ b/openwisp_monitoring/check/settings.py @@ -18,6 +18,8 @@ 'IPERF_SERVERS', { # Running on my local + # Some Public Iperf Servers : https://iperf.fr/iperf-servers.php#public-servers + # 'be63c4e5-a68a-4650-bfe8-733837edb8be': ['iperf.biznetnetworks.com'], 'be63c4e5-a68a-4650-bfe8-733837edb8be': ['192.168.5.109'], # '': [''] }, diff --git a/openwisp_monitoring/db/backends/influxdb/queries.py b/openwisp_monitoring/db/backends/influxdb/queries.py index 11f048096..a540a45d5 100644 --- a/openwisp_monitoring/db/backends/influxdb/queries.py +++ b/openwisp_monitoring/db/backends/influxdb/queries.py @@ -100,6 +100,29 @@ "object_id = '{object_id}' GROUP BY time(1d)" ) }, + 'bitrate': { + 'influxdb': ( + "SELECT MEAN(sent_bps) AS sent, " + "MEAN(received_bps) AS received FROM {key} WHERE " + "time >= '{time}' AND content_type = '{content_type}' AND " + "object_id = '{object_id}' GROUP BY time(1d)" + ) + }, + 'transfer': { + 'influxdb': ( + "SELECT MEAN(sent_bytes) AS sent, " + "MEAN(received_bytes) AS received FROM {key} WHERE " + "time >= '{time}' AND content_type = '{content_type}' AND " + "object_id = '{object_id}' GROUP BY time(1d)" + ) + }, + 'retransmits': { + 'influxdb': ( + "SELECT MEAN(retransmits) AS retransmits FROM {key} " + "WHERE time >= '{time}' AND content_type = '{content_type}' " + "AND object_id = '{object_id}' GROUP BY time(1d)" + ) + }, } default_chart_query = [ diff --git a/openwisp_monitoring/monitoring/configuration.py b/openwisp_monitoring/monitoring/configuration.py index 0106508c4..2cad24970 100644 --- a/openwisp_monitoring/monitoring/configuration.py +++ b/openwisp_monitoring/monitoring/configuration.py @@ -543,6 +543,56 @@ def _get_access_tech(): } }, }, + 'iperf': { + 'label': _('Iperf'), + 'name': 'Iperf', + 'key': 'iperf', + 'field_name': 'iperf_result', + 'related_fields': [ + 'sent_bps', + 'received_bps', + 'sent_bytes', + 'received_bytes', + 'retransmits', + ], + 'charts': { + 'bitrate': { + 'type': 'stackedbar', + 'title': _('Bandwidth'), + 'description': _('Iperf3 bitrate in TCP mode.'), + 'summary_labels': [ + _('Sent bitrate'), + _('Received bitrate'), + ], + 'unit': _(' Gbps'), + 'order': 280, + 'query': chart_query['bitrate'], + 'colors': (DEFAULT_COLORS[5], DEFAULT_COLORS[9]), + }, + 'transfer': { + 'type': 'stackedbar', + 'title': _('Transfer'), + 'description': _('Iperf3 transfer in TCP mode.'), + 'summary_labels': [ + _('Sent bytes'), + _('Received bytes'), + ], + 'unit': _(' GB'), + 'order': 290, + 'query': chart_query['transfer'], + 'colors': (DEFAULT_COLORS[2], DEFAULT_COLORS[4]), + }, + 'retransmits': { + 'type': 'bar', + 'title': _('Retransmits'), + 'colors': (DEFAULT_COLORS[4]), + 'description': _('No. of retransmits during Iperf3 test in TCP mode.'), + 'unit': '', + 'order': 300, + 'query': chart_query['retransmits'], + }, + }, + }, } DEFAULT_CHARTS = {}