diff --git a/healthcheck.py b/healthcheck.py index 3d2f9e4..ccb058c 100644 --- a/healthcheck.py +++ b/healthcheck.py @@ -1,71 +1,78 @@ #!/usr/bin/env python3 -import json import os import sys -import urllib.error import urllib.request +import urllib.error from datetime import datetime, timezone from pathlib import Path -PAYLOAD = json.dumps({"name": "Ronaldo Freitas Dias"}).encode() +USER_NAME = "Ronaldo Freitas Dias" -def load_url_from_config(): - config_file = Path.home() / ".config" / "health-check" +def load_config(): + config_file = Path.home() / ".config" / "health-monitor" + config = {} if config_file.exists(): for line in config_file.read_text().splitlines(): - if line.startswith("HEALTHCHECK_URL="): - return line.split("=", 1)[1].strip() - return None + if "=" in line: + key, value = line.split("=", 1) + config[key.strip()] = value.strip() + return config -HEADERS = {"User-Agent": "health-check/1.0", "Content-Type": "application/json"} - - -def ping(url: str, timeout: int) -> int: - req = urllib.request.Request(url, data=PAYLOAD, method="GET", headers=HEADERS) +def ping(url: str, body: bytes, timeout: int) -> int: + req = urllib.request.Request( + url, data=body, method="POST", + headers={"User-Agent": "health-monitor/1.0", "Content-Type": "text/plain"} + ) with urllib.request.urlopen(req, timeout=timeout) as response: return response.getcode() -def ping_fail(url: str, timeout: int) -> None: +def ping_fail(url: str, body: bytes, timeout: int) -> None: fail_url = url.rstrip("/") + "/fail" try: - req = urllib.request.Request(fail_url, data=PAYLOAD, method="GET", headers=HEADERS) + req = urllib.request.Request( + fail_url, data=body, method="POST", + headers={"User-Agent": "health-monitor/1.0", "Content-Type": "text/plain"} + ) urllib.request.urlopen(req, timeout=timeout) except Exception: pass def main(): - url = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("HEALTHCHECK_URL") or load_url_from_config() + config = load_config() + url = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("HEALTHCHECK_URL") or config.get("HEALTHCHECK_URL") if not url: print("ERROR: HEALTHCHECK_URL not set", file=sys.stderr) return 1 + device = os.environ.get("DEVICE_NAME") or config.get("DEVICE_NAME", "unknown") timeout = int(os.environ.get("HEALTHCHECK_TIMEOUT", 10)) + body = f"user={USER_NAME}\ndevice={device}".encode() try: - status = ping(url, timeout) + status = ping(url, body, timeout) if 200 <= status < 300: - print(f"{datetime.now(timezone.utc).isoformat()} status={status} url={url}") + print(f"{datetime.now(timezone.utc).isoformat()} status={status} device={device} url={url}") return 0 print(f"ERROR: HTTP {status} url={url}", file=sys.stderr) - ping_fail(url, timeout) + ping_fail(url, body, timeout) return 2 except urllib.error.HTTPError as exc: print(f"HTTPError {exc.code} {exc.reason} url={url}", file=sys.stderr) - ping_fail(url, timeout) + ping_fail(url, body, timeout) return 3 except urllib.error.URLError as exc: print(f"URLError {exc.reason} url={url}", file=sys.stderr) - ping_fail(url, timeout) + ping_fail(url, body, timeout) return 4 except Exception as exc: print(f"ERROR {exc} url={url}", file=sys.stderr) - ping_fail(url, timeout) + ping_fail(url, body, timeout) return 5 diff --git a/setup.fish b/setup.fish index e073b5a..b671294 100644 --- a/setup.fish +++ b/setup.fish @@ -36,25 +36,30 @@ except Exception as e: end end -# 4. Clonar ou atualizar repositório +# 4. Capturar nome do device +if not set -q DEVICE_NAME + read -P "Nome do device: " DEVICE_NAME +end + +# 5. Clonar ou atualizar repositório if test -d "$INSTALL_DIR/.git" git -C $INSTALL_DIR pull --ff-only else git clone $REPO_URL $INSTALL_DIR end -# 5. Salvar configuração (confirmando antes de sobrescrever) +# 6. Salvar configuração (confirmando antes de sobrescrever) if test -f $CONFIG_FILE echo "Configuração existente encontrada em $CONFIG_FILE." read -P "Deseja substituir? [s/N] " confirm if string match -qi 's' $confirm - echo "HEALTHCHECK_URL=$HEALTHCHECK_URL" > $CONFIG_FILE + printf "HEALTHCHECK_URL=%s\nDEVICE_NAME=%s\n" $HEALTHCHECK_URL $DEVICE_NAME > $CONFIG_FILE echo "Configuração salva em $CONFIG_FILE." else echo "Configuração mantida. Prosseguindo com valor existente." end else - echo "HEALTHCHECK_URL=$HEALTHCHECK_URL" > $CONFIG_FILE + printf "HEALTHCHECK_URL=%s\nDEVICE_NAME=%s\n" $HEALTHCHECK_URL $DEVICE_NAME > $CONFIG_FILE echo "Configuração salva em $CONFIG_FILE." end diff --git a/setup.sh b/setup.sh index f7632cc..e493f43 100644 --- a/setup.sh +++ b/setup.sh @@ -31,25 +31,30 @@ except Exception as e: [[ "$confirm" =~ ^[sS]$ ]] || { echo "Instalação cancelada."; exit 1; } fi -# 4. Clonar ou atualizar repositório +# 4. Capturar nome do device +if [[ -z "${DEVICE_NAME:-}" ]]; then + read -rp "Nome do device: " DEVICE_NAME "${CONFIG_FILE}" + printf "HEALTHCHECK_URL=%s\nDEVICE_NAME=%s\n" "${HEALTHCHECK_URL}" "${DEVICE_NAME}" > "${CONFIG_FILE}" echo "Configuração salva em ${CONFIG_FILE}." fi else - echo "HEALTHCHECK_URL=${HEALTHCHECK_URL}" > "${CONFIG_FILE}" + printf "HEALTHCHECK_URL=%s\nDEVICE_NAME=%s\n" "${HEALTHCHECK_URL}" "${DEVICE_NAME}" > "${CONFIG_FILE}" echo "Configuração salva em ${CONFIG_FILE}." fi