From 522022f56d1983f2a6eaf019a534a840049aff84 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Fri, 27 Jun 2025 19:30:50 +0300 Subject: [PATCH] Update github --- .woodpecker.yml | 2 +- ...t-checker.py => external_alert_checker.py} | 0 .../alerts/generate_monitor_workflow.py | 115 ++++++++++++++++++ ...pe-config.py => generate_scrape_config.py} | 0 4 files changed, 116 insertions(+), 1 deletion(-) rename automation/alerts/{external-alert-checker.py => external_alert_checker.py} (100%) create mode 100644 automation/alerts/generate_monitor_workflow.py rename automation/prometheus/{generate-scrape-config.py => generate_scrape_config.py} (100%) diff --git a/.woodpecker.yml b/.woodpecker.yml index 9ff7d34..3ed32a3 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -36,7 +36,7 @@ steps: commands: - apk add --no-cache git curl bash - pip install pyyaml requests - - python3 automation/alerts/external-alert-checker.py + - python3 automation/alerts/external_alert_checker.py environment: PUSHOVER_USER: from_secret: PUSHOVER_USER diff --git a/automation/alerts/external-alert-checker.py b/automation/alerts/external_alert_checker.py similarity index 100% rename from automation/alerts/external-alert-checker.py rename to automation/alerts/external_alert_checker.py diff --git a/automation/alerts/generate_monitor_workflow.py b/automation/alerts/generate_monitor_workflow.py new file mode 100644 index 0000000..11b3900 --- /dev/null +++ b/automation/alerts/generate_monitor_workflow.py @@ -0,0 +1,115 @@ +import os +import yaml +import requests +import subprocess + +REPOS = { + "dev-tools": "https://git.dvirlabs.com/dvirlabs/dev-tools.git", + "infra": "https://git.dvirlabs.com/dvirlabs/infra.git" +} +GITHUB_REPO = "git@github.com:dvirh/lab-monitor.git" +BASE_DIR = "./repos" +WORKFLOW_FILE = "./repos/lab-monitor/.github/workflows/monitor.yml" + +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 extract_urls(): + urls = [] + for repo in REPOS: + manifests_path = os.path.join(BASE_DIR, repo, "manifests") + if not os.path.isdir(manifests_path): + continue + for app in os.listdir(manifests_path): + path = os.path.join(manifests_path, app, "monitoring.yaml") + if not os.path.exists(path): + continue + with open(path) as f: + cfg = yaml.safe_load(f) + if not cfg.get("enabled"): + continue + ext = cfg.get("external_check") + if not ext or not ext.get("url"): + continue + app_name = cfg.get("app", app) + for alert in ext.get("alerts", []): + urls.append({ + "url": ext["url"], + "name": app_name, + "code": alert["code"], + "message": alert["message"], + "severity": alert.get("severity", "info") + }) + return urls + +def severity_to_priority(sev): + return { + "critical": 2, + "high": 1, + "warning": 0, + "info": 0 + }.get(sev.lower(), 0) + +def generate_workflow(urls): + os.makedirs(os.path.dirname(WORKFLOW_FILE), exist_ok=True) + with open(WORKFLOW_FILE, "w") as f: + f.write("""name: Monitor Lab URLs + +on: + schedule: + - cron: "*/5 * * * *" + workflow_dispatch: + +jobs: + monitor: + runs-on: ubuntu-latest + steps: + - name: Check all URLs + run: | + check_url() { + URL=$1 + NAME=$2 + CODE=$3 + MESSAGE=$4 + PRIORITY=$5 + STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL") + TIME=$(date "+%Y-%m-%d %H:%M:%S") + + if [[ "$STATUS" == "$CODE" ]]; then + curl -s \\ + --form-string "token=${{ secrets.PUSHOVER_TOKEN }}" \\ + --form-string "user=${{ secrets.PUSHOVER_USER }}" \\ + --form-string "title=🔴 $NAME Alert" \\ + --form-string "message=$MESSAGE at $TIME" \\ + --form-string "priority=$PRIORITY" \\ + --form-string "retry=60" \\ + --form-string "expire=600" \\ + https://api.pushover.net/1/messages.json + else + echo "✅ $NAME is up: $STATUS" + fi + } +""") + for item in urls: + f.write(f' check_url "{item["url"]}" "{item["name"]}" "{item["code"]}" "{item["message"]}" "{severity_to_priority(item["severity"])}"\n') + +def push_workflow(): + repo_path = os.path.join(BASE_DIR, "lab-monitor") + if not os.path.exists(repo_path): + subprocess.run(["git", "clone", GITHUB_REPO, repo_path]) + subprocess.run(["git", "-C", repo_path, "pull"]) + subprocess.run(["git", "-C", repo_path, "add", ".github/workflows/monitor.yml"]) + subprocess.run(["git", "-C", repo_path, "commit", "-m", "update monitor.yml from monitoring.yaml"]) + subprocess.run(["git", "-C", repo_path, "push"]) + +if __name__ == "__main__": + clone_repos() + urls = extract_urls() + generate_workflow(urls) + push_workflow() diff --git a/automation/prometheus/generate-scrape-config.py b/automation/prometheus/generate_scrape_config.py similarity index 100% rename from automation/prometheus/generate-scrape-config.py rename to automation/prometheus/generate_scrape_config.py