• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openwisp / openwisp-monitoring / 29622336079

18 Jul 2026 12:06AM UTC coverage: 98.588% (+0.002%) from 98.586%
29622336079

Pull #832

github

web-flow
Merge 28d967222 into 30fc297ca
Pull Request #832: [backport] #831: [fix] Fixed devices stuck in PROBLEM status #830 (to 1.2)

3840 of 3895 relevant lines covered (98.59%)

11.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
openwisp_monitoring/device/tasks.py
1
import logging
12✔
2
import warnings
12✔
3

4
from celery import shared_task
12✔
5
from django.core.exceptions import ObjectDoesNotExist
12✔
6
from django.utils.timezone import now, timedelta
12✔
7
from swapper import load_model
12✔
8

9
from openwisp_utils.tasks import OpenwispCeleryTask
12✔
10

11
from ..check.tasks import perform_check
12✔
12

13
logger = logging.getLogger(__name__)
12✔
14

15

16
@shared_task(base=OpenwispCeleryTask)
12✔
17
def trigger_device_critical_checks(pk, recovery=True):
12✔
18
    """Triggers the monitoring checks for the specified device pk.
19

20
    Retrieves all related checks to the passed ``device`` and calls the
21
    ``perform_check`` task from each of them.
22

23
    If no check exists changes the status according to the ``recovery``
24
    argument.
25
    """
26
    DeviceData = load_model("device_monitoring", "DeviceData")
12✔
27
    try:
12✔
28
        device = DeviceData.objects.select_related("monitoring").get(pk=pk)
12✔
29
    except ObjectDoesNotExist:
12✔
30
        logger.warning(f"The device with uuid {pk} has been deleted")
12✔
31
        return
12✔
32
    check_ids = list(
12✔
33
        device.checks.filter(
34
            is_active=True, check_type__in=device.monitoring.get_critical_checks()
35
        ).values_list("id", flat=True)
36
    )
37
    if not check_ids:
12✔
38
        status = "ok" if recovery else "critical"
12✔
39
        device.monitoring.update_status(status)
12✔
40
        return
12✔
41
    if recovery and device.monitoring.status == "critical":
12✔
42
        status = device.monitoring._get_status_from_metrics()
12✔
43
        # If metrics no longer imply critical status, move to problem.
44
        # The checks triggered below will confirm if the device is ok.
45
        if status != "critical":
12✔
46
            device.monitoring.update_status("problem")
12✔
47
    for check_id in check_ids:
12✔
48
        perform_check.delay(check_id)
12✔
49

50

51
@shared_task(base=OpenwispCeleryTask)
12✔
52
def trigger_device_checks(pk, recovery=True):
12✔
53
    """
54
    Deprecated, use trigger_device_critical_checks instead.
55
    """
56
    warnings.warn(
12✔
57
        "trigger_device_checks is deprecated, use trigger_device_critical_checks instead.",
58
        DeprecationWarning,
59
        stacklevel=2,
60
    )
61
    trigger_device_critical_checks(pk, recovery)
12✔
62

63

64
@shared_task(base=OpenwispCeleryTask)
12✔
65
def delete_wifi_clients_and_sessions(days=6 * 30):
12✔
66
    WifiClient = load_model("device_monitoring", "WifiClient")
12✔
67
    WifiSession = load_model("device_monitoring", "WifiSession")
12✔
68

69
    WifiSession.objects.filter(start_time__lte=(now() - timedelta(days=days))).delete()
12✔
70
    WifiClient.objects.exclude(
12✔
71
        mac_address__in=WifiSession.objects.values_list("wifi_client")
72
    ).delete()
73

74

75
@shared_task(base=OpenwispCeleryTask)
12✔
76
def offline_device_close_session(device_id):
12✔
77
    WifiSession = load_model("device_monitoring", "WifiSession")
12✔
78
    WifiSession.objects.filter(device_id=device_id, stop_time__isnull=True).update(
12✔
79
        stop_time=now()
80
    )
81

82

83
@shared_task(base=OpenwispCeleryTask)
12✔
84
def write_device_metrics(pk, data, time=None, current=False):
12✔
85
    DeviceData = load_model("device_monitoring", "DeviceData")
12✔
86
    try:
12✔
87
        device_data = DeviceData.get_devicedata(str(pk))
12✔
88
    except DeviceData.DoesNotExist:
×
89
        return
×
90
    device_data.writer.write(data, time, current)
12✔
91

92

93
@shared_task(base=OpenwispCeleryTask)
12✔
94
def handle_disabled_organization(organization_id):
12✔
95
    DeviceMonitoring = load_model("device_monitoring", "DeviceMonitoring")
12✔
96
    DeviceMonitoring.handle_disabled_organization(organization_id)
12✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc