1
0
Fork 0
llvm-premerge-checks/scripts/metrics/server_monitoring.py

77 lines
2.4 KiB
Python
Raw Permalink Normal View History

2021-05-07 08:42:16 +02:00
#!/usr/bin/env python3
import traceback
2021-05-07 08:42:16 +02:00
import psycopg2
from phabricator import Phabricator
import os
from typing import Optional
import datetime
import requests
import logging
2021-05-07 08:42:16 +02:00
PHABRICATOR_URL = "https://reviews.llvm.org/api/"
BUILDBOT_URL = "https://lab.llvm.org/buildbot/api/v2"
2021-05-07 08:42:16 +02:00
def phab_up() -> Optional[Phabricator]:
"""Try to connect to phabricator to see if the server is up.
Returns None if server is down.
"""
logging.info("Checking Phabricator status...")
2021-05-07 08:42:16 +02:00
try:
phab = Phabricator(token=os.getenv('CONDUIT_TOKEN'), host=PHABRICATOR_URL)
2021-05-07 08:42:16 +02:00
phab.update_interfaces()
logging.info("Phabricator is up.")
2021-05-07 08:42:16 +02:00
return phab
except Exception as ex:
logging.error(ex)
logging.error(traceback.format_exc())
logging.warning("Phabricator is down.")
2021-05-07 08:42:16 +02:00
return None
def buildbot_up() -> bool:
"""Check if buildbot server is up"""
logging.info("Checking Buildbot status...")
2021-05-07 08:42:16 +02:00
try:
response = requests.get(BUILDBOT_URL)
logging.info(f'{response.status_code} {BUILDBOT_URL}')
logging.info(response.content)
return response.status_code == 200
except Exception as ex:
logging.error(ex)
logging.error(traceback.format_exc())
logging.warning("Buildbot is down.")
2021-05-07 08:42:16 +02:00
return False
def log_server_status(phab: bool, buildbot: bool, conn: psycopg2.extensions.connection):
"""log the phabricator status to the database."""
logging.info("Writing Phabricator status to database...")
2021-05-07 08:42:16 +02:00
cur = conn.cursor()
cur.execute(
"INSERT INTO server_status (timestamp, phabricator, buildbot) VALUES (%s,%s,%s);",
(datetime.datetime.now(), phab, buildbot),
)
conn.commit()
def connect_to_db() -> psycopg2.extensions.connection:
"""Connect to the database, create tables as needed."""
conn = psycopg2.connect(
2021-06-18 12:21:41 +02:00
f"host=127.0.0.1 sslmode=disable dbname=stats user=stats password={os.getenv('DB_PASSWORD')}")
2021-05-07 08:42:16 +02:00
cur = conn.cursor()
cur.execute(
"CREATE TABLE IF NOT EXISTS server_status (timestamp timestamp, phabricator boolean, buildbot boolean);"
)
conn.commit()
return conn
if __name__ == "__main__":
logging.basicConfig(level='INFO', format='%(levelname)-7s %(message)s')
2021-05-07 08:42:16 +02:00
conn = connect_to_db()
phab = phab_up()
buildbot = buildbot_up()
log_server_status(phab is not None, buildbot, conn)