Zabbix-DHT
What?
This repository contains templates and scripts to easily log DHT11/DHT22 sensor values with Zabbix. It was developed and tested on standard Raspbian and is currently running on several Raspberry Pis (the models being 1, 3 and Zero).
I works like this:
- A shell script, regularly invoked by cron, reads the sensor metrics. You can configure the number of retries and a delay in between if it doesn't work. There is also a very simple plausibility check of the result. After a successful read the values and the current timestamp are saved in a file.
- A second shell script is executed by the Zabbix agent; it simply parses the previously created file. If the file contains data in the correct format and the timestamp is not too old (configurable) it prints the requested value, otherwise an error
- Zabbix logs the values
Why?
Originally I configured Zabbix to read the sensor directly, through commands in the userparameter_dht.conf. Though this worked most of the time it had some drawbacks.
First, the timeout for agent checks needed to be raised to 5-6 seconds to work reliably. Supposedly this is not a problem but still... did not feel right. Second and more importantly, sometimes the results were stuck in the qeueue for a long time. When it happened, most of the time it resolved itself after some hours, sometimes a restart of the proxy was necessary.
I never figured out why and what caused it; but with the setup provided in this repo it does not happen. You can also use the same data for something else without additional queries to the sensor.
How?
The instructions are for Raspbian under the assumption that the script will be installed in /opt
- Install dependencies:
sudo apt update
sudo apt install git python-pip
sudo python -m pip install --upgrade pip setuptools wheel
- Download the repository:
cd /opt
git clone https://code.shdx.org/Der_Leopold/Zabbix-DHT.git
- Optional: Install the Adafruit tool that reads the sensor data
Skip this step if you already have it.
cd Zabbix-DHT
git submodule update --init
cd external/Adafruit_Python_DHT
sudo python setup.py install
- Edit the readSensor script and change as needed. At least the two variables SENSOR and GPIOPIN have to be adjusted to your setup.
- Run the script:
./readSensor
- If everything works, it the script created the output file with content similar to this:
Timestamp=1543420306
Temperature=23.7
Humidity=39.7
- Set up a cronjob to keep the output file up to date:
crontab -e
*/1 * * * * /opt/Zabbix-DHT/readSensor
- Copy the file userparameter_dht.conf to your Zabbix config directory:
sudo cp userparameter_dht.conf /etc/zabbix/zabbix_agentd.conf.d/
-
Restart Zabbix agent to pick up the change:
sudo service zabbix_agentd restart
-
Finally, import the template (template_dht.xml) into Zabbix and assign it to the host
After a few minutes you should see data starting to come in.
Who?
I'm codeing purely as a hobby and do it far too infrequent to have any kind of practice. So please bear with me if the code is amateurish...