Tuesday, September 6, 2016

Monitoring Raspberry Pi using my DigitalOcean VPS

So, this is a simple one and just felt like sharing in case someone wants to save time writing those one-liners.

I had a faulty raspberry pi power supply and my Rpi used to hang unexpectedly. Here is what I did to monitor uptime of my Rpi.


  • Host a simple text file via nginx on my DigitalOcean VPS.
➜ $?=0 /home/shadyabhi [ 1:20PM] % cat /var/www/html/rpihealth
GOOD

>>>  0s elasped...
➜ $?=0 /home/shadyabhi [ 1:20PM] %
  • Write a simple systemd service in Rpi to curl this file. 
➜ $?=0 /home/shadyabhi [ 5:21PM] % sudo cat /usr/lib/systemd/system/send_latest_ip.service
[Unit]
Description=Latest IP sender

[Service]
User=shadyabhi
ExecStart=/usr/local/bin/send_uptime.sh

[Install]
WantedBy=multi-user.target

>>>  0s elasped...
➜ $?=0 /home/shadyabhi [ 5:21PM] % cat /usr/local/bin/send_uptime.sh
#!/bin/bash

while [ 1 ]; do
        curl https://abhijeetr.com/rpihealth
        sleep 1
done

>>>  0s elasped...
➜ $?=0 /home/shadyabhi [ 5:21PM] % sudo systemctl enable send_uptime
  • A sample logline for a health checks looks like this.
106.51.128.57 - - [06/Sep/2016:13:37:46 -0400] "GET /rpihealth HTTP/1.1" 200 5 "-" "curl/7.50.0" "-"
  • A cronjob on my VPS to send me notifications via pushover if there are no log lines for this health check in the last 10 seconds. (using https://github.com/jnwatts/pushover.sh)
* * * * * sudo /usr/bin/perl -MDate::Parse -ne 'print if/^.*- - \[(.*?)\] .*?/&&str2time($1)>time-10' /var/log/nginx/access.log | if ! /bin/grep -q "/rpihealth"; then /home/shadyabhi/pushover.sh/pushover.sh -t "RPi Health" "Rpi is down"; fi

Now, whenever my Rpi is down, I get push notifications on my phone.





No comments:

Post a Comment