Skip to content

Commit

Permalink
[minor] Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nepython committed Jun 16, 2020
1 parent d3ac3f4 commit 7ab69dd
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 70 deletions.
19 changes: 10 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,25 @@ Whether ping checks are created automatically for devices.

Whether ``config_modified`` checks are created automatically for devices.

``OPENWISP_MONITORING_CONFIG_MODIFIED_MAX_TIME``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``OPENWISP_MONITORING_DEVICE_CONFIGURATION_MODIFIED_MAXIMUM_TIME``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+--------------+-----------+
| **type**: | ``int`` |
+--------------+-----------+
| **default**: | ``5`` |
+--------------+-----------+

After ``config`` is ``modified``, if the ``modified`` status does not change after a
fixed **duration** then ``device`` health status shall change to ``problem``.
This fixed **duration** is 5 minutes by defaut and can be changed with the help of this setting.
The input represents corresponding duration in minutes.
This setting allows you to configure the time that the check is allowed to fail after
which the device health status changes to ``problem``.

**Note**: If the setting ``AUTO_CONFIG_STATUS`` is disabled then this setting need not be declared.
This default **duration** is 5 minutes. The input represents corresponding duration in minutes.

``OPENWISP_MONITORING_CONFIG_MODIFIED_RETENTION_POLICY``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Note**: If the setting ``OPENWISP_MONITORING_AUTO_CONFIG_MODIFIED`` is disabled
then this setting need not be declared.

``OPENWISP_MONITORING_DEVICE_CONFIGURATION_MODIFIED_RETENTION_POLICY``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+--------------+-------------+
| **type**: | ``time`` |
Expand Down
70 changes: 36 additions & 34 deletions openwisp_monitoring/check/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,45 +76,47 @@ def perform_check(self, store=True):
return self.check_instance.check(store=True)


@receiver(post_save, sender=Device, dispatch_uid='auto_ping')
def auto_ping_receiver(sender, instance, created, **kwargs):
"""
Implements OPENWISP_MONITORING_AUTO_PING
The creation step is executed in the background
"""
# we need to skip this otherwise this task will be executed
# every time the configuration is requested via checksum
if app_settings.AUTO_PING:
from openwisp_monitoring.check.tasks import auto_create_ping

