2021-09-16 22:41:29 +00:00
|
|
|
# Any alerts produced by the server.py layer. This likely means BIG issues.
|
|
|
|
import json
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from helpers.os_environment import resolve_external_file_path
|
|
|
|
from helpers.alert_manager import AlertProvider
|
|
|
|
from baps_types.alert import CRITICAL, WARNING, Alert
|
2021-09-21 23:57:51 +00:00
|
|
|
from baps_types.happytime import happytime
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
MODULE = "BAPSicleServer" # This should match the log file, so the UI will link to the logs page.
|
|
|
|
|
2021-09-16 22:41:29 +00:00
|
|
|
|
|
|
|
class ServerAlertProvider(AlertProvider):
|
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
_state: Dict[str, Any]
|
|
|
|
# To simplify monitoring (and allow detection of things going super
|
|
|
|
# weird), we are going to read from the state file to work out the alerts.
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
def get_alerts(self):
|
|
|
|
with open(resolve_external_file_path("state/BAPSicleServer.json")) as file:
|
|
|
|
self._state = json.loads(file.read())
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
funcs = [self._api_key, self._start_time]
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
alerts: List[Alert] = []
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
for func in funcs:
|
|
|
|
func_alerts = func()
|
|
|
|
if func_alerts:
|
|
|
|
alerts.extend(func_alerts)
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
return alerts
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
def _api_key(self):
|
|
|
|
if not self._state["myradio_api_key"]:
|
|
|
|
return [Alert({
|
|
|
|
"start_time": -1, # Now
|
|
|
|
"id": "api_key_missing",
|
|
|
|
"title": "MyRadio API Key is not configured.",
|
2021-09-22 19:14:58 +00:00
|
|
|
"description": "This means you will be unable to load show plans, audio items, or tracklist. \
|
|
|
|
Please set one on the 'Server Config' page.",
|
2021-09-22 18:49:24 +00:00
|
|
|
"module": MODULE,
|
|
|
|
"severity": CRITICAL
|
|
|
|
})]
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
if len(self._state["myradio_api_key"]) < 10:
|
|
|
|
return [Alert({
|
|
|
|
"start_time": -1,
|
|
|
|
"id": "api_key_missing",
|
|
|
|
"title": "MyRadio API Key seems incorrect.",
|
2021-09-22 19:14:58 +00:00
|
|
|
"description": "The API key is less than 10 characters, it's probably not a valid one. \
|
|
|
|
If it is valid, it shouldn't be.",
|
2021-09-22 18:49:24 +00:00
|
|
|
"module": MODULE,
|
|
|
|
"severity": WARNING
|
|
|
|
})]
|
2021-09-16 22:41:29 +00:00
|
|
|
|
2021-09-22 18:49:24 +00:00
|
|
|
def _start_time(self):
|
|
|
|
start_time = self._state["start_time"]
|
|
|
|
start_time = datetime.fromtimestamp(start_time)
|
|
|
|
delta = timedelta(
|
|
|
|
days=1,
|
|
|
|
)
|
|
|
|
if (start_time + delta > datetime.now()):
|
|
|
|
return [Alert({
|
|
|
|
"start_time": -1,
|
|
|
|
"id": "server_restarted",
|
|
|
|
"title": "BAPSicle restarted recently.",
|
|
|
|
"description":
|
|
|
|
"""The BAPSicle server restarted at {}, less than a day ago.
|
2021-09-16 22:41:29 +00:00
|
|
|
|
|
|
|
It may have been automatically restarted by the OS.
|
|
|
|
|
|
|
|
If this is not expected, please check logs to investigate why BAPSicle restarted/crashed."""
|
2021-09-22 18:49:24 +00:00
|
|
|
.format(happytime(start_time)),
|
|
|
|
"module": MODULE,
|
|
|
|
"severity": WARNING
|
|
|
|
})]
|