Skip to content

Commit d1c1b81

Browse files
committed
[chores:refactor] Improved auto check logic
- Makes it easy to create new checks without having to write the auto check logic - Improves consistency
1 parent 71de97f commit d1c1b81

File tree

10 files changed

+47
-62
lines changed

10 files changed

+47
-62
lines changed

openwisp_monitoring/check/base/models.py

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from django.utils.translation import gettext_lazy as _
99
from jsonfield import JSONField
1010

11-
from openwisp_monitoring.check import settings as app_settings
12-
from openwisp_monitoring.check.tasks import auto_create_check
1311
from openwisp_utils.base import TimeStampedEditableModel
1412

1513
from ...utils import transaction_on_commit
14+
from .. import settings as app_settings
15+
from ..tasks import auto_create_check
1616

1717

1818
class AbstractCheck(TimeStampedEditableModel):
@@ -33,7 +33,7 @@ class AbstractCheck(TimeStampedEditableModel):
3333
content_object = GenericForeignKey('content_type', 'object_id')
3434
check_type = models.CharField(
3535
_('check type'),
36-
choices=app_settings.CHECK_CLASSES,
36+
choices=app_settings.CHECK_CHOICES,
3737
db_index=True,
3838
max_length=128,
3939
)
@@ -114,43 +114,14 @@ def _auto_check_receiver(sender, instance, **kwargs):
114114
model = sender.__name__.lower()
115115
app_label = sender._meta.app_label
116116
object_id = str(instance.pk)
117-
if app_settings.AUTO_PING:
118-
auto_create_check.delay(
119-
model=model,
120-
app_label=app_label,
121-
object_id=object_id,
122-
check_type='openwisp_monitoring.check.classes.Ping',
123-
check_name='Ping',
124-
)
125-
if app_settings.AUTO_CONFIG_CHECK:
126-
auto_create_check.delay(
127-
model=model,
128-
app_label=app_label,
129-
object_id=object_id,
130-
check_type='openwisp_monitoring.check.classes.ConfigApplied',
131-
check_name='Configuration Applied',
132-
)
133-
if app_settings.AUTO_IPERF3:
134-
auto_create_check.delay(
135-
model=model,
136-
app_label=app_label,
137-
object_id=object_id,
138-
check_type='openwisp_monitoring.check.classes.Iperf3',
139-
check_name='Iperf3',
140-
)
141-
if app_settings.AUTO_WIFI_CLIENTS_CHECK:
142-
auto_create_check.delay(
143-
model=model,
144-
app_label=app_label,
145-
object_id=object_id,
146-
check_type='openwisp_monitoring.check.classes.WifiClients',
147-
check_name='WiFi Clients',
148-
)
149-
if app_settings.AUTO_DATA_COLLECTED_CHECK:
117+
118+
for class_string, name, auto_create_setting in app_settings.CHECK_CLASSES:
119+
if not getattr(app_settings, auto_create_setting):
120+
continue
150121
auto_create_check.delay(
151122
model=model,
152123
app_label=app_label,
153124
object_id=object_id,
154-
check_type='openwisp_monitoring.check.classes.DataCollected',
155-
check_name='Monitoring Data Collected',
125+
check_type=class_string,
126+
check_name=name,
156127
)

openwisp_monitoring/check/migrations/0001_initial_squashed_0002_check_unique_together.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import swapper
1111
from django.db import migrations, models
1212

13-
from ..settings import CHECK_CLASSES
13+
from ..settings import CHECK_CHOICES
1414

1515

