2022-05-08 17:20:04 +01:00
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
import requests
|
2022-05-16 13:41:17 +01:00
|
|
|
from requests import RequestException
|
2022-05-08 17:20:04 +01:00
|
|
|
|
2022-05-18 15:49:36 +01:00
|
|
|
from app.alarms import get_or_create_alarm
|
2022-05-08 17:20:04 +01:00
|
|
|
from app.extensions import db
|
2022-05-18 15:49:36 +01:00
|
|
|
from app.models.alarms import AlarmState
|
2022-05-08 17:20:04 +01:00
|
|
|
from app.models.mirrors import Proxy
|
|
|
|
from app.terraform import BaseAutomation
|
|
|
|
|
|
|
|
|
|
|
|
class AlarmProxyHTTPStatusAutomation(BaseAutomation):
|
|
|
|
short_name = "alarm_http_status"
|
|
|
|
description = "Check all deployed proxies for HTTP status code"
|
2022-05-13 09:28:54 +01:00
|
|
|
frequency = 45
|
2022-05-08 17:20:04 +01:00
|
|
|
|
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
2024-12-06 18:15:47 +00:00
|
|
|
proxies = Proxy.query.filter(Proxy.destroyed.is_(None))
|
2022-05-08 17:20:04 +01:00
|
|
|
for proxy in proxies:
|
|
|
|
try:
|
|
|
|
if proxy.url is None:
|
|
|
|
continue
|
2024-12-06 18:15:47 +00:00
|
|
|
r = requests.get(proxy.url, allow_redirects=False, timeout=5)
|
2022-05-08 17:20:04 +01:00
|
|
|
r.raise_for_status()
|
2022-05-18 15:49:36 +01:00
|
|
|
alarm = get_or_create_alarm(proxy.brn, "http-status")
|
2022-05-08 17:20:04 +01:00
|
|
|
if r.is_redirect:
|
2022-05-18 15:49:36 +01:00
|
|
|
alarm.update_state(
|
2024-12-06 18:15:47 +00:00
|
|
|
AlarmState.CRITICAL, f"{r.status_code} {r.reason}"
|
2022-05-08 17:20:04 +01:00
|
|
|
)
|
|
|
|
else:
|
2024-12-06 18:15:47 +00:00
|
|
|
alarm.update_state(AlarmState.OK, f"{r.status_code} {r.reason}")
|
2022-05-12 10:13:49 +01:00
|
|
|
except requests.HTTPError:
|
2022-05-19 12:26:16 +01:00
|
|
|
alarm = get_or_create_alarm(proxy.brn, "http-status")
|
2024-12-06 18:15:47 +00:00
|
|
|
alarm.update_state(AlarmState.CRITICAL, f"{r.status_code} {r.reason}")
|
2022-05-16 13:41:17 +01:00
|
|
|
except RequestException as e:
|
2022-05-19 12:26:16 +01:00
|
|
|
alarm = get_or_create_alarm(proxy.brn, "http-status")
|
2024-12-06 18:15:47 +00:00
|
|
|
alarm.update_state(AlarmState.CRITICAL, repr(e))
|
2022-05-18 15:49:36 +01:00
|
|
|
db.session.commit()
|
2022-05-12 10:13:49 +01:00
|
|
|
return True, ""
|