Intro

Here is a Bourne shell Nagios plugin which can be executed directly on the NAS Synology. It's inspired by the great work of Jur Groen and Nicolas Ordonez.

The plugin will check the following elements :

Configuration

Instructions

Code

#! /bin/sh
# check_snmp_synology for nagios version 1.3
# 05.12.2014  shebangthedolphins.net add disk usage check
# 06.11.2014  shebangthedolphins.net
# 03.10.2014  Jur Groen, Syso IT Services, The Netherlands
# 30.04.2013  Nicolas Ordonez, Switzerland
#---------------------------------------------------
# this plugin checks the health of your Synology NAS
# - System status (Power, Fans)
# - Disks status
# - RAID status
# - DSM update status
#
# this plugin now makes use of SNMP V3 for enhanced security
# Tested with DSM 5.0
#---------------------------------------------------
# Based on http://download.synology.com/download/ds/userguide/Synology_DiskStation_MIB_Guide_enu_20110725.pdf
#---------------------------------------------------

#Variables definition
## NAGIOS returns
CRITICAL=2
WARNING=1
OK=0
RETURN=0                #everything's fine, by default

## If arguments found
option_found=0

#get the number of partitions
NB_part=$(df -h | grep "/" | grep -v "\(tmpfs\|dev \|rootfs \)" | wc -l)

##OIDs
OID_syno="1.3.6.1.4.1.6574"
OID_model="1.3.6.1.4.1.6574.1.5.1.0"
OID_serialNumber="1.3.6.1.4.1.6574.1.5.2.0"
OID_DSMVersion="1.3.6.1.4.1.6574.1.5.3.0"
OID_upgradeAvailable="1.3.6.1.4.1.6574.1.5.4.0"
OID_systemStatus="1.3.6.1.4.1.6574.1.1.0"
OID_powerStatus="1.3.6.1.4.1.6574.1.3.0"
OID_systemFanStatus="1.3.6.1.4.1.6574.1.4.1.0"
OID_CPUFanStatus="1.3.6.1.4.1.6574.1.4.2.0"
OID_temp="1.3.6.1.4.1.6574.1.2.0"

OID_disk=""
OID_diskID="1.3.6.1.4.1.6574.2.1.1.2"
OID_diskModel="1.3.6.1.4.1.6574.2.1.1.3"
OID_diskStatus="1.3.6.1.4.1.6574.2.1.1.5"
OID_diskTemp="1.3.6.1.4.1.6574.2.1.1.6"

OID_RAID=""
OID_RAIDName="1.3.6.1.4.1.6574.3.1.1.2"
OID_RAIDStatus="1.3.6.1.4.1.6574.3.1.1.3"

#Functions
FunctionDiskSpace()
{
        SORTIE=""
        for i in $(seq 1 "$NB_part"); do
                PARTITION=$(df -h | grep "/" | tr -s " " | grep -v "\(tmpfs\|dev \|rootfs \)" | head -n "$i" | tail -n 1 | grep "/" | cut -d " " -f 6)
                UTI=$(df -h | grep "/" | tr -s " " | grep -v "\(tmpfs\|dev \|rootfs \)" | head -n "$i" | tail -n 1 | grep "/" | cut -d " " -f 5)
                DISPO=$(df -h | grep "/" | tr -s " " | grep -v "\(tmpfs\|dev \|rootfs \)" | head -n "$i" | tail -n 1 | grep "/" | cut -d " " -f 4)
                SORTIE="$(echo ""$PARTITION" is used at "$UTI", "$DISPO" remaining free space") \n"$SORTIE""
                TEST=$(echo "$UTI" | sed 's/%//')
                if [ $TEST -gt "$WARNING_SPACE" ]; then
                        RETURN="$WARNING"
                        if [ "$TEST" -gt "$CRITICAL_SPACE" ]; then
                                RETURN="$CRITICAL"
                        fi
                fi
        done
        if [ "$1" == "Output" ]; then
                echo -e "$SORTIE"
        fi
}

usage()
{
        echo "usage: ./check_snmp_synology -u [snmp username] -p [snmp password] -h [hostname] -w [warning occuped space in % -c [max occuped space in %]"
        echo ""
        exit 3
}

while getopts u:p:h:c:w: OPTNAME; do
        case "$OPTNAME" in
        u)
                SNMPUSERNAME="$OPTARG"
                option_found=1
                ;;
        p)
                SNMPPASSWORD="$OPTARG"
                option_found=1
                ;;

        h)
                HOSTNAME="$OPTARG"
                option_found=1
                ;;
        w)
                WARNING_SPACE="$OPTARG"
                option_found=1
                ;;
        c)
                CRITICAL_SPACE="$OPTARG"
                option_found=1
                ;;
        *)
                usage
                ;;
        esac
