initial upload

This commit is contained in:
2018-12-04 17:31:33 +01:00
commit 5db92da93a
4 changed files with 173 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
output

86
parseOutput Executable file
View File

@ -0,0 +1,86 @@
#!/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 " readOutput <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

83
readSensor Executable file
View File

@ -0,0 +1,83 @@
#!/bin/bash
#
# Copyright (c) 2018 leopold@shdx.org
# Author: Der_Leopold
#
# This script tries to read sensor values from a 1wire bus and writes the results
# and a timestamp to a file. It is intended to be executed periodically, for
# example via crontab.
#
# Retry up to n times if the reading fails
RETRY=3
# Number of seconds to wait between retries
DELAY=3
# Specify the temperature range where you expect the sensors will be operating
# This is simply used as a safeguard to detect implausible sensor readings
MINTEMP=-30
MAXTEMP=40
# Interface that is connected to the 1wire bus
USETTY=/dev/ttyUSB0
# Location of the digittemp config file with data about the sensors
DIGITEMPCONF="/etc/digitemp.conf"
# Command to use for reading the sensors
CMD="sudo /usr/bin/digitemp_DS9097"
CMDOPTS=" -q -s $USETTY -c $DIGITEMPCONF"
# Define sensors that should be queried
# Format: [<Label>]=<Id>
declare -A SENSORS=( [0]=LuftTemp [1]=WasserTemp [2]=BodenTemp )
# Location where the readings should be stored
OUTFILE="output"
#
# no configuration/changes should be necessary below this line
#
cd "$(dirname "$0")";
touch "$OUTFILE" 2> /dev/null
echo "Timestamp="`date +%s` > "$OUTFILE"
if [ ! -w $OUTFILE ]; then
echo "Error: output file '$OUTFILE' not writeable"
exit 1
fi
function read_sensor () {
OUTPUT=`$CMD $CMDOPTS -t $SENSORID`
VALUE=`echo "$OUTPUT" | awk -F "C: " '{print $2}' | awk -F " F:" '{print $1}'`
CNT=$(($CNT+1))
}
function check_output () {
if [[ $OUTPUT =~ fail ]] || [[ $OUTPUT =~ owAcquire ]] || [ ${VALUE%.*} -lt $MINTEMP ] || [ ${VALUE%.*} -gt $MAXTEMP ]; then
if [ $CNT -le $RETRY ]; then
sleep $DELAY
read_sensor
check_output
else
echo "${SENSORS[$SENSORID]}=Error" > "$OUTFILE"
fi
fi
}
function write_results () {
echo "${SENSORS[$SENSORID]}=${VALUE}" >> "$OUTFILE"
}
for SENSORID in "${!SENSORS[@]}"
do
CNT=0
read_sensor
check_output
write_results
sleep $DELAY
done

3
userparameter_1wire.conf Normal file
View File

@ -0,0 +1,3 @@
UserParameter=1wire.luft_temp[*],/opt/Zabbix-1wire/parseOutput LuftTemp
UserParameter=1wire.wasser_temp[*],/opt/Zabbix-1wire/parseOutput WasserTemp
UserParameter=1wire.boden_temp[*],/opt/Zabbix-1wire/parseOutput BodenTemp