apps-gitops/automation/alerts/external-alert-checker.py
2025-06-27 19:17:06 +03:00

62 lines
1.9 KiB
Python

import requests
import yaml
import os
import subprocess
# === CONFIG ===
PUSHOVER_TOKEN = os.getenv("PUSHOVER_TOKEN")
PUSHOVER_USER = os.getenv("PUSHOVER_USER")
REPOS = {
"dev-tools": "https://git.dvirlabs.com/dvirlabs/dev-tools.git",
"infra": "https://git.dvirlabs.com/dvirlabs/infra.git"
}
BASE_DIR = "./repos"
def clone_repos():
os.makedirs(BASE_DIR, exist_ok=True)
for name, url in REPOS.items():
repo_path = os.path.join(BASE_DIR, name)
if os.path.exists(repo_path):
subprocess.run(["git", "-C", repo_path, "pull"])
else:
subprocess.run(["git", "clone", url, repo_path])
def send_pushover(message):
requests.post("https://api.pushover.net/1/messages.json", data={
"token": PUSHOVER_TOKEN,
"user": PUSHOVER_USER,
"message": message
})
def check_targets():
for repo in os.listdir(BASE_DIR):
repo_path = os.path.join(BASE_DIR, repo, "manifests")
if not os.path.isdir(repo_path):
continue
for app in os.listdir(repo_path):
file_path = os.path.join(repo_path, app, "monitoring.yaml")
if not os.path.exists(file_path):
continue
with open(file_path) as f:
config = yaml.safe_load(f)
if not config.get("enabled"):
continue
external = config.get("external_check")
if not external:
continue
url = external.get("url")
alerts = external.get("alerts", [])
try:
r = requests.get(url, timeout=5)
code = r.status_code
for alert in alerts:
if code == alert["code"]:
send_pushover(alert["message"])
break
except Exception as e:
send_pushover(f"{config.get('app')} unreachable: {e}")
if __name__ == "__main__":
clone_repos()
check_targets()