Skip to content

Commit

Permalink
[misc] Fix bugs, requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Aug 4, 2021
1 parent c277a53 commit 9dd843c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 34 deletions.
17 changes: 13 additions & 4 deletions openwisp_monitoring/check/classes/snmp_devicemonitoring.py
Original file line number Diff line number Diff line change
@@ -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')


Expand All @@ -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):
"""
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 @@ -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)
29 changes: 0 additions & 29 deletions openwisp_monitoring/check/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,13 @@
from django.core.exceptions import ObjectDoesNotExist
from swapper import load_model

from openwisp_controller.connection import settings as app_settings

logger = logging.getLogger(__name__)


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():
"""
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 9dd843c

Please sign in to comment.