done

# Script beginning :
if [ "$option_found" -eq "0" ] || [ "$HOSTNAME" == "" ] ; then
        usage
else
        # DiskSpace function usage but without print anything
        FunctionDiskSpace NoOutput

        nbDISK=$(/usr/syno/sbin/snmpwalk -OQne -v 3 -t 10 -l authNoPriv -u $SNMPUSERNAME -a MD5 -A $SNMPPASSWORD $HOSTNAME .1.3.6.1.4.1.6574.2.1.1.3 2>/dev/null|wc -l)
        #if SNMP failed, exit with error
        if [ "$?" != "0" ] ; then
                echo "CRITICAL - Problem with SNMP request"
                exit "$CRITICAL"
        fi

        nbRAID=$(/usr/syno/sbin/snmpwalk -OQne -v 3 -t 10 -l authNoPriv -u $SNMPUSERNAME -a MD5 -A $SNMPPASSWORD $HOSTNAME .1.3.6.1.4.1.6574.3.1.1.2 2>/dev/null|wc -l)

        for i in $(seq 1 $nbDISK);
        do
                OID_DISK="$OID_disk $OID_diskID.$(($i-1)) $OID_diskModel.$(($i-1)) $OID_diskStatus.$(($i-1)) $OID_diskTemp.$(($i-1)) "
        done
        for i in $(seq 1 $nbRAID);
        do
                OID_RAID="$OID_RAID $OID_RAIDName.$(($i-1)) $OID_RAIDStatus.$(($i-1))"
        done

        #MODEL
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_model" 2>/dev/null)
        model=$(echo $temp | grep $OID_model | cut -d " " -f3 | sed "s/\"//g")
        #echo $model

        #serialNumber
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_serialNumber" 2>/dev/null)
        serialNumber=$(echo $temp | grep $OID_serialNumber | cut -d " " -f3 | sed "s/\"//g")
        #echo $serialNumber

        #OID_upgradeAvailable
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_upgradeAvailable" 2>/dev/null)

        #OID_DSMVersion
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_DSMVersion" 2>/dev/null)
        DSMVersion=$(echo $temp | grep $OID_DSMVersion | cut -d "\"" -f2)
        #echo "$DSMVersion"

        #OID_systemStatus
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_systemStatus" 2>/dev/null)
        systemStatus=$(echo $temp | grep $OID_systemStatus | cut -d " " -f3 | sed "s/\"//g")
        #echo $systemStatus

        #OID_powerStatus
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_powerStatus" 2>/dev/null)
        powerStatus=$(echo $temp | grep $OID_powerStatus | cut -d " " -f3 | sed "s/\"//g")
        #echo $powerStatus

        #OID_systemFanStatus
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_systemFanStatus" 2>/dev/null)
        systemFanStatus=$(echo $temp | grep $OID_systemFanStatus | cut -d " " -f3 | sed "s/\"//g")
        #echo $systemFanStatus

        #OID_CPUFanStatus
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_CPUFanStatus" 2>/dev/null)
        CPUFanStatus=$(echo $temp | grep $OID_CPUFanStatus | cut -d " " -f3 | sed "s/\"//g")
        #echo $CPUFanStatus

        #OID_temp
        temp=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_temp" 2>/dev/null)
        TEMP=$(echo "$temp" | grep "$OID_temp" | cut -d " " -f3 | sed "s/\"//g")
        #echo "$TEMP"

        #Disks state
        for i in $(seq 1 "$nbDISK")
        do
                temp01=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_diskStatus.$(($i-1))" 2>/dev/null)
                temp02=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_diskModel.$(($i-1))" 2>/dev/null)
                temp03=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_diskID.$(($i-1))" 2>/dev/null)
                temp04=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_diskTemp.$(($i-1))" 2>/dev/null)

                status=$(echo "$temp01" | grep "$OID_disk" | cut -d " " -f3)
                diskmodel=$(echo "$temp02" | grep "$OID_disk" | cut -d "\"" -f2 | sed 's/\s//g')
                ID=$(echo "$temp03" | grep "$OID_disk" | cut -d "\"" -f2)
                temp=$(echo "$temp04" | grep "$OID_disk" | cut -d " " -f3)
                if [ "$status" -eq "5" ]; then
                        disques="$(echo "The Hard "$ID" model "$diskmodel" is damaged")\\n"$disques""
                        RETURN="$CRITICAL"
                elif [ "$status" -eq "4" ]; then
                        disques="$(echo "The Hard "$ID" model "$diskmodel" has partitions damaged")\\n"$disques""
                        RETURN="$CRITICAL"
                elif [ "$status" -eq "3" ]; then
                        disques="$(echo "The Hard "$ID" model "$diskmodel" is not partitioned")\\n"$disques""
                        RETURN="$WARNING"
                elif [ "$status" -eq "2" ]; then
                        disques="$(echo "The Hard "$ID" model "$diskmodel" has system partitions but no data")\\n"$disques""
                        RETURN="$WARNING"
                elif [ "$status" -eq "1" ]; then
                        disques="$(echo "The Hard "$ID" model "$diskmodel" is functioning normally")\\n"$disques""
                        #RETURN=$(echo "$OK")
                else
                        disques="$(echo "The Hard "$ID" model "$diskmodel" is in an unknown state")\\n"$disques""
                        RETURN="$WARNING"
                fi
        done

        #RAID State
        for i in $(seq 1 "$nbRAID")
        do
                temp01=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_RAIDName.$(($i-1))" 2>/dev/null)
                temp02=$(/usr/syno/sbin/snmpwalk -OQne -t 10 -v 3 -l authNoPriv -u "$SNMPUSERNAME" -a MD5 -A "$SNMPPASSWORD" "$HOSTNAME" "$OID_RAIDStatus.$(($i-1))" 2>/dev/null)
                Volume=$(echo "$temp01" | grep "$OID_disk" | cut -d "\"" -f2)
                Statut=$(echo "$temp02" | grep "$OID_disk" | cut -d " " -f3)
                if [ "$Statut" -eq "11" ]; then
                        raids="$(echo "The RAID "$Volume" is degraded") "$raids""
                        RETURN="$CRITICAL"
                elif [ "$Statut" -eq "12" ]; then
                        raids="$(echo "The RAID "$Volume" has crashed") "$raids""
                        RETURN="$CRITICAL"
                elif [ "$Statut" -eq "1" ]; then
                        raids="$(echo "The RAID "$Volume" is fine") "$raids""
                else
                        raids="$(echo "The RAID "$Volume" is in an unknown state") "$raids""
                        RETURN="$WARNING"
                fi
        done

        # Nagios States
        if [ "$RETURN" -eq "2" ] || [ "$systemFanStatus" -eq "2" ] || [ "$CPUFanStatus" -eq "2" ] || [ "$powerStatus" -eq "2" ] || [ "$systemStatus" -eq "2" ]; then
                echo -e "CRITICAL - The NAS "$model" - SN : "$serialNumber" - DSM : "$DSMVersion" has a problem"
                echo -e "NAS temperature is "$TEMP"°C - Fan Status : "$systemFanStatus" - CPU Fan Status : "$CPUFanStatus" - Power Status : "$powerStatus" - System Status : "$systemStatus" " | sed 's/ 1 / OK
                for i in "$disques"; do
                        echo -e "$i" | sed '/^$/d'
                done
                for i in "$raids"; do
                        echo -e "$i" | sed '/^$/d'
                done
                FunctionDiskSpace Output

                exit "$CRITICAL"
        fi
        if [ "$RETURN" -eq "1" ]; then
                echo -e "WARNING - The NAS "$model" - SN : "$serialNumber" - DSM : "$DSMVersion" is in Warning state"
                echo -e "NAS temperature is "$TEMP"°C - Fan Status : "$systemFanStatus" - CPU Fan Status : "$CPUFanStatus" - Power Status : "$powerStatus" - System Status : "$systemStatus" " | sed 's/ 1 / OK
                for i in "$disques"; do
                        echo -e "$i" | sed '/^$/d'
                done
                for i in "$raids"; do
                        echo -e "$i" | sed '/^$/d'
                done
                FunctionDiskSpace Output

                exit "$WARNING"
        fi
        if [ "$RETURN" -eq "0" ]; then
                echo -e "OK - The NAS "$model" - SN : "$serialNumber" - DSM : "$DSMVersion" is fine"
                echo -e "NAS temperature is "$TEMP"°C - Fan Status : "$systemFanStatus" - CPU Fan Status : "$CPUFanStatus" - Power Status : "$powerStatus" - System Status : "$systemStatus" " | sed 's/ 1 / OK
                for i in "$disques"; do
                        echo -e "$i" | sed '/^$/d'
                done
                for i in "$raids"; do
                        echo -e "$i" | sed '/^$/d'
                done
                FunctionDiskSpace Output

                exit "$OK"
        fi
        echo -e "CRITICAL - The NAS "$model" - SN : "$serialNumber" - DSM : "$DSMVersion" is in an indeterminate State"
        exit "$CRITICAL"
fi
Licence Creative Commons
This websitehttp://shebangthedolphins.net is licensed to the public under a licence Creative Commons Attribution licence.
Contact :