Go to file
2023-10-25 09:53:15 -05:00
go.mod Initial commit and stuff 2023-10-25 03:23:36 -05:00
LICENSE Initial commit and stuff 2023-10-25 03:23:36 -05:00
main.go Initial commit and stuff 2023-10-25 03:23:36 -05:00
README.md Add Bash and curl way 2023-10-25 09:53:15 -05:00

go-uptime-alert

This program checks to see if each URL in a given set of URLs responds with a 200 OK HTTP status, and if they do it send a ping to a healthchecks.io API. I run this as a cronjob or systemd timer every 15 minutes, and I have the grace time and period of the Healthchecks check set to 15 minutes.

Installation

Requires Go >= 1.20

go install git.sr.ht/~hyperreal/go-uptime-alert@latest

Usage

It requires a JSON configuration file located at ~/.config/go-uptime-alert.json. The configuration file is of the following format:

{
    "service_urls_uuids": [
        { "site_url": "https://hyperreal.coffee", "hc_ping_url": "<Healthchecks.io API ping URL>" },
        { "site_url": "https://fedi.hyperreal.coffee", "hc_ping_url": "<Healthchecks.io API ping URL>" }
    ]
}

systemd

~/.config/systemd/user/go-uptime-alert.service

[Unit]
Description=go-uptime-alert service

[Service]
Type=oneshot
ExecStart=/home/user/go/bin/go-uptime-alert

~/.config/systemd/user/go-uptime-alert.timer

[Unit]
Description=go-uptime-alert timer

[Timer]
OnBootSec=15min
OnUnitActiveSec=15min

[Install]
WantedBy=timers.target

Enable linger for the user and enable the timer:

sudo loginctl enable-linger user
systemctl --user enable --now go-uptime-alert.timer

cron/periodic

Place the following at /etc/periodic/15min/go-uptime-alert:

#!/bin/sh

LOGFILE="/var/log/go-uptime-alert.log"

sudo -u user /home/user/go/bin/go-uptime-alert >> "${LOGFILE}" 2>&1

TODO

  • Send output to a static HTML file and format appropriately for web server.
  • Send output to a .md file for use in Hugo or other static site generators.

Or just use Bash and curl

#!/usr/bin/env bash

# associative array to store mappings between site URLS and ping URLs.
declare -A sites_hc_urls

sites_hc_urls["https://hyperreal.coffee"]="https://healthchecks.io/ping/blahblah"
sites_hc_urls["https://anonoverflow.hyperreal.coffee""]="https://healthchecks.io/ping/blahblahblah"
...

for key in "${!sites_hc_urls[@]}"; do
    if curl -sSL -q "$key" >/dev/null 2>&1; then
        if curl -m 10 --retry 5 "${sites_hc_urls[$key]}" >/dev/null 2>&1; then
            echo "$key: OK"
        else
            echo "Error sending ping to ${sites_hc_urls[$key]}"
        fi
    else
        echo "$key: DOWN"
    fi
done