Nagios script to check the online status of devices.

Matyi Szabolcs10 months ago

Hi All,

This is a simple BASH script to check the online status of Traccar devices with Nagios.
You must enter the time in seconds, which must not be exceeded.
If no entry is received from the device for the specified period of time (-t), Nagios will notify you.

How to use:

~]$ /usr/lib/nagios/plugins/check_traccar_device_online -i 7501043656 -t 600 -l /mnt/sda3/containers/alpine-gps/rootfs/opt/traccar/logs/tracker-server.log
OK - Device 7501043656 is online.

Description text

Code:

#!/bin/bash
#set -x


print_help() {
    cat << EOH
___                __             __            
 | _ _  _ _ _  _  |  \ _  . _ _  /  \ _ |. _  _ 
 || (_|(_(_(_||   |__/(-\/|(_(-  \__/| )||| )(- 
                                                

    Usage:
        -i, --identifier	-GPS tracker identifier number.
        -t, --time-interval	-GPS device data sending time interval in seconds. If this time is exceeded, it indicates offline.
        -l, --log-path          -Path to the Traccar log file.
        -h, --help		-This help.

Example: /usr/lib/nagios/plugins/check_traccar_device_online -i 7501043656 -t 600 -l /Path/To/Traccar/logs/tracker-server.log
OK - Device 7501043656 is online.

EOH
}

OPTS="$(getopt -o i:t:l:h -l identifier:,time-interval:,log-path:,help -- "$@")"

eval set -- "$OPTS"

while true; do
    case $1 in
        -i|--identifier)
            IDENTIFIER="${2}"; shift 2;;
        -t|--time-interval)
            TIME_INTERVAL="${2}"; shift 2;;
        -l|--log-path)
            LOG_PATH="${2}"; shift 2;;
        -h|--help)
            print_help; exit 0;;
        --)
            shift; break;;
        *)
            break;;
    esac
done

ACT_TIME=$(date +%H:%M)
AUTO_LAST_ONLINE=$(grep ${IDENTIFIER} ${LOG_PATH} | tail -1 | awk '{ print $2 }' |sed 's/...$//')
TIME_DIFF=$(dateutils.ddiff $ACT_TIME $AUTO_LAST_ONLINE |sed 's/.$//' | sed 's/^.//')

if [[ $TIME_DIFF < ${TIME_INTERVAL} ]]; then

   echo "OK - Device ${IDENTIFIER} is online."
   exit 0

else
   echo "CRITICAL - Device ${IDENTIFIER} is offline."
   exit 2
fi

Github:
https://github.com/unix1984/traccar_device_online