Backwards

Site status monitor

Mini-guide - 03/01/2017

Example script notifying the user when a site (or a server) goes offline.


BASH script:


#!/bin/bash
token="TOKEN"
testonoti="google.it is offile :("

ip="google.it"
ping -c 3 $ip > /dev/null 2>&1
if [ $? -ne 0 ]
then
   curl --request GET "https://api.rpinotify.it/notification/$token/text/$(echo ${testonoti// /%20})"
fi
						

It works in a very simple way. The device pings the remote server. If it does not answer a notification is sent.

Script variables:

  • $tokenPersonal token assigned during the registration process.
  • $testonotiNotification text.
  • $ip IP address or domain to monitor.

The call structure HTTP GET is really simple.

https://api.rpinotify.it/notification/$token/text/$testonoti

Variable name Description Format
$token Personal token. Alphanumeric string.
$testonoti Notification text. Text without spaces (use %20)

Lanch the script every two minutes

Every two minutes the script checks the target status and if it does not answer a notification is sent.

You can use cron, a very popular tool to plan operations on linux systems.
Change contab with crontab -e. The syntax of the line to add is the following:


* * * * * /command/to/be/executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. In this specific example, an asterisk in all fields means run job every minute.
We can edit our crontab with crontab -e and add the line (after last line) about this script.

*/2 * * * * sitestatus.sh

Where /home/gab/sitestatus.sh is the position of the script in the device.

For the execution of the script authorizations are necessary. Authorizations can be given by enteringchmod 755 sitestatus.sh

RpiNotify is an Italian project supported by RaspberryItaly

The first Italian RaspberryPi community.