Bugfixes and Dockerfile
This commit is contained in:
parent
aefde5013a
commit
311a454ab0
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@ -0,0 +1,14 @@
|
||||
FROM python3
|
||||
|
||||
ENV TZ=Europe/Luxembourg
|
||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||
|
||||
RUN mkdir /app
|
||||
|
||||
COPY requirements.txt /app
|
||||
COPY main.py /app
|
||||
WORKDIR /app
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
CMD ['python', "main.py"]
|
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Perma Alert
|
||||
|
||||
System to forward Alarmdepesche from CGDIS
|
||||
|
28
main.py
28
main.py
@ -28,6 +28,9 @@ class PrometheusExporter():
|
||||
|
||||
metrics = []
|
||||
|
||||
def __init__(self):
|
||||
self.metrics = []
|
||||
|
||||
def append_gauge_metric(self, metric_name, metric, help_text=""):
|
||||
if help_text != "":
|
||||
self.metrics.append(f"#HELP {metric_name} {help_text}")
|
||||
@ -35,7 +38,7 @@ class PrometheusExporter():
|
||||
self.metrics.append(f"{metric_name} {metric}")
|
||||
|
||||
def write(self):
|
||||
with open("permalaert.prom", "w") as file:
|
||||
with open("data/permalaert.prom", "w") as file:
|
||||
for metric in self.metrics:
|
||||
file.write(f"{metric}\n")
|
||||
self.metrics = []
|
||||
@ -43,6 +46,8 @@ class PrometheusExporter():
|
||||
|
||||
class FetchEmail():
|
||||
|
||||
metric_success = 0
|
||||
|
||||
connection = None
|
||||
is_close: bool = False
|
||||
error = None
|
||||
@ -52,8 +57,10 @@ class FetchEmail():
|
||||
try:
|
||||
self.connection = imaplib.IMAP4_SSL(mail_server)
|
||||
self.connection.login(username, password)
|
||||
self.metric_success = 1
|
||||
except Exception as err:
|
||||
logger.error(f"Couldn't connect to imap server. Got: {err}")
|
||||
self.metric_success = 0
|
||||
|
||||
def __del__(self):
|
||||
pass
|
||||
@ -145,6 +152,8 @@ class Alarmdepesche():
|
||||
|
||||
raw: BeautifulSoup
|
||||
|
||||
metric_api_up = 0
|
||||
|
||||
def get_string(self, td, clean=True):
|
||||
string = td.parent.find_all('td')[-1].string
|
||||
if clean:
|
||||
@ -153,6 +162,7 @@ class Alarmdepesche():
|
||||
return string
|
||||
|
||||
def get_members_emails(self):
|
||||
self.metric_api_up = 0
|
||||
logger.info("Getting duty from API")
|
||||
members = []
|
||||
url = os.environ.get("DUTY_API_URL") + f"?vehicle={self.engine}"
|
||||
@ -167,6 +177,7 @@ class Alarmdepesche():
|
||||
logger.debug(f"Got JSON result: {json_res}")
|
||||
for member in json_res["vehicles"][0]["contacts"]:
|
||||
members.append(member["mail"].lower())
|
||||
self.metric_api_up = 1
|
||||
|
||||
except requests.exceptions.JSONDecodeError as e:
|
||||
logger.error(f"Cannot get data from perma api. Got {e}")
|
||||
@ -250,12 +261,12 @@ class Alarmdepesche():
|
||||
self.geo_point = json_data["intervention_coordinates"]
|
||||
|
||||
|
||||
def loadFromDisk(self, file_path="last_alarmdepesche.json"):
|
||||
def loadFromDisk(self, file_path="data/last_alarmdepesche.json"):
|
||||
logger.debug(f"Loading last Alarmdepesche from disk")
|
||||
with open(file_path, "r") as file:
|
||||
self.fromDict(json.load(file))
|
||||
|
||||
def saveToDisk(self, file_path="last_alarmdepesche.json"):
|
||||
def saveToDisk(self, file_path="data/last_alarmdepesche.json"):
|
||||
logger.debug(f"Writing {self} to disk")
|
||||
with open(file_path, "w") as file:
|
||||
json.dump(self.toDict(), file, indent=3)
|
||||
@ -324,6 +335,8 @@ class Email():
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
os.makedirs('data/', exist_ok=True)
|
||||
|
||||
mail_server = os.environ.get('MAIL_SERVER', None)
|
||||
mail_user = os.environ.get('MAIL_USER', None)
|
||||
mail_from = os.environ.get('MAIL_FROM', mail_user)
|
||||
@ -333,13 +346,15 @@ if __name__ == '__main__':
|
||||
logger.error("Environment variables not set!, exiting...")
|
||||
exit(1)
|
||||
|
||||
metric_api_up = 1
|
||||
|
||||
try:
|
||||
while True:
|
||||
timer_global = Timer()
|
||||
prometheus = PrometheusExporter()
|
||||
|
||||
now = datetime.datetime.now()
|
||||
if os.path.exists("last_alarmdepesche.json"):
|
||||
if os.path.exists("data/last_alarmdepesche.json"):
|
||||
last_alarmdepsche = Alarmdepesche()
|
||||
last_alarmdepsche.loadFromDisk()
|
||||
last_alarmdepsche_in_seconds = (now - last_alarmdepsche.timestamp).total_seconds()
|
||||
@ -353,6 +368,7 @@ if __name__ == '__main__':
|
||||
current_alarmdepsche = Alarmdepesche()
|
||||
current_alarmdepsche.from_html(html_message)
|
||||
current_alarmdepsche.saveToDisk()
|
||||
metric_api_up = current_alarmdepsche.metric_api_up
|
||||
members = current_alarmdepsche.get_members_emails()
|
||||
sendmail = Email(mail_server, 587, mail_user, mail_pass)
|
||||
for email_addr in members:
|
||||
@ -363,6 +379,10 @@ if __name__ == '__main__':
|
||||
timer_global.end()
|
||||
prometheus.append_gauge_metric("permaalert_execution_time", timer_global.took,
|
||||
help_text="Program execution time in seconds")
|
||||
prometheus.append_gauge_metric("permaalert_duty_api_up", metric_api_up,
|
||||
help_text="Boolean if api is up")
|
||||
prometheus.append_gauge_metric("permaalert_mailserver_up", fetchmail.metric_success,
|
||||
help_text="Boolean if mail server is up")
|
||||
prometheus.write()
|
||||
|
||||
del prometheus
|
||||
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
beautifulsoup4==4.11.1
|
||||
requests==2.28.1
|
||||
utm==0.7.0
|
Loading…
x
Reference in New Issue
Block a user