(late) init
This commit is contained in:
commit
6d8eef6572
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.sqlite3
|
||||
venv
|
86
ED_servers_monitor.py
Executable file
86
ED_servers_monitor.py
Executable file
@ -0,0 +1,86 @@
|
||||
#!/usr/bin/python3
|
||||
import json
|
||||
import logging
|
||||
import sqlite3
|
||||
from os import environ
|
||||
from sys import stdout
|
||||
|
||||
import requests
|
||||
|
||||
# First version of script is dated by Fri Mar 19 00:03:08 2021 +0300
|
||||
|
||||
url = environ["DISCORD_WEBHOOK_URL"]
|
||||
|
||||
# setting up logging
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.INFO)
|
||||
stdout_handler = logging.StreamHandler(stdout)
|
||||
logger.addHandler(stdout_handler)
|
||||
|
||||
|
||||
def normalize_status(status_raw):
|
||||
status = json.loads(status_raw)
|
||||
return status["status"]
|
||||
# if status['code'] == 0:
|
||||
# return 'OK'
|
||||
|
||||
# else:
|
||||
# return status['status']
|
||||
|
||||
|
||||
sqlite_connection = sqlite3.connect("ED_servers_status.sqlite3")
|
||||
|
||||
with sqlite_connection:
|
||||
try:
|
||||
logger.debug("Trying to create the table")
|
||||
sqlite_connection.execute(
|
||||
"create table servers_status (code int, status text, Timestamp DATATIME DEFAULT CURRENT_TIMESTAMP);"
|
||||
)
|
||||
logger.debug("Table created")
|
||||
except sqlite3.OperationalError:
|
||||
logger.debug("Table already exists")
|
||||
|
||||
logger.debug("Making status request")
|
||||
status_request = requests.get("https://ed-server-status.orerve.net")
|
||||
|
||||
logger.debug(f"Request result: {status_request.text}")
|
||||
logger.debug(f"Request http code: {status_request.status_code}")
|
||||
|
||||
with sqlite_connection:
|
||||
sqlite_connection.execute(
|
||||
"insert into servers_status (code, status) values (?, ?);",
|
||||
[status_request.status_code, status_request.text],
|
||||
)
|
||||
|
||||
logger.debug("Inserted")
|
||||
|
||||
# part with alerting
|
||||
logger.debug("Diffing status")
|
||||
|
||||
with sqlite_connection:
|
||||
old_status_raw = sqlite_connection.execute(
|
||||
"select status from servers_status order by Timestamp desc limit 1 offset 1;"
|
||||
).fetchone()[0]
|
||||
sqlite_connection.close()
|
||||
|
||||
"""
|
||||
old statuses for old endpoint
|
||||
{"text":"OK","status":2}
|
||||
{"text":"Offline","status":0}
|
||||
{"text":"Scheduled Maintenance","status":1}
|
||||
{"text":"Upgrading","status":1}
|
||||
"""
|
||||
|
||||
old_status_text = normalize_status(old_status_raw)
|
||||
new_status_text = normalize_status(status_request.text)
|
||||
|
||||
logger.debug(f"Old status: {old_status_text}, New status: {new_status_text}")
|
||||
|
||||
if new_status_text != old_status_text:
|
||||
message = f"Servers status was changed: {old_status_text} -> {new_status_text}"
|
||||
logger.info(message)
|
||||
r = requests.post(
|
||||
url=url,
|
||||
data=f"content=```{requests.utils.quote(message)}```",
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user