Skip to content

Commit

Permalink
[chores] Removed unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryamanz29 committed Jun 20, 2022
1 parent 3fb428b commit f968f79
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
31 changes: 11 additions & 20 deletions openwisp_monitoring/check/classes/iperf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json

import logging
from django.core.exceptions import ObjectDoesNotExist
from swapper import load_model

Expand All @@ -8,10 +8,11 @@
from .. import settings as app_settings
from .base import BaseCheck

logger = logging.getLogger(__name__)

Chart = load_model('monitoring', 'Chart')
Metric = load_model('monitoring', 'Metric')
Device = load_model('config', 'Device')
DeviceMonitoring = load_model('device_monitoring', 'DeviceMonitoring')
Credentials = load_model('connection', 'Credentials')
AlertSettings = load_model('monitoring', 'AlertSettings')
DeviceConnection = load_model('connection', 'DeviceConnection')
Expand All @@ -24,47 +25,38 @@ def check(self, store=True):
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 store and exit_code != 0:
print('---- Command Failed (TCP)----')
self.store_result_fail()
device_connection.disconnect()
return
else:
result_dict_tcp = self._get_iperf_result(res, mode='TCP')
print('---- Command Output (TCP) ----')
print(result_dict_tcp)
# UDP
command = f'iperf3 -c {servers[0]} -u -J'
res, exit_code = device_connection.connector_instance.exec_command(
command, raise_unexpected_exit=False
)
if store and exit_code != 0:
print('---- Command Failed (UDP) ----')
self.store_result_fail()
device_connection.disconnect()
return
else:
result_dict_udp = self._get_iperf_result(res, mode='UDP')
print('---- Command Output (UDP) ----')
print(result_dict_udp)
if store:
self.store_result({**result_dict_tcp, **result_dict_udp})
device_connection.disconnect()
return {**result_dict_tcp, **result_dict_udp}
else:
print(
f'{self.related_object}: connection not properly set, Iperf skipped!'
)
logger.warning(f'{device}: 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!')
logger.warning(f'{device}: connection not properly set, Iperf skipped!')
return

def _check_device_connection(self, device):
Expand All @@ -73,24 +65,23 @@ def _check_device_connection(self, device):
"""
openwrt_ssh = UPDATE_STRATEGIES[0][0]
device_connection = DeviceConnection.objects.get(device_id=device.id)
device_monitoring = DeviceMonitoring.objects.get(device_id=device.id)
if device_connection.update_strategy == openwrt_ssh:
if (
device_connection.enabled
and device_connection.is_working
and device_monitoring.status in ['ok', 'problem']
and device.monitoring.status in ['ok', 'problem']
):
return device_connection
else:
return False
else:
return False

def _get_iperf_servers(self, organization):
def _get_iperf_servers(self, organization_id):
"""
Get iperf test servers
"""
org_servers = app_settings.IPERF_SERVERS.get(str(organization))
org_servers = app_settings.IPERF_SERVERS.get(str(organization_id))
return org_servers

def _get_iperf_result(self, res, mode):
Expand Down Expand Up @@ -127,12 +118,12 @@ def _get_iperf_result(self, res, mode):
jitter_ms = res_dict['end']['sum']['jitter_ms']
packets = res_dict['end']['sum']['packets']
lost_packets = res_dict['end']['sum']['lost_packets']
lost_percent = res_dict['end']['sum']['lost_percent']
lost_percent = float(res_dict['end']['sum']['lost_percent'])
result = {
'jitter': round(jitter_ms, 2),
'packets': packets,
'lost_packets': lost_packets,
'lost_percent': lost_percent,
'lost_percent': round(lost_percent, 2),
}
return result

Expand Down Expand Up @@ -160,7 +151,7 @@ def store_result_fail(self):
'jitter': 0.0,
'packets': 0,
'lost_packets': 0,
'lost_percent': 0,
'lost_percent': 0.0,
}
)

Expand Down
2 changes: 1 addition & 1 deletion openwisp_monitoring/check/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# 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'],
'a9734710-db30-46b0-a2fc-01f01046fe4f': ['speedtest.uztelecom.uz'],
# '<org-pk>': ['<ORG_IPERF_SERVER>']
},
)
4 changes: 2 additions & 2 deletions openwisp_monitoring/monitoring/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def _get_access_tech():
'unit': _(' Gbps'),
'order': 280,
'query': chart_query['bitrate'],
'colors': (DEFAULT_COLORS[5], DEFAULT_COLORS[9]),
'colors': [DEFAULT_COLORS[5], DEFAULT_COLORS[9]],
},
'transfer': {
'type': 'stackedbar',
Expand All @@ -584,7 +584,7 @@ def _get_access_tech():
'unit': _(' GB'),
'order': 290,
'query': chart_query['transfer'],
'colors': (DEFAULT_COLORS[2], DEFAULT_COLORS[4]),
'colors': [DEFAULT_COLORS[2], DEFAULT_COLORS[4]],
},
'retransmits': {
'type': 'bar',
Expand Down

0 comments on commit f968f79

Please sign in to comment.