From 98180a0f6b8b1bcb9d32c04bf1f2a2d4db36d9ee Mon Sep 17 00:00:00 2001 From: Ronaldo Dias Date: Fri, 5 Jun 2026 11:49:57 -0300 Subject: [PATCH] Initial commit Co-Authored-By: Claude Sonnet 4.6 --- README.md | 37 ++++++++++++++++++++++++++++++ healthcheck.py | 41 ++++++++++++++++++++++++++++++++++ install_systemd.sh | 14 ++++++++++++ systemd/chamado-health.service | 14 ++++++++++++ systemd/chamado-health.timer | 10 +++++++++ 5 files changed, 116 insertions(+) create mode 100644 README.md create mode 100755 healthcheck.py create mode 100755 install_systemd.sh create mode 100644 systemd/chamado-health.service create mode 100644 systemd/chamado-health.timer diff --git a/README.md b/README.md new file mode 100644 index 0000000..c19a243 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Chamado Health + +Simple health check script for Healthchecks.io, designed to run every minute using a systemd timer. + +## Installation + +1. Copy the systemd unit files to `/etc/systemd/system/`: + + ```bash + sudo cp systemd/chamado-health.service /etc/systemd/system/ + sudo cp systemd/chamado-health.timer /etc/systemd/system/ + ``` + +2. Update the `HEALTHCHECK_URL` in the service unit or set it in `/etc/default/chamado-health`. + +3. Reload systemd and enable the timer: + + ```bash + sudo systemctl daemon-reload + sudo systemctl enable --now chamado-health.timer + sudo systemctl status chamado-health.timer + ``` + +## Configuration + +- `HEALTHCHECK_URL` must be set to your Healthchecks.io ping endpoint. +- You can also pass the URL as a command-line argument to the script: + + ```bash + ./healthcheck.py https://hc-ping.com/ + ``` + +## Script behavior + +- Sends an HTTP GET to the configured Healthchecks.io URL. +- Exits with code `0` when the request succeeds with `2xx`. +- Logs diagnostic errors to journal if the request fails. diff --git a/healthcheck.py b/healthcheck.py new file mode 100755 index 0000000..f13c748 --- /dev/null +++ b/healthcheck.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +import os +import sys +import urllib.request +import urllib.error +from datetime import datetime, timezone + + +def main(): + url = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("HEALTHCHECK_URL") + if not url: + print("ERROR: HEALTHCHECK_URL not set", file=sys.stderr) + return 1 + + timeout = int(os.environ.get("HEALTHCHECK_TIMEOUT", 10)) + req = urllib.request.Request(url, method="GET", headers={"User-Agent": "chamado-health/1.0"}) + + try: + with urllib.request.urlopen(req, timeout=timeout) as response: + status = response.getcode() + if 200 <= status < 300: + print(f"{datetime.now(timezone.utc).isoformat()} status={status} url={url}") + return 0 + body = response.read(200).decode("utf-8", errors="replace") + + print(f"ERROR: HTTP {status}\n{body}", file=sys.stderr) + return 2 + + except urllib.error.HTTPError as exc: + print(f"HTTPError {exc.code} {exc.reason} url={url}", file=sys.stderr) + return 3 + except urllib.error.URLError as exc: + print(f"URLError {exc.reason} url={url}", file=sys.stderr) + return 4 + except Exception as exc: + print(f"ERROR {exc} url={url}", file=sys.stderr) + return 5 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/install_systemd.sh b/install_systemd.sh new file mode 100755 index 0000000..628c79c --- /dev/null +++ b/install_systemd.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +mkdir -p "${HOME}/.config/systemd/user" +cp "${SCRIPT_DIR}/systemd/chamado-health.service" "${HOME}/.config/systemd/user/" +cp "${SCRIPT_DIR}/systemd/chamado-health.timer" "${HOME}/.config/systemd/user/" + +echo "Copied systemd unit files to ~/.config/systemd/user/." + +systemctl --user daemon-reload +systemctl --user enable --now chamado-health.timer +systemctl --user status --no-pager chamado-health.timer diff --git a/systemd/chamado-health.service b/systemd/chamado-health.service new file mode 100644 index 0000000..4649f7f --- /dev/null +++ b/systemd/chamado-health.service @@ -0,0 +1,14 @@ +[Unit] +Description=Chamado Health check script +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +EnvironmentFile=-%h/.config/chamado-health +ExecStart=/usr/bin/env python3 %h/src/chamado-health/healthcheck.py +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=default.target diff --git a/systemd/chamado-health.timer b/systemd/chamado-health.timer new file mode 100644 index 0000000..ca3e758 --- /dev/null +++ b/systemd/chamado-health.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Run Chamado Health check every minute + +[Timer] +OnBootSec=1min +OnUnitActiveSec=1min +Persistent=true + +[Install] +WantedBy=timers.target