if not app_settings.AUTO_PING or not created:
return
with transaction.atomic():
transaction.on_commit(
lambda: auto_create_ping.delay(
model=sender.__name__.lower(),
app_label=sender._meta.app_label,
object_id=str(instance.pk),
@receiver(post_save, sender=Device, dispatch_uid='auto_ping')
def auto_ping_receiver(sender, instance, created, **kwargs):
"""
Implements OPENWISP_MONITORING_AUTO_PING
The creation step is executed in the background
"""
# we need to skip this otherwise this task will be executed
# every time the configuration is requested via checksum
if not created:
return
with transaction.atomic():
transaction.on_commit(
lambda: auto_create_ping.delay(
model=sender.__name__.lower(),
app_label=sender._meta.app_label,
object_id=str(instance.pk),
)
)
)


@receiver(post_save, sender=Device, dispatch_uid='auto_config_modified')
def auto_config_modified_receiver(sender, instance, created, **kwargs):
"""
Implements OPENWISP_MONITORING_AUTO_CONFIG_MODIFIED
The creation step is executed in the background
"""
# we need to skip this otherwise this task will be executed
# every time the configuration is requested via checksum
if app_settings.AUTO_CONFIG_MODIFIED:
from openwisp_monitoring.check.tasks import auto_create_config_modified

if not app_settings.AUTO_CONFIG_MODIFIED or not created:
return
with transaction.atomic():
transaction.on_commit(
lambda: auto_create_config_modified.delay(
model=sender.__name__.lower(),
app_label=sender._meta.app_label,
object_id=str(instance.pk),
@receiver(post_save, sender=Device, dispatch_uid='auto_config_modified')
def auto_config_modified_receiver(sender, instance, created, **kwargs):
"""
Implements OPENWISP_MONITORING_AUTO_CONFIG_MODIFIED
The creation step is executed in the background
"""
# we need to skip this otherwise this task will be executed
# every time the configuration is requested via checksum
if not created:
return
with transaction.atomic():
transaction.on_commit(
lambda: auto_create_config_modified.delay(
model=sender.__name__.lower(),
app_label=sender._meta.app_label,
object_id=str(instance.pk),
)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def add_config_modified_checks(apps, schema_editor):

def remove_config_modified_checks(apps, schema_editor):
Check = apps.get_model('config', 'Device')
Check.objects.filter(name='Config Modified').delete()
Check.objects.filter(name='Configuration Modified').delete()


class Migration(migrations.Migration):
Expand Down
8 changes: 5 additions & 3 deletions openwisp_monitoring/check/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'OPENWISP_MONITORING_CHECK_CLASSES',
(
('openwisp_monitoring.check.classes.Ping', 'Ping'),
('openwisp_monitoring.check.classes.ConfigModified', 'Config Modified'),
('openwisp_monitoring.check.classes.ConfigModified', 'Configuration Modified'),
),
)
AUTO_PING = getattr(settings, 'OPENWISP_MONITORING_AUTO_PING', True)
Expand All @@ -14,10 +14,12 @@
)
# Input in minutes
CONFIG_MODIFIED_MAX_TIME = getattr(
settings, 'OPENWISP_MONITORING_CONFIG_MODIFIED_MAXIMUM_TIME', 5
settings, 'OPENWISP_MONITORING_DEVICE_CONFIGURATION_MODIFIED_MAXIMUM_TIME', 5
)
# Input in days
CONFIG_MODIFIED_RETENTION_POLICY = getattr(
settings, 'OPENWISP_MONITORING_CONFIG_STATUS_RETENTION_POLICY', '48h0m0s'
settings,
'OPENWISP_MONITORING_DEVICE_CONFIGURATION_MODIFIED_RETENTION_POLICY',
'48h0m0s',
)
MANAGEMENT_IP_ONLY = getattr(settings, 'OPENWISP_MONITORING_MANAGEMENT_IP_ONLY', True)
28 changes: 13 additions & 15 deletions openwisp_monitoring/check/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.contrib.contenttypes.models import ContentType
from swapper import load_model

from . import settings as app_settings


@shared_task
def run_checks():
Expand Down Expand Up @@ -39,14 +41,13 @@ def auto_create_ping(model, app_label, object_id):
"""
Called by openwisp_monitoring.check.models.auto_ping_receiver
"""
if not app_settings.AUTO_PING:
return
Check = load_model('check', 'Check')
ping_path = 'openwisp_monitoring.check.classes.Ping'
has_check = (
Check.objects.filter(
object_id=object_id, content_type__model='device', check=ping_path
).count()
> 0
)
has_check = Check.objects.filter(
object_id=object_id, content_type__model='device', check=ping_path
).exists()
# create new check only if necessary
if has_check:
return
Expand All @@ -61,22 +62,19 @@ def auto_create_config_modified(model, app_label, object_id):
"""
Called by openwisp_monitoring.check.models.auto_config_modified_receiver
"""
if not app_settings.AUTO_CONFIG_MODIFIED:
return
Check = load_model('check', 'Check')
config_modified_path = 'openwisp_monitoring.check.classes.ConfigModified'
has_check = (
Check.objects.filter(
object_id=object_id,
content_type__model='device',
check=config_modified_path,
).count()
> 0
)
has_check = Check.objects.filter(
object_id=object_id, content_type__model='device', check=config_modified_path,
).exists()
# create new check only if necessary
if has_check:
return
ct = ContentType.objects.get(app_label=app_label, model=model)
check = Check(
name='Config Modified',
name='Configuration Modified',
check=config_modified_path,
content_type=ct,
object_id=object_id,
Expand Down
14 changes: 8 additions & 6 deletions openwisp_monitoring/check/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_check_str(self):

def test_check_no_content_type(self):
check = Check(
name='Config Modified',
name='Configuration Modified',
check='openwisp_monitoring.check.classes.ConfigModified',
)
m = check.check_instance.get_metric()
Expand All @@ -44,8 +44,10 @@ def test_check_class(self):
with self.subTest('Test Ping check Class'):
c = Check(name='Ping class check', check=self._PING)
self.assertEqual(c.check_class, Ping)
with self.subTest('Test Config Modified check Class'):
c = Check(name='Config Modified class check', check=self._CONFIG_MODIFIED)
with self.subTest('Test Configuration Modified check Class'):
c = Check(
name='Configuration Modified class check', check=self._CONFIG_MODIFIED
)
self.assertEqual(c.check_class, ConfigModified)

def test_check_instance(self):
Expand All @@ -58,9 +60,9 @@ def test_check_instance(self):
self.assertIsInstance(i, Ping)
self.assertEqual(i.related_object, obj)
self.assertEqual(i.params, c.params)
with self.subTest('Test Config Modified check instance'):
with self.subTest('Test Configuration Modified check instance'):
c = Check(
name='Config Modified class check',
name='Configuration Modified class check',
check=self._CONFIG_MODIFIED,
content_object=obj,
params={},
Expand All @@ -79,7 +81,7 @@ def test_validation(self):
self.assertIn('device', str(e))
else:
self.fail('ValidationError not raised')
with self.subTest('Test Config Modified check validation'):
with self.subTest('Test Configuration Modified check validation'):
check = Check(name='Ping check', check=self._CONFIG_MODIFIED, params={})
try:
check.full_clean()
Expand Down
2 changes: 1 addition & 1 deletion openwisp_monitoring/device/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ def config_modified_receiver(cls, sender, instance, **kwargs):

DeviceData = load_model('device_monitoring', 'DeviceData')
device = DeviceData.objects.get(config=instance)
check = device.checks.get(name='Config Modified')
check = device.checks.get(name='Configuration Modified')
perform_check.delay(check.id)
4 changes: 3 additions & 1 deletion openwisp_monitoring/device/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ def test_config_modified_receiver(self, mock_method):
c = self._create_config(status='applied', organization=self._create_org())
config_modified_path = 'openwisp_monitoring.check.classes.ConfigModified'
Check.objects.create(
name='Config Modified', content_object=c.device, check=config_modified_path
name='Configuration Modified',
content_object=c.device,
check=config_modified_path,
)
c.config = {'general': {'description': 'test'}}
c.full_clean()
Expand Down

0 comments on commit 7ab69dd

Please sign in to comment.