alarms: refactor the alarms subsystem
also include eotk alarms now
This commit is contained in:
parent
a935055083
commit
e2ce24bf3b
17 changed files with 288 additions and 152 deletions
|
@ -5,8 +5,9 @@ from typing import Dict, Tuple, Any
|
|||
|
||||
import requests
|
||||
|
||||
from app.alarms import get_or_create_alarm
|
||||
from app.extensions import db
|
||||
from app.models.alarms import Alarm, AlarmState
|
||||
from app.models.alarms import AlarmState
|
||||
from app.models.mirrors import Origin
|
||||
from app.terraform import BaseAutomation
|
||||
|
||||
|
@ -58,20 +59,6 @@ def threshold_origin(domain_name: str) -> Dict[str, Any]:
|
|||
return ooni
|
||||
|
||||
|
||||
def set_ooni_alarm(origin_id: int, country: str, state: AlarmState, text: str) -> None:
|
||||
alarm = Alarm.query.filter(
|
||||
Alarm.origin_id == origin_id,
|
||||
Alarm.alarm_type == f"origin-block-ooni-{country}"
|
||||
).first()
|
||||
if alarm is None:
|
||||
alarm = Alarm()
|
||||
alarm.origin_id = origin_id
|
||||
alarm.alarm_type = f"origin-block-ooni-{country}"
|
||||
alarm.target = "origin"
|
||||
db.session.add(alarm)
|
||||
alarm.update_state(state, text)
|
||||
|
||||
|
||||
class BlockOONIAutomation(BaseAutomation):
|
||||
short_name = "block_ooni"
|
||||
description = "Import origin and/or proxy reachability results from OONI"
|
||||
|
@ -82,5 +69,8 @@ class BlockOONIAutomation(BaseAutomation):
|
|||
for origin in origins:
|
||||
ooni = threshold_origin(origin.domain_name)
|
||||
for country in ooni:
|
||||
set_ooni_alarm(origin.id, country.lower(), ooni[country]["state"], ooni[country]["message"])
|
||||
alarm = get_or_create_alarm(origin.brn,
|
||||
f"origin-block-ooni-{country.lower()}")
|
||||
alarm.update_state(ooni[country]["state"], ooni[country]["message"])
|
||||
db.session.commit()
|
||||
return True, ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue