go-uptime-alert/README.md

100 lines
2.3 KiB
Markdown
Raw Normal View History

2023-10-25 10:23:36 +02: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
``` shell
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:
``` json
{
"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`
``` ini
[Unit]
Description=go-uptime-alert service
[Service]
Type=oneshot
ExecStart=/home/user/go/bin/go-uptime-alert
```
`~/.config/systemd/user/go-uptime-alert.timer`
``` ini
[Unit]
Description=go-uptime-alert timer
[Timer]
OnBootSec=15min
OnUnitActiveSec=15min
[Install]
WantedBy=timers.target
```
Enable linger for the user and enable the timer:
``` shell
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`:
``` shell
#!/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.
2023-10-25 16:53:15 +02:00
## Or just use Bash and curl
``` shell
#!/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
```