Skip to content

Commit

Permalink
[feature] Added device connection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryamanz29 committed Jun 11, 2022
1 parent 29a0744 commit 28a73a0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 16 deletions.
96 changes: 89 additions & 7 deletions openwisp_monitoring/check/classes/iperf.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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):
Expand Down
18 changes: 9 additions & 9 deletions openwisp_monitoring/check/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
# # '<org-pk>': ['<ORG_IPERF_SERVER>']
# },
# )
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'],
# '<org-pk>': ['<ORG_IPERF_SERVER>']
},
)

0 comments on commit 28a73a0

Please sign in to comment.