Skip to content

Commit fb6423f

Browse files
committed
[chores] Requested changes
1 parent 9dd843c commit fb6423f

File tree

5 files changed

+32
-62
lines changed

5 files changed

+32
-62
lines changed

openwisp_monitoring/check/classes/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from django.core.exceptions import ValidationError
33
from swapper import load_model
44

5+
from .. import settings as app_settings
6+
57
Check = load_model('check', 'Check')
68
Metric = load_model('monitoring', 'Metric')
79
Device = load_model('config', 'Device')
@@ -49,3 +51,13 @@ def _get_or_create_metric(self, configuration=None):
4951
)
5052
metric, created = Metric._get_or_create(**options)
5153
return metric, created
54+
55+
def _get_ip(self):
56+
"""
57+
Figures out ip to use or fails raising OperationalError
58+
"""
59+
device = self.related_object
60+
ip = device.management_ip
61+
if not ip and not app_settings.MANAGEMENT_IP_ONLY:
62+
ip = device.last_ip
63+
return ip

openwisp_monitoring/check/classes/ping.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from swapper import load_model
77

88
from ... import settings as monitoring_settings
9-
from .. import settings as app_settings
109
from ..exceptions import OperationalError
1110
from .base import BaseCheck
1211

@@ -123,16 +122,6 @@ def _get_param(self, param):
123122
"""
124123
return self.params.get(param, self.schema['properties'][param]['default'])
125124

126-
def _get_ip(self):
127-
"""
128-
Figures out ip to use or fails raising OperationalError
129-
"""
130-
device = self.related_object
131-
ip = device.management_ip
132-
if not ip and not app_settings.MANAGEMENT_IP_ONLY:
133-
ip = device.last_ip
134-
return ip
135-
136125
def _command(self, command):
137126
"""
138127
Executes command (easier to mock)
Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from copy import deepcopy
2-
31
from django.utils.functional import cached_property
42
from netengine.backends.snmp.openwrt import OpenWRT
53
from swapper import load_model
64

75
from openwisp_monitoring.device.api.views import MetricChartsMixin
86

9-
from .. import settings as app_settings
107
from .base import BaseCheck
118

129
Chart = load_model('monitoring', 'Chart')
@@ -19,7 +16,7 @@
1916
class SnmpDeviceMonitoring(BaseCheck, MetricChartsMixin):
2017
def check(self, store=True):
2118
result = self.netengine_instance.to_dict()
22-
self._init_previous_data()
19+
self._init_previous_data(data=getattr(self.related_object, 'data', {}))
2320
self.related_object.data = result
2421
if store:
2522
self.store_result(result)
@@ -35,38 +32,11 @@ def store_result(self, data):
3532
@cached_property
3633
def netengine_instance(self):
3734
ip = self._get_ip()
38-
return OpenWRT(host=ip, **self.credential_params)
35+
return OpenWRT(host=ip, **self._get_credential_params())
3936

40-
@cached_property
41-
def credential_params(self):
42-
params = {}
37+
def _get_credential_params(self):
4338
cred = Credentials.objects.filter(
4439
deviceconnection__device_id=self.related_object,
4540
connector='openwisp_controller.connection.connectors.snmp.Snmp',
4641
).last()
47-
if cred is not None:
48-
params.update(cred.params)
49-
return params
50-
51-
def _get_ip(self):
52-
"""
53-
Figures out ip to use or fails raising OperationalError
54-
"""
55-
device = self.related_object
56-
ip = device.management_ip
57-
if not ip and not app_settings.MANAGEMENT_IP_ONLY:
58-
ip = device.last_ip
59-
return ip
60-
61-
def _init_previous_data(self):
62-
"""
63-
makes NetJSON interfaces of previous
64-
snapshots more easy to access
65-
"""
66-
data = getattr(self.related_object, 'data', {})
67-
if data:
68-
data = deepcopy(data)
69-
data['interfaces_dict'] = {}
70-
for interface in data.get('interfaces', []):
71-
data['interfaces_dict'][interface['name']] = interface
72-
self._previous_data = data
42+
return getattr(cred, 'params', {})

openwisp_monitoring/check/tests/test_ping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def test_auto_chart_disabled(self, *args):
227227
device = self._create_device(organization=self._create_org())
228228
device.last_ip = '127.0.0.1'
229229
device.save()
230-
check = Check.objects.first()
230+
check = Check.objects.filter(check=self._PING).first()
231231
self.assertEqual(Chart.objects.count(), 0)
232232
check.perform_check()
233233
self.assertEqual(Chart.objects.count(), 0)

openwisp_monitoring/device/api/views.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ def _write(self, pk, data=None):
4747
"""
4848
write metrics to database
4949
"""
50-
try:
50+
if data is None:
5151
# saves raw device data
5252
self.instance.save_data()
5353
data = self.instance.data
54-
except AttributeError:
55-
pass
5654
ct = ContentType.objects.get_for_model(Device)
5755
for interface in data.get('interfaces', []):
5856
ifname = interface['name']
@@ -317,6 +315,20 @@ def _create_access_tech_chart(self, metric):
317315
chart.full_clean()
318316
chart.save()
319317

318+
def _init_previous_data(self, data=None):
319+
"""
320+
makes NetJSON interfaces of previous
321+
snapshots more easy to access
322+
"""
323+
if data is None:
324+
data = self.instance.data or {}
325+
if data:
326+
data = deepcopy(data)
327+
data['interfaces_dict'] = {}
328+
for interface in data.get('interfaces', []):
329+
data['interfaces_dict'][interface['name']] = interface
330+
self._previous_data = data
331+
320332

321333
class DeviceMetricView(GenericAPIView, MetricChartsMixin):
322334
model = DeviceData
@@ -448,19 +460,6 @@ def post(self, request, pk):
448460
)
449461
return Response(None)
450462

451-
def _init_previous_data(self):
452-
"""
453-
makes NetJSON interfaces of previous
454-
snapshots more easy to access
455-
"""
456-
data = self.instance.data or {}
457-
if data:
458-
data = deepcopy(data)
459-
data['interfaces_dict'] = {}
460-
for interface in data.get('interfaces', []):
461-
data['interfaces_dict'][interface['name']] = interface
462-
self._previous_data = data
463-
464463

465464
device_metric = DeviceMetricView.as_view()
466465

0 commit comments

Comments
 (0)