Initial commit
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
98180a0f6b
5 changed files with 116 additions and 0 deletions
37
README.md
Normal file
37
README.md
Normal file
|
|
@ -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/<your-uuid>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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.
|
||||||
41
healthcheck.py
Executable file
41
healthcheck.py
Executable file
|
|
@ -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())
|
||||||
14
install_systemd.sh
Executable file
14
install_systemd.sh
Executable file
|
|
@ -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
|
||||||
14
systemd/chamado-health.service
Normal file
14
systemd/chamado-health.service
Normal file
|
|
@ -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
|
||||||
10
systemd/chamado-health.timer
Normal file
10
systemd/chamado-health.timer
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Run Chamado Health check every minute
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=1min
|
||||||
|
OnUnitActiveSec=1min
|
||||||
|
Persistent=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
Loading…
Reference in a new issue