Monitoring /home Usage with a Systemd Timer
A home server rarely needs a full‑blown monitoring stack.
If you just want a quick alert when /home fills up, a tiny systemd service plus curl will do the trick. The example below keeps the footprint minimal, respects security best practices, and uses the official Telegram Bot API.
1. Create a Bot and Grab Credentials
- Talk to BotFather on Telegram and create a bot.
- Note the bot token (e.g.
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11). - Find your chat ID by sending a message to the bot and querying
https://api.telegram.org/bot<token>/getUpdates. - Store both values in a read‑only file:
sudo mkdir -p /etc/telegram-alert
sudo tee /etc/telegram-alert/credentials <<EOF
TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
CHAT_ID=987654321
EOF
sudo chmod 600 /etc/telegram-alert/credentials
2. Service Unit – home-usage-alert.service
# /etc/systemd/system/home-usage-alert.service
[Unit]
Description=Send Telegram alert when /home exceeds 90%% usage
After=network.target
[Service]
Type=oneshot
EnvironmentFile=/etc/telegram-alert/credentials
ExecStart=/usr/bin/bash -c '
USAGE=$(df --output=pcent /home | tail -1 | tr -d "%")
if [ "$USAGE" -ge 90 ]; then
curl -s -X POST https://api.telegram.org/bot$TOKEN/sendMessage \
-d chat_id=$CHAT_ID \
-d text="⚠️ /home is at ${USAGE}%% capacity. Free space needed." \
-H "Content-Type: application/x-www-form-urlencoded"
fi
'
PrivateTmp=yes
ProtectSystem=full
RestrictAddressFamilies=AF_INET AF_INET6
Restart=no
[Install]
WantedBy=multi-user.target
Security notes
[Read More]