add script to be used by zabbix agent to parse output file
This commit is contained in:
81
readOutput.sh
Executable file
81
readOutput.sh
Executable file
@ -0,0 +1,81 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright (c) 2018 leopold@shdx.org
|
||||||
|
# Author: Der_Leopold
|
||||||
|
#
|
||||||
|
# This script simply parses the text file created by readSensor.sh and
|
||||||
|
# prints the requested value. It is supposed to be called by the Zabbix
|
||||||
|
# agent.
|
||||||
|
# To prevent processing of stale data, it compares the timestamp from
|
||||||
|
# the file to the current one and returns an error if the difference is
|
||||||
|
# too big.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Maximum data age in seconds. If the data is older, print an error and exit.
|
||||||
|
MAXAGE=300
|
||||||
|
|
||||||
|
# File to read the sensor data from
|
||||||
|
DATAFILE=/opt/Zabbix-DHT/output
|
||||||
|
|
||||||
|
function print_usage () {
|
||||||
|
echo ""
|
||||||
|
echo "Usage:"
|
||||||
|
echo ""
|
||||||
|
echo " readOutput.sh <temperature|humidity>"
|
||||||
|
echo ""
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_datafile () {
|
||||||
|
# REGEX="^(\w+)=(\d+\.*\d*)$" # should work but does not?
|
||||||
|
REGEX="^(\w+)=(.*)$"
|
||||||
|
while read -r LINE; do
|
||||||
|
if [[ $LINE =~ $REGEX ]]; then
|
||||||
|
KEY=${BASH_REMATCH[1]}
|
||||||
|
VALUE=${BASH_REMATCH[2]}
|
||||||
|
case $KEY in
|
||||||
|
"Timestamp")
|
||||||
|
THRESHOLD=$(( $VALUE + $MAXAGE ))
|
||||||
|
TS=$(date +"%s")
|
||||||
|
if (( $TS <= $THRESHOLD )); then
|
||||||
|
TSVALID=1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
"Temperature")
|
||||||
|
if [ $PARAM == "temperature" ]; then
|
||||||
|
RESULT=$VALUE
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
"Humidity")
|
||||||
|
if [ $PARAM == "humidity" ]; then
|
||||||
|
RESULT=$VALUE
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done < "$DATAFILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
PARAM="$1"
|
||||||
|
|
||||||
|
if [[ ( ! $# -eq 1 ) || ( $PARAM != "temperature" && $PARAM != "humidity" ) ]]; then
|
||||||
|
print_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -r $DATAFILE ]; then
|
||||||
|
echo "Error: cannot read data file"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
TSVALID=0
|
||||||
|
parse_datafile
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $TSVALID -eq 0 ]; then
|
||||||
|
echo "Error: Data file contains old or invalid data"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "$RESULT"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
Reference in New Issue
Block a user