Messing about with IT

Network monitoring using Synology NAS and Healthchecks.io

I’ve been using this script on a Synology NAS to monitor other hosts on the same network. In the event that a host stops responding to ping, a notification is sent by Healthchecks.io. This allows me to use the NAS as a basic availability monitor for other devices on the network.

About Healthchecks.io

Healthchecks.io is a cron job monitoring service. It listens for HTTP requests and email messages (“pings”) from your cron jobs and scheduled tasks (“checks”). When a ping doesn’t arrive on time, Healthchecks sends out alerts. You can create a free ‘Hobbyist’ account and receive alerts for up to 20 devices, then upgrade later if you need more.

Why not just send an email?

I’ve attempted to get Network monitoring using Synology NAS and a ping script to send email from a Synology NAS in the past without success so decided to use Heathchecks.io as I had already set that up for other purposes.

In addition, you get the benefit of receiving an email when your main router is not reachable (as the alert is sent from the Healthchecks.io website, rather than from a device on your own network).

Setting up Healthchecks.io

Sign up for an account, then add a new check for each network device you’d like to monitor.

You can configure each check with an expected period and grace time, for example ‘expect a ping every five minutes and allow 4 minutes grace time in case it’s late’. If the ping is not received by the check within the grace period, then send an email alert.

For example, in the image below are three checks for devices on a local network. Each check is configured to send an email on failure. Since this screenshot was taken, I’ve added an additional ‘self check’ url to monitor the running of this script from healthchecks.io too.

Network monitoring using Synology NAS: Healthchecks.io website showing 3 checks

Once you’ve added checks for each device you want to monitor, you should test each one by visiting the given url with your web browser. You should find the status turns green in Healthchecks.io after the browser visits the url.

You can also simulate a failure to confirm that an alert email is sent. To do so, append /fail to the end of the check url, for example:

https://hc-ping.com/{uuid}/fail

Confirm that you receive an email as per the check configuration.

Creating the script on the Synology NAS

Save the script below into a new text file and store it on a Synology NAS volume. E.g. /volume1/Documents/ScheduledTasks/PingNotify.sh

Set the permissions so that the root user has execute permission. In this screenshot, the script owner is in the administrators group:

Set permissions for the custom script

Test the script (use sudo /path/script.sh) using an ssh session. You can see the output on screen and this is also output to a log file.

username@SynologyNAS/volume1/Documents/ScheduledTasks$
sudo PingNotify.sh
Main router (192.168.0.1) is up
Retry count (5) exceeded
Wi-Fi access point (192.168.0.10) is down, sending notification
Network Switch (192.168.0.20) is up

Setting up the scheduled task on the Synology NAS

On the Synology NAS, go to Control Panel > Task Scheduler, click Create, and select Scheduled Task

See the screenshots here for guidance, or check the current Synology documentation on scheduled tasks.

  1. Enter a task name and set the user as root
Scheduled Task General Settings, set the user as root

2. Set the task to run Daily starting at 00:00 and repeat at the interval you chose when creating the Healthchecks.io checks. Set the last run time to be midnight, less check the interval

Scheduled Task recurrence settings

3. Enter the path on the NAS to the script you created earlier. No need to configure email notification here.

Network monitoring using Synology NAS: Scheduled Task settings, enter the file path for the script

How to configure the script

Note that this has only been tested with DSM 6.2. In this example, the script is configured to monitor 3 hosts. You will need to prepare the following details for your own hosts and edit the script as necessary on lines 17 to 32.

IP addressDescriptionSuccess URLFailure URL
192.168.0.1Main routerhttps://hc-ping.com/your-router-uuid-herehttps://hc-ping.com/your-router-uuid-here/fail
192.168.0.10Wi-Fi access pointhttps://hc-ping.com/your-wifi-uuid-herehttps://hc-ping.com/your-wifi-uuid-here
192.168.0.20Network Switchhttps://hc-ping.com/your-switch-uuid-herehttps://hc-ping.com/your-switch-uuid-here/fail

Script for network monitoring using Synology NAS

#!/bin/bash

# Add new elements to each of the arrays below, ensuring you keep the values
# in the same position in each array. For example, looking up 3 in the first
# array will return c, iii and # from the subseqent arrays:
#  (1 2 3 4)
#  (a b c d)
#  (i ii iii iv)
#  (£ $ # %)
#
# Also, ensure that each array contains the same number of elements
#
# While loop based on an example by Sirch from here:
# https://serverfault.com/questions/521885/try-the-command-multiple-times/521889

# add list of ips to monitor
ips=("192.168.0.1" "192.168.0.10" "192.168.0.20")

# add descriptive names for each IP address
hostnames=("Main router" "Wi-Fi access point" "Network Switch")

# add 'success' urls for each ip from healthchecks.io
upURLs=("https://hc-ping.com/your-router-uuid-here" "https://hc-ping.com/your-wifi-uuid-here" "https://hc-ping.com/your-switch-uuid-here")

# add 'failure' urls for each ip from healthchecks.io
downURLs=("https://hc-ping.com/your-router-uuid-here/fail" "https://hc-ping.com/your-wifi-uuid-here/fail" "https://hc-ping.com/your-switch-uuid-here/fail")

# add a 'self check' url to monitor this script from healthchecks.io
selfCheckURL=https://hc-ping.com/your-self-check-uuid-here

# define a log file
logfile=/volume1/Documents/ScheduledTasks/PingNotify.log
echo "$(date)" > $logfile

########################################################
# no need to change anything below this point

# check that arrays contain same number of elements
CheckIPS=${#ips[@]}
CheckHostnames=${#hostnames[@]}
CheckUpURLs=${#upURLs[@]}
CheckDownURLs=${#downURLs[@]}

if [ $CheckIPS -ne $CheckHostnames ] || [ $CheckIPS -ne $CheckUpURLs ] || [ $CheckIPS -ne $CheckDownURLs ]
then
  echo ERROR: arrays contain different number of elements, exiting 2>&1 |& tee -a $logfile
  exit 1
fi
# arrays are OK, begin checks

# send a self-check notification that this script is running
wget -T 10 -qO - $selfCheckURL > /dev/null

# ping function
PingIP () {
  local max=5 # number of ping attempts
  local delay=1 # seconds between attempts
  local count=0 # starting count
  while [ $count -lt $max ]; do
    ping -c 1 $1 > /dev/null
    if [ $? -eq 0 ]; then
      return 0
    fi
    sleep $delay # wait for $delay seconds before retrying
    let $((count+=1)) # increment count variable by one
  done
  echo "Retry count ($count) exceeded" 2>&1 |& tee -a $logfile
  return 1
}

# define counter as 'n', starting at zero
n=0

# loop through each IP address
for i in "${ips[@]}"; do
  PingIP "$i"
    if [ $? -ne 0 ]; then
      echo "${hostnames[$n]} ($i) is down, sending notification" 2>&1 |& tee -a $logfile
      # request a URL to notify the current status 
      wget -T 10 -qO - ${downURLs[$n]} > /dev/null
    else
      echo "${hostnames[$n]} ($i) is up" 2>&1 |& tee -a $logfile
      # request a URL to notify the current status
      wget -T 10 -qO - ${upURLs[$n]} > /dev/null
    fi
    let $((n+=1)) # increment n variable by one
done

1 Comment

  1. Ivan

    Hi. This looks great! Is there a way of monitoring as well latency within the network so if for example is below a specific threshold it alerts?
    Thanks!

© 2024 Tech Explorer

Theme by Anders NorénUp ↑