1616
class Migration(migrations.Migration):
@@ -58,7 +58,7 @@ class Migration(migrations.Migration):
5858
(
5959
'check',
6060
models.CharField(
61-
choices=CHECK_CLASSES,
61+
choices=CHECK_CHOICES,
6262
db_index=True,
6363
help_text='Select check type',
6464
max_length=128,

openwisp_monitoring/check/migrations/0004_rename_active_to_is_active.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.db import migrations, models
44

5-
from ..settings import CHECK_CLASSES
5+
from ..settings import CHECK_CHOICES
66

77

88
class Migration(migrations.Migration):
@@ -26,7 +26,7 @@ class Migration(migrations.Migration):
2626
model_name='check',
2727
name='check',
2828
field=models.CharField(
29-
choices=CHECK_CLASSES,
29+
choices=CHECK_CHOICES,
3030
db_index=True,
3131
max_length=128,
3232
verbose_name='check type',

openwisp_monitoring/check/settings.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
from django.conf import settings
2+
from django.utils.translation import gettext_lazy as _
23

34
from ..settings import get_settings_value
45

56
CHECK_CLASSES = get_settings_value(
67
'CHECK_CLASSES',
78
(
8-
('openwisp_monitoring.check.classes.Ping', 'Ping'),
9-
('openwisp_monitoring.check.classes.ConfigApplied', 'Configuration Applied'),
10-
('openwisp_monitoring.check.classes.Iperf3', 'Iperf3'),
11-
('openwisp_monitoring.check.classes.WifiClients', 'WiFi Clients'),
9+
('openwisp_monitoring.check.classes.Ping', _('Ping'), 'AUTO_PING'),
10+
(
11+
'openwisp_monitoring.check.classes.ConfigApplied',
12+
_('Configuration Applied'),
13+
'AUTO_CONFIG_CHECK',
14+
),
15+
('openwisp_monitoring.check.classes.Iperf3', 'Iperf3', 'AUTO_IPERF3'),
16+
(
17+
'openwisp_monitoring.check.classes.WifiClients',
18+
'WiFi Clients',
19+
'AUTO_WIFI_CLIENTS_CHECK',
20+
),
1221
(
1322
'openwisp_monitoring.check.classes.DataCollected',
1423
'Monitoring Data Collected',
24+
'AUTO_DATA_COLLECTED_CHECK',
1525
),
1626
),
1727
)
28+
29+
CHECK_CHOICES = []
30+
CHECK_LIST = []
31+
for class_string, name, setting_name in CHECK_CLASSES:
32+
CHECK_CHOICES.append((class_string, name))
33+
CHECK_LIST.append(class_string)
34+
1835
AUTO_PING = get_settings_value('AUTO_PING', True)
1936
AUTO_CONFIG_CHECK = get_settings_value('AUTO_DEVICE_CONFIG_CHECK', True)
2037
# If OPENWISP_MONITORING_MANAGEMENT_IP_ONLY is not configured, use
@@ -44,7 +61,6 @@
4461
'IPERF3_CHECK_LOCK_EXPIRE', 10 * 60
4562
) # 10 minutes arbitrarily chosen (must be longer than TCP + UDP test time)
4663
IPERF3_CHECK_DELETE_RSA_KEY = get_settings_value('IPERF3_CHECK_DELETE_RSA_KEY', True)
47-
CHECKS_LIST = get_settings_value('CHECK_LIST', list(dict(CHECK_CLASSES).keys()))
4864
CONFIG_CHECK_INTERVAL = int(
4965
get_settings_value('CONFIG_CHECK_INTERVAL', 5)
5066
) # in minutes

openwisp_monitoring/check/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from openwisp_utils.tasks import OpenwispCeleryTask
1212

13-
from .settings import CHECKS_LIST
13+
from . import settings as app_settings
1414

1515
logger = logging.getLogger(__name__)
1616

@@ -31,15 +31,15 @@ def run_checks(checks=None):
3131
"""
3232
# If checks is None, We should execute all the checks
3333
if checks is None:
34-
checks = CHECKS_LIST
34+
checks = app_settings.CHECK_LIST
3535

3636
if not isinstance(checks, list):
3737
raise ImproperlyConfigured(
3838
f'Check path {checks} should be of type "list"'
3939
) # pragma: no cover
40-
if not all(check_path in CHECKS_LIST for check_path in checks):
40+
if not all(check_path in app_settings.CHECK_LIST for check_path in checks):
4141
raise ImproperlyConfigured(
42-
f'Check path {checks} should be in {CHECKS_LIST}'
42+
f'Check path {checks} should be listed in CHECK_CLASSES'
4343
) # pragma: no cover
4444

4545
runnable_checks = []

openwisp_monitoring/check/tests/test_ping.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ... import settings as monitoring_settings
88
from ...device import settings as device_settings
99
from ...device.tests import TestDeviceMonitoringMixin
10-
from .. import settings
10+
from .. import settings as app_settings
1111
from ..classes import Ping
1212
from ..classes.ping import get_ping_schema
1313
from ..exceptions import OperationalError
@@ -20,7 +20,7 @@
2020

2121

2222
class TestPing(TestDeviceMonitoringMixin, TransactionTestCase):
23-
_PING = settings.CHECK_CLASSES[0][0]
23+
_PING = app_settings.CHECK_CLASSES[0][0]
2424
_RESULT_KEYS = ['reachable', 'loss', 'rtt_min', 'rtt_avg', 'rtt_max']
2525
_RTT_KEYS = _RESULT_KEYS[-3:]
2626
_UNRECOGNIZED_OUTPUT = (
@@ -63,7 +63,7 @@ def test_check_ping_params(self):
6363
self.assertTrue(result[key] < 1)
6464

6565
@patch.object(
66-
settings,
66+
app_settings,
6767
'PING_CHECK_CONFIG',
6868
{
6969
'timeout': {'default': '10000'},

openwisp_monitoring/check/tests/test_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from .. import settings as app_settings
1010
from ..checks import check_wifi_clients_snooze_schedule
1111
from ..classes import Ping
12-
from ..settings import CHECK_CLASSES
1312
from ..tasks import perform_check
1413
from ..utils import run_checks_async
1514
from . import _FPING_REACHABLE
@@ -18,7 +17,7 @@
1817

1918

2019
class TestUtils(TestDeviceMonitoringMixin, TransactionTestCase):
21-
_PING = CHECK_CLASSES[0][0]
20+
_PING = app_settings.CHECK_CLASSES[0][0]
2221

2322
def _create_check(self):
2423
device = self._create_device(organization=self._create_org())

openwisp_monitoring/check/tests/test_wifi_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from swapper import load_model
99

1010
from ...device.tests import TestDeviceMonitoringMixin
11-
from .. import settings
1211
from .. import settings as app_settings
1312
from .. import tasks
1413
from ..classes import WifiClients
@@ -26,7 +25,7 @@ class TestWifiClient(
2625
TestDeviceMonitoringMixin,
2726
TransactionTestCase,
2827
):
29-
_WIFI_CLIENTS = settings.CHECK_CLASSES[3][0]
28+
_WIFI_CLIENTS = app_settings.CHECK_CLASSES[3][0]
3029

3130
def _run_wifi_clients_checks(self):
3231
tasks.run_checks(checks=[self._WIFI_CLIENTS])

openwisp_monitoring/device/base/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from openwisp_controller.config.validators import mac_address_validator
2626
from openwisp_utils.base import TimeStampedEditableModel
2727

28-
from ...check.settings import CHECKS_LIST
28+
from ...check import settings as check_settings
2929
from ...db import device_data_query, timeseries_db
3030
from ...monitoring.signals import threshold_crossed
3131
from ...monitoring.tasks import _timeseries_write
@@ -392,7 +392,7 @@ def get_active_metrics(cls):
392392
"""
393393
if not hasattr(cls, '_active_metrics'):
394394
active_metrics = []
395-
for check in CHECKS_LIST:
395+
for check in check_settings.CHECK_LIST:
396396
Check = import_string(check)
397397
active_metrics.extend(Check.get_related_metrics())
398398
cls._active_metrics = active_metrics

tests/openwisp2/sample_check/migrations/0001_initial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import model_utils.fields
1010
from django.db import migrations, models
1111

12-
from openwisp_monitoring.check.settings import CHECK_CLASSES
12+
from openwisp_monitoring.check.settings import CHECK_CHOICES
1313

1414

1515
class Migration(migrations.Migration):
@@ -66,7 +66,7 @@ class Migration(migrations.Migration):
6666
(
6767
'check_type',
6868
models.CharField(
69-
choices=CHECK_CLASSES,
69+
choices=CHECK_CHOICES,
7070
db_index=True,
7171
max_length=128,
7272
verbose_name='check type',

0 commit comments

Comments
 (0)