Esempio n. 1
0
    def test_warn_observer(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=5)
        Alert.set(2, observer=observer)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_WAITING
        assert mock.call_count == 1
Esempio n. 2
0
    def test_warn_notified_identifier(self, mock):
        alert = AlertFactory.create(state=Alert.STATE_NOTIFIED)
        Alert.set(identifier=alert.identifier)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 0
Esempio n. 3
0
    def test_no_warning_period(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=0)
        Alert.set(2, observer=observer)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 1
Esempio n. 4
0
    def test_clear_observer(self, mock):
        observer = ObserverFactory.create()
        AlertFactory.create(identifier=observer.get_alert_identifier())
        Alert.clear(observer.value, observer=observer)

        assert Alert.objects.all().count() == 0
        assert mock.call_count == 1
Esempio n. 5
0
    def test_clear(self, mock):
        observer = ObserverFactory.create()
        AlertFactory.create(observer=observer)
        Alert.clear(observer, observer.value)

        assert Alert.objects.all().count() == 0
        assert mock.call_count == 1
Esempio n. 6
0
    def test_warn_critical_waiting_priod_not_achieved(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=5)
        AlertFactory.create(identifier=observer.get_alert_identifier())
        Alert.set(2, observer=observer)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_WAITING
        assert mock.call_count == 0
Esempio n. 7
0
    def test_warn_identifier(self, mock):
        Alert.set(2, identifier='alert-name', waiting_period=5)

        assert Alert.objects.all().count() == 1
        alert = Alert.objects.first()
        assert alert.state == Alert.STATE_WAITING
        assert alert.identifier == 'alert-name'
        assert mock.call_count == 1
Esempio n. 8
0
    def test_warn_notified_observer(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=0)
        AlertFactory.create(
            identifier=observer.get_alert_identifier(), state=Alert.STATE_NOTIFIED)
        Alert.set(2, observer=observer)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 0
Esempio n. 9
0
    def test_warn_critical(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=0)
        AlertFactory.create(observer=observer, state=Alert.STATE_WAITING)

        Alert.set(observer, 2)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 1
Esempio n. 10
0
    def test_warn_every_time(self, mock):
        observer = ObserverFactory.create(value=4, waiting_period=0, alert_every_time=True)
        AlertFactory.create(observer=observer, state=Alert.STATE_NOTIFIED)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 0

        Alert.set(observer, 2)

        assert Alert.objects.all().count() == 1
        assert Alert.objects.first().state == Alert.STATE_NOTIFIED
        assert mock.call_count == 1
Esempio n. 11
0
def check_watchdogs():
    all_clear = True
    for watchdog in Watchdog.objects.all():
        if watchdog.max_age and watchdog.last_timestamp_delta > watchdog.max_age:
            Alert.set(watchdog.last_timestamp,
                      identifier='watchdog:{0}'.format(watchdog.pk),
                      title='OUTDATED: {0}'.format(watchdog.metric))
            all_clear = False
            continue
        else:
            Alert.clear(watchdog.last_timestamp,
                        identifier='watchdog:{0}'.format(watchdog.pk),
                        title='OK: {0}'.format(watchdog.metric))

        compare_value = watchdog.last_value
        if not watchdog.observer.compare(compare_value):
            all_clear = False

    return all_clear
Esempio n. 12
0
def check_watchdogs():
    all_clear = True
    for watchdog in Watchdog.objects.all():
        if watchdog.max_age and watchdog.last_timestamp_delta > watchdog.max_age:
            Alert.set(
                watchdog.last_timestamp,
                identifier='watchdog:{0}'.format(watchdog.pk),
                title='OUTDATED: {0}'.format(watchdog.metric)
            )
            all_clear = False
            continue
        else:
            Alert.clear(
                watchdog.last_timestamp,
                identifier='watchdog:{0}'.format(watchdog.pk),
                title='OK: {0}'.format(watchdog.metric)
            )

        compare_value = watchdog.last_value
        if not watchdog.observer.compare(compare_value):
            all_clear = False

    return all_clear
Esempio n. 13
0
    def handle(self, *args, **options):
        for watchdog in Watchdog.objects.all():
            if watchdog.must_checked:
                if watchdog.max_age and watchdog.last_timestamp_delta > watchdog.max_age:
                    Alert.set(
                        watchdog.last_timestamp,
                        identifier='watchdog_outdated:{0}'.format(watchdog.pk),
                        title='OUTDATED: {0}'.format(watchdog.metric)
                    )
                    continue
                else:
                    Alert.clear(
                        watchdog.last_timestamp,
                        identifier='watchdog_outdated:{0}'.format(watchdog.pk),
                        title='OK: {0}'.format(watchdog.metric),
                        watchdog=watchdog
                    )

                watchdog.observer.compare(
                    watchdog.last_value, metric=watchdog.metric, watchdog=watchdog,
                    identifier='watchdog:{0}'.format(watchdog.pk))

            if watchdog.is_time_based and not watchdog.must_checked:
                Alert.clear(
                    'Time is outside the test interval',
                    observer=watchdog.observer,
                    identifier='watchdog:{0}'.format(watchdog.pk),
                    metric=watchdog.metric,
                    watchdog=watchdog,
                )

            for plan in watchdog.emergencyplan_set.all():
                if plan.is_active:
                    operator_class = Observer.operators[plan.disable_observer.operator]
                    if operator_class(plan.disable_observer).compare(watchdog.last_value):
                        plan.switch.mode = plan.disable_mode
                        plan.switch.save(update_fields=['mode'])
                        plan.is_active = False
                        plan.save()
                else:
                    operator_class = Observer.operators[plan.enable_observer.operator]
                    if operator_class(plan.enable_observer).compare(watchdog.last_value):
                        plan.switch.mode = plan.enable_mode
                        plan.switch.save(update_fields=['mode'])
                        plan.is_active = True
                        plan.save()
Esempio n. 14
0
 def test_clear_observer_and_identifier_missing(self):
     with pytest.raises(ValueError):
         Alert.clear(2)
Esempio n. 15
0
 def test_set_observer_and_identifier_missing(self):
     with pytest.raises(ValueError):
         Alert.set(2)
Esempio n. 16
0
    def test_clear_identifier(self, mock):
        alert = AlertFactory.create()
        Alert.clear(identifier=alert.identifier)

        assert Alert.objects.all().count() == 0
        assert mock.call_count == 1