Files
sensor-1wire/parseOutput
2020-07-11 11:16:19 +02:00

87 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright (c) 2018 leopold@shdx.org
# Author: Der_Leopold
#
# This script simply parses the text file created by readSensor 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-1wire/output
function print_usage () {
echo ""
echo "Usage:"
echo ""
echo " parseOutput <LuftTemp|WasserTemp|BodenTemp>"
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
;;
"LuftTemp")
if [ $PARAM == "LuftTemp" ]; then
RESULT=$VALUE
fi
;;
"WasserTemp")
if [ $PARAM == "WasserTemp" ]; then
RESULT=$VALUE
fi
;;
"BodenTemp")
if [ $PARAM == "BodenTemp" ]; then
RESULT=$VALUE
fi
;;
esac
fi
done < "$DATAFILE"
}
PARAM="$1"
if [[ ( ! $# -eq 1 ) || ( $PARAM != "LuftTemp" && $PARAM != "WasserTemp" && $PARAM != "BodenTemp" ) ]]; 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