Disclosure: I run Buran.host, a small VPS provider. This article is not about our service, the script works on any Linux server.
A single small server does not need Prometheus, Grafana and an alert manager. It needs to tell you when one of four things goes wrong, before your users do:
- The disk is almost full
- Memory is almost gone
- The CPU has been overloaded for a while
- Your own service stopped answering
Disk is the classic one. It fills up quietly with logs, backups and database files, and then the database refuses to write. Memory comes next: when MemAvailable hits zero, the kernel starts killing processes.
The idea
A shell script runs from cron every five minutes. For each check it keeps a small marker file. When a check fails and there is no marker, it sends one message and creates the marker. When the check passes and the marker exists, it sends a recovery message and deletes it. That is how you avoid getting the same alert every five minutes.
For notifications I use ntfy.sh: you pick a long random topic name, subscribe to it in the phone app, and curl posts to it. No account is needed. If you prefer Telegram, replace the curl line with a call to the Bot API.
The script
Save it as /usr/local/bin/healthcheck.sh:
#!/bin/sh
# Minimal health check for a small Linux VPS. Run from cron every 5 minutes.
# Sends one message when a check fails and one when it recovers.
TOPIC="change-this-to-a-long-random-string" # your private ntfy.sh topic
DISK_MAX=85 # percent of / in use
MEM_MAX=90 # percent of RAM in use (MemAvailable based)
SERVICE_URL="http://127.0.0.1:8080/health" # what your app answers locally
HOST=$(hostname)
STATE=/var/tmp/healthcheck
mkdir -p "$STATE"
notify() {
curl -fsS -m 10 -d "$HOST: $1" "https://ntfy.sh/$TOPIC" >/dev/null 2>&1
}
# check NAME BAD(0|1) MESSAGE
check() {
if [ "$2" = 1 ]; then
[ -e "$STATE/$1" ] || { notify "ALERT $3"; : > "$STATE/$1"; }
else
[ -e "$STATE/$1" ] && { notify "OK $1 is back to normal"; rm -f "$STATE/$1"; }
fi
}
disk=$(df -P / | awk 'NR==2 {gsub("%","",$5); print $5}')
check disk "$([ "$disk" -ge "$DISK_MAX" ] && echo 1 || echo 0)" "disk is ${disk}% full"
mem=$(awk '/MemTotal/ {t=$2} /MemAvailable/ {a=$2} END {printf "%d", (t-a)*100/t}' /proc/meminfo)
check mem "$([ "$mem" -ge "$MEM_MAX" ] && echo 1 || echo 0)" "memory is ${mem}% used"
load=$(awk '{print $2}' /proc/loadavg)
cores=$(nproc)
check load "$(awk -v l="$load" -v c="$cores" 'BEGIN {print (l > c * 1.5) ? 1 : 0}')" "5 min load ${load} on ${cores} cores"
curl -fsS -m 5 -o /dev/null "$SERVICE_URL"
check service "$([ $? -ne 0 ] && echo 1 || echo 0)" "service does not answer at $SERVICE_URL"
Make it executable and test it by temporarily setting DISK_MAX=1:
chmod +x /usr/local/bin/healthcheck.sh
/usr/local/bin/healthcheck.sh
You should get one message on your phone. Run it again and you should get nothing, because the alert is already sent. Put the threshold back and run it a third time: you get the recovery message.
Cron
*/5 * * * * /usr/local/bin/healthcheck.sh
What it does not cover
A monitor that lives on the server cannot tell you the server is down: if the machine is off, the script is off too. Add one outside check, for example a free external uptime service that requests your site every few minutes and emails you. Together, the two cover most small projects.
It also says nothing about why something went wrong. When the disk alert fires, du -xh / | sort -h | tail is your next command. Dashboards are worth adding once you miss them, not before.
Notes
-
MemAvailableis the right number for memory. Plain "used" counts disk cache, which Linux gives back when needed, and makes healthy servers look full. - Load is compared with the number of cores. A load of 3 means very different things on 1 core and on 8.
- Keep the topic name private. Anyone who knows it can read your alerts.
What is the first alert you set up on a new server?
Top comments (0)