A couple of weeks ago I stumbled upton textbee on reddit which brought back something I always wanted to do:
- have an Text/SMS Gateway with a throw-away phone number for signups an 2FA for trash accounts
- Text/SMS Alerts in case my home network is down or offline
Textbee
is quite new and there are other projects out there like httpsms or sms-gate.
Text/SMS Gateway
I went with httpsms
as textbee
doesn’t support outgoing webhooks - yet.
Install the app, configure a webhook target (I am using n8n) which then sends me a message (telegram, signal, whatsapp, whatever) on my phone with the content of the text message.
Easy and straight forward.
Monitoring your homeserver and internet connection
Prometheus + Alertmanager
seemed like overkill to monitor my little homeserver(it’s thinclient with 32GB Memory and 2TB SSD), so I went with Beszel and uptime-kuma.
Also running monitoring on the node itself has it’s constraints (you can’t detect an outage of your own server), but it has to be done. Well knowing the limitations.
So I reviewed my Scope for Monitoring:
- Monitoring basic health metrics of homeserver (done with Beszel)
- Monitoring the containers and some web endpoints running on my homeserver (done with uptime-kuma)
- Monitor if my router is reachable (e.g. detect power or network outage)
- Monitor if my internet-connection is working (e.g. power outage, network outage or ISP Outage)
- Monitor recurring jobs (cron) like Backups (done with healthchecks.io)
Why detecting a network outage can make sense
While a non-running backup can be an indicator, that your network is done, it’s not proof but only an indicator. I wanted to properly monitor my network for a larger outage. When we are traveling and the power is gone for hours, I maybe want to call a neighbor to check on things. This might be even more relevant for other parts of the world where power outages are more common.
So the device to monitor power-outages should be able to keep running (battery!) and needs to be able to reach me on a different communication channel.
Setting up Android
I picked up my old Xiaomi Mi A1, flashed a new ROM on it, without any Bloatware and installed the f-droid App-Store.
I did not root my phone, as it’s not necessary.
After installing f-droid
I installed termux, termux-api and termux-boot.
For easier management I set up sshd
follwing the remote access docs of termux.
Make sure to start sshd
on every boot like described at https://wiki.termux.com/wiki/Termux:Boot.
I installed nginx
, cronie
,vim
and python
packages.
I added gave my phone a static IP via DHCP reservation and added the default HTTP endpoint from
nginx
touptime-kuma
to monitor my monitor ;)
Sending text messages
I wrote a simple bash script like this:
#!/data/data/com.termux/files/usr/bin/bash
##########################################
# Configuration
##########################################
# IP to ping (Google DNS)
PING_IP="8.8.8.8"
# Number to send SMS to
SMS_TARGET="+491781234567"
# Message to send if ping fails
SMS_MESSAGE="ALERT: Internet connection failed (cannot reach $PING_IP)"
# Healthchecks.io fail endpoint
HEALTHCHECKS_URL="https://hc-ping.com/SLUG-GOES-HERE"
# Timeout for ping in seconds
PING_TIMEOUT=2
##########################################
# Logic
##########################################
curl -fsS --max-time 5 "$HEALTHCHECKS_URL" > /dev/null
# Check if we can reach the IP
if ! ping -c 1 -W "$PING_TIMEOUT" "$PING_IP" > /dev/null 2>&1; then
echo "$(date): Ping to $PING_IP failed"
# Send SMS using Termux
termux-sms-send -n "$SMS_TARGET" "$SMS_MESSAGE"
# Notify healthchecks.io
curl -fsS --max-time 5 "$HEALTHCHECKS_URL/fail" > /dev/null
else
echo "$(date): Ping to $PING_IP successful"
fi
The script uses termux-sms-send
to send the text. Double check the app permissions if sending texts does not work.
Add the script to contrab -e
like */5 * * * * /data/data/com.termux/files/home/ping_google.sh >> /data/data/com.termux/files/home/google_check.log 2>&1
and you are done.
Summary
Termux is a mighty tool for android and with the coming Linux Shell for Android 16, this will give everyone completely new opportunities to use their phones! You are basically carrying your server around.