Monitoring a small Linux server: what to watch, what to alert on, and what to ignore
In this article8 sections
Most small servers are not monitored at all. The owner finds out about a problem when a customer rings, or when they happen to open the site. The opposite is also common: a large monitoring system with two hundred graphs and fifty alerts that nobody reads, until the one that matters is lost in the noise.
The right amount is small. A handful of checks, each answering a plain question, each wired to a notification that reaches a person. This guide lists the checks that catch the large majority of real outages on a small Linux server, gives you a script you can install in ten minutes, and explains how to keep the alerts meaningful.
The principle: alert on symptoms and on silence
Two ideas do most of the work.
Watch from outside. The most important check is not on the server at all: it is a request from the internet to your site, made by a service that is not your server. If the machine is down, a check running on it cannot tell you. Use an external uptime monitor for every public address you care about.
Alert on silence, not only on failure. A backup job that stops running does not fail: it simply never reports. A cron daemon that dies does not log an error. The pattern that catches this is the dead man’s switch: your job pings a monitoring address every time it succeeds, and the monitoring service alerts you when the pings stop. No news is bad news.
The six checks that matter
1. Is the site up, and is it the right page?
An external monitor requesting your home page every one to five minutes. Better: check for a keyword that proves the application is working (a phrase from the footer, or a /health address that touches the database), not only that the web server answers with 200. A site can return a pleasant error page with a success code.
Also monitor your login and checkout pages if the business depends on them.
2. Is there disk space and are there inodes?
A full disk is the most common cause of a small server falling over: the database refuses writes, logs fill up, mail stops, backups fail. Watch both space and inodes:
df -h --output=source,size,pcent,target -x tmpfs -x devtmpfs
df -i --output=source,ipcent,target -x tmpfs -x devtmpfs
Alert at 80% (time to act this week) and 90% (act today). Find what is growing:
sudo du -xh --max-depth=2 /var | sort -h | tail -15
sudo journalctl --disk-usage
3. Is memory under pressure?
A small server that runs out of memory kills processes, usually the database. Watch available memory and swap use:
free -m
dmesg -T 2>/dev/null | grep -i 'out of memory' | tail -3
sudo journalctl -k --since "7 days ago" | grep -i 'killed process' | tail -3
If the kernel has killed a process even once, you want to know before the next time. Constant swapping (vmstat 1 5 showing non-zero si and so) means the machine needs less running on it or more memory.
4. Will the certificate expire?
An expired certificate is a self-inflicted outage, and it is entirely predictable. Check how long you have left:
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate
sudo certbot certificates
sudo certbot renew --dry-run
Alert when fewer than 14 days remain: auto-renewal normally happens around 30 days before expiry, so a certificate at 14 days means renewal has been failing for two weeks. Many external monitors check this for you.
5. Did the backup run, and did it work?
This is the dead man’s switch case. Your backup job finishes by pinging a monitoring address, but only when it succeeded. If the job fails, hangs or never starts, the ping does not arrive and you are told. Also test a restore now and then: a backup that arrives is not proof that it can be used.
6. Are any services failed?
systemctl --failed
systemctl list-units --state=failed --no-legend
Nothing should be in this list. A failed unit (a queue worker, a mail service, a backup timer) is often the first sign of a slow problem.
A check script with a dead man’s switch
The idea: one script, run every five minutes. It checks the basics. Only if everything is healthy does it ping the monitoring address. If anything is wrong, or the script itself stops running, the pings stop and you are alerted. One script, one alert path.
Create the script:
sudo nano /usr/local/sbin/health-check
#!/usr/bin/env bash
set -u
PING_URL="https://hc-ping.example/your-unique-id" # your monitor's ping address
DISK_LIMIT=85 # percent
INODE_LIMIT=85 # percent
MEM_MIN_MB=150 # available memory, MB
CERT_DAYS=14
SITE="example.com"
problems=()
# disk and inodes on the main filesystems
while read -r use mount; do
[ "${use%\%}" -ge "$DISK_LIMIT" ] && problems+=("disk $mount at $use")
done < <(df --output=pcent,target -x tmpfs -x devtmpfs | tail -n +2 | tr -s ' ')
while read -r use mount; do
[ "$use" = "-" ] && continue
[ "${use%\%}" -ge "$INODE_LIMIT" ] && problems+=("inodes $mount at $use")
done < <(df --output=ipcent,target -x tmpfs -x devtmpfs | tail -n +2 | tr -s ' ')
# memory
avail=$(free -m | awk '/^Mem:/ {print $7}')
[ "$avail" -lt "$MEM_MIN_MB" ] && problems+=("low memory: ${avail} MB available")
# failed services
failed=$(systemctl --failed --no-legend | wc -l)
[ "$failed" -gt 0 ] && problems+=("$failed failed systemd unit(s)")
# certificate
end=$(echo | openssl s_client -connect "$SITE:443" -servername "$SITE" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
if [ -n "$end" ]; then
left=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
[ "$left" -lt "$CERT_DAYS" ] && problems+=("certificate expires in $left days")
else
problems+=("could not read the certificate")
fi
# site answers
code=$(curl -s -o /dev/null -m 15 -w '%{http_code}' "https://$SITE/")
[ "$code" = "200" ] || problems+=("site answered $code")
if [ "${#problems[@]}" -eq 0 ]; then
curl -fsS -m 10 --retry 2 -o /dev/null "$PING_URL"
else
logger -t health-check "PROBLEMS: ${problems[*]}"
printf 'health-check: %s\n' "${problems[@]}" >&2
fi
Install and test it:
sudo chmod 750 /usr/local/sbin/health-check
sudo /usr/local/sbin/health-check; echo "exit: $?"
To prove the alert path works, temporarily set DISK_LIMIT=1, run it, and watch for the alert from your monitoring service when the pings stop. Then set it back.
Run it every five minutes:
echo '*/5 * * * * root /usr/local/sbin/health-check' | sudo tee /etc/cron.d/health-check
The same trick works for every scheduled job: end the backup script with a ping to its own address, and set that address to expect one ping a day. An alert then means “the backup has not reported for more than a day” and needs no further explanation.
Where the alerts go
An alert is only useful if it reaches someone who will act. Decide that on purpose:
- Two channels. Email alone gets buried. Add a phone notification, a chat message or SMS for the alerts that mean “the site is down”.
- A name, not a team. Each alert has one person who is responsible, even if that person is you.
- Quiet hours. If nobody can act at 3 a.m., do not wake anyone for a disk at 82%. Page for outages; collect everything else in a morning summary.
Avoiding alert fatigue
The failure mode of monitoring is not too few alerts but too many that mean nothing. Rules that keep it honest:
- Every alert needs an action. If you would read it and do nothing, delete the alert.
- Alert on conditions that last. A CPU spike for a minute is noise; a failed check three times in a row is a problem.
- Have a threshold for “warn” and one for “act”. Disk at 80% is a ticket; at 90% it is a page.
- Review the alerts monthly. Anything that fired and was ignored is either a bad alert or a real problem you have been avoiding.
What to ignore
CPU graphs without a question attached, a hundred metrics from a tool you installed once, and uptime counters (“up 400 days” is a warning about missing kernel updates, not an achievement). Collect metrics if you will use them for capacity planning, but do not alert on them.
A starting checklist
- An external uptime monitor on the home page (with a keyword) and on one business-critical page.
- The health-check script above, pinging a dead man’s switch.
- A ping at the end of every backup and scheduled job.
- Two alert channels, and one named owner.
- A monthly look at what fired.
When it is worth getting help
The checks are simple. What takes experience is choosing thresholds that match your server, wiring every scheduled job to a dead man’s switch, and making sure the alert reaches a person who knows what to do with it. That is the work of a light maintenance retainer: patching, backup checks and monitoring, with a short report each month.
I run this kind of upkeep for small and medium sites. You can see my profile on Upwork or read about monitoring and upkeep. If backups are your biggest worry, start with how we test restores.