diff --git a/openwisp_monitoring/check/classes/iperf.py b/openwisp_monitoring/check/classes/iperf.py index 80698be23..637ba89f1 100644 --- a/openwisp_monitoring/check/classes/iperf.py +++ b/openwisp_monitoring/check/classes/iperf.py @@ -1,5 +1,11 @@ +import json + +from django.core.exceptions import ObjectDoesNotExist from swapper import load_model +from openwisp_controller.connection.settings import UPDATE_STRATEGIES + +from .. import settings as app_settings from .base import BaseCheck Chart = load_model('monitoring', 'Chart') @@ -13,24 +19,100 @@ class Iperf(BaseCheck): def check(self, store=True): - pass + try: + device = self.related_object + device_connection = self._check_device_connection(device) + if device_connection: + device_connection.connect() + print(f'--- [{self.related_object}] is connected! ---') + servers = self._get_iperf_servers(device.organization.id) + command = f'iperf3 -c {servers[0]} -J' + res, exit_code = device_connection.connector_instance.exec_command( + command, raise_unexpected_exit=False + ) + if exit_code != 0: + print('---- Command Failed ----') + if store: + 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, + } + ) + return + else: + result_dict = self._get_iperf_result(res) + print('---- Command Output ----') + print(result_dict) + if store: + self.store_result(result_dict) + return result_dict + else: + print( + f'{self.related_object}: connection not properly set, Iperf skipped!' + ) + return + # If device have not active connection warning logged (return) + except ObjectDoesNotExist: + print(f'{self.related_object}: has no active connection, Iperf skipped!') + return - def store_result(self, result): + def _check_device_connection(self, device): """ - store result in the DB + Check device has an active connection with right update_strategy(ssh) """ - pass + openwrt_ssh = UPDATE_STRATEGIES[0][0] + device_connection = DeviceConnection.objects.get(device_id=device.id) + if device_connection.update_strategy == openwrt_ssh: + if device_connection.enabled and device_connection.is_working: + return device_connection + else: + return False + else: + return False - def _get_iperf_servers(self): + def _get_iperf_servers(self, organization): """ Get iperf test servers """ - pass + org_servers = app_settings.IPERF_SERVERS.get(str(organization)) + return org_servers - def _get_iperf_result(self, mode=None): + def _get_iperf_result(self, res, mode=None): """ Get iperf test result """ + res_dict = json.loads(res) + if mode is None: + 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'], + } + return result + # For UDP + else: + pass + + def store_result(self, result): + """ + store result in the DB + """ pass def _get_metric(self): diff --git a/openwisp_monitoring/check/settings.py b/openwisp_monitoring/check/settings.py index 9ebab74cc..3bda6e0fb 100644 --- a/openwisp_monitoring/check/settings.py +++ b/openwisp_monitoring/check/settings.py @@ -13,12 +13,12 @@ MANAGEMENT_IP_ONLY = get_settings_value('MANAGEMENT_IP_ONLY', True) PING_CHECK_CONFIG = get_settings_value('PING_CHECK_CONFIG', {}) # By default it should be disabled. -AUTO_IPERF = get_settings_value('AUTO_IPERF', False) -# IPERF_SERVERS = get_settings_value( -# 'IPERF_SERVERS', -# { -# # Running on my local -# 'be63c4e5-a68a-4650-bfe8-733837edb8be': ['172.19.0.1'], -# # '': [''] -# }, -# ) +AUTO_IPERF = get_settings_value('AUTO_IPERF', True) +IPERF_SERVERS = get_settings_value( + 'IPERF_SERVERS', + { + # Running on my local + 'be63c4e5-a68a-4650-bfe8-733837edb8be': ['192.168.5.109'], + # '': [''] + }, +)