commit 5db92da93a803fc2f19071cad22950951be96559 Author: Der_Leopold Date: Tue Dec 4 17:31:33 2018 +0100 initial upload diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53752db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output diff --git a/parseOutput b/parseOutput new file mode 100755 index 0000000..90791f2 --- /dev/null +++ b/parseOutput @@ -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 " + 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 + diff --git a/readSensor b/readSensor new file mode 100755 index 0000000..8282a22 --- /dev/null +++ b/readSensor @@ -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: [