From 9dd843c24d69a856b806d1a9d15ca913e02e9c2e Mon Sep 17 00:00:00 2001 From: purhan Date: Wed, 4 Aug 2021 21:09:43 +0530 Subject: [PATCH] [misc] Fix bugs, requested changes --- .../check/classes/snmp_devicemonitoring.py | 17 ++++++++--- openwisp_monitoring/check/settings.py | 2 +- openwisp_monitoring/check/tasks.py | 29 ------------------- tests/openwisp2/settings.py | 1 + 4 files changed, 15 insertions(+), 34 deletions(-) diff --git a/openwisp_monitoring/check/classes/snmp_devicemonitoring.py b/openwisp_monitoring/check/classes/snmp_devicemonitoring.py index 4f9e5d1b3..da8f24c10 100644 --- a/openwisp_monitoring/check/classes/snmp_devicemonitoring.py +++ b/openwisp_monitoring/check/classes/snmp_devicemonitoring.py @@ -1,19 +1,18 @@ from copy import deepcopy -from django.contrib.contenttypes.models import ContentType from django.utils.functional import cached_property from netengine.backends.snmp.openwrt import OpenWRT from swapper import load_model from openwisp_monitoring.device.api.views import MetricChartsMixin -from ... import settings as monitoring_settings from .. import settings as app_settings from .base import BaseCheck Chart = load_model('monitoring', 'Chart') Metric = load_model('monitoring', 'Metric') Device = load_model('config', 'Device') +Credentials = load_model('connection', 'Credentials') AlertSettings = load_model('monitoring', 'AlertSettings') @@ -35,9 +34,19 @@ def store_result(self, data): @cached_property def netengine_instance(self): - params = self.params['credential_params'] ip = self._get_ip() - return OpenWRT(host=ip, **params) + return OpenWRT(host=ip, **self.credential_params) + + @cached_property + def credential_params(self): + params = {} + cred = Credentials.objects.filter( + deviceconnection__device_id=self.related_object, + connector='openwisp_controller.connection.connectors.snmp.Snmp', + ).last() + if cred is not None: + params.update(cred.params) + return params def _get_ip(self): """ diff --git a/openwisp_monitoring/check/settings.py b/openwisp_monitoring/check/settings.py index 36b18afa0..08cd6e3f9 100644 --- a/openwisp_monitoring/check/settings.py +++ b/openwisp_monitoring/check/settings.py @@ -13,5 +13,5 @@ ) AUTO_PING = get_settings_value('AUTO_PING', True) AUTO_CONFIG_CHECK = get_settings_value('AUTO_DEVICE_CONFIG_CHECK', True) -AUTO_SNMP_DEVICEMONITORING = get_settings_value('OPENWISP_MONITORING_AUTO_SNMP_DEVICEMONITORING', False) +AUTO_SNMP_DEVICEMONITORING = get_settings_value('AUTO_SNMP_DEVICEMONITORING', False) MANAGEMENT_IP_ONLY = get_settings_value('MANAGEMENT_IP_ONLY', True) diff --git a/openwisp_monitoring/check/tasks.py b/openwisp_monitoring/check/tasks.py index 0b70bf9cf..f7ba31f0f 100644 --- a/openwisp_monitoring/check/tasks.py +++ b/openwisp_monitoring/check/tasks.py @@ -7,8 +7,6 @@ from django.core.exceptions import ObjectDoesNotExist from swapper import load_model -from openwisp_controller.connection import settings as app_settings - logger = logging.getLogger(__name__) @@ -16,31 +14,6 @@ def get_check_model(): return load_model('check', 'Check') -def _get_or_create_credentials(device_id, **kwargs): - Credentials = load_model('connection', 'Credentials') - cred = Credentials.objects.filter( - deviceconnection__device_id=device_id, - connector='openwisp_controller.connection.connectors.snmp.Snmp', - ).last() - if cred is not None: - return cred - - # if credentials don't exist, create new SNMP credentials - Device = load_model('config', 'Device') - opts = dict( - name='Default SNMP Credentials', - connector=app_settings.DEFAULT_CONNECTORS[1][0], - params={'community': 'public', 'agent': 'default', 'port': 161}, - ) - opts.update(kwargs) - if 'organization' not in opts: - opts['organization'] = Device.objects.get(id=device_id).organization - c = Credentials(**opts) - c.full_clean() - c.save() - return c - - @shared_task def run_checks(): """ @@ -144,13 +117,11 @@ def auto_create_snmp_devicemonitoring( return content_type_model = content_type_model or ContentType ct = content_type_model.objects.get(app_label=app_label, model=model) - cred = _get_or_create_credentials(object_id) check = Check( name='SNMP Device Monitoring', check=devicemonitoring_path, content_type=ct, object_id=object_id, - params={'credential_params': cred.get_params()}, ) check.full_clean() check.save() diff --git a/tests/openwisp2/settings.py b/tests/openwisp2/settings.py index 719fbe814..feb3d8ea3 100644 --- a/tests/openwisp2/settings.py +++ b/tests/openwisp2/settings.py @@ -192,6 +192,7 @@ OPENWISP_MONITORING_MAC_VENDOR_DETECTION = False OPENWISP_MONITORING_API_URLCONF = 'openwisp_monitoring.urls' OPENWISP_MONITORING_API_BASEURL = 'http://testserver' + OPENWISP_MONITORING_AUTO_SNMP_DEVICEMONITORING = True # Temporarily added to identify slow tests TEST_RUNNER = 'openwisp_utils.tests.TimeLoggingTestRunner'