Temperature and humidity will have a substantial effect on the lifespan of your hardware. Almost all computers and servers have several temperature sensors for gathering component or ambient temperatures. Humidity sensors though, are often overlooked. With very few and inexpensive components, we can monitor both in Zabbix.

In short, we hook up an DHT22/AM2302 sensor to a Raspberry Pi 3, load Raspbian onto it, install the Zabbix Agent and configure it to pull temperature and humidity data through a small Python script.

Although this can seem difficult to accomplish, trust me, it isn’t!

Requirements

All necessary components can be found at DX.com which also offers world-wide free shipping.

The parts total about $72, the Raspberry Pi and power supply being the most costly at $53 combined. If you already have a Raspberry Pi or Raspberry Pi 2, these will do just fine. Any decent 1A+ micro USB power adapter, Micro SD card and SD card reader will also get the job done.

This guide is written using a Raspberry Pi 3 with Raspbian Jessie Lite, Python 2.7 and Zabbix Agent 2.2.7 installed.

Download Raspbian Lite Download Raspbian Lite: https://www.raspberrypi.org/downloads/raspbian/

Write Raspbian to your SD card Install Win32DiskImager: https://sourceforge.net/projects/win32diskimager/files/latest/download Extract the Raspbian IMG file from the downloaded ZIP file Open Win32DiskImager, select your Raspbian IMG file, select your micro SD card device and click “Write”. When finished, insert the micro SD card into your Raspberry Pi.

Hook up your Raspberry Pi and connect via SSH Connect your Raspberry Pi to your local network with an Ethernet cable and plug in the power supply. After about 30-60 seconds, the Raspberry Pi is booted. To find the IP of the Pi, you can use a network scanning tool like SoftPerfect Network Scanner or similar. Connect to the Pi using the Putty SSH client. The default username is “pi” and the password is “raspberry”.

Connect the sensor module With the Pi booted and ready to go, we now have to connect the sensor module. The sensor module has 3 pins. Looking at the front, they are: Left: 3.3v

Middle: GPIO

Right: Ground With the module, you also received a set of wires, red, brown and black. Red goes to the left pin, brown to the middle one and black goes to the right pin. The pins now have to be connected to the Pi’s GPIO pins, which is the two large rows of pins at the edge of the Pi board. Note, Raspberry Pi 2 and 3 pin placements are identical. Connect the wires in this order: Red (3.3v) → Any orange pin

Brown (GPIO) → Pin number 2

Black (ground) → Any black pin You can take a peak at the top image, to see how I’ve connected my module.

Installing software on the Pi Having earlier connected to the Pi via SSH, we now have to install the necessary software to pull temperature and humidity data from the sensor module. In the Putty SSH console, do the following: sudo apt-get update && sudo apt-get -y install git python-dev git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT/ sudo python setup.py install This ensures we have the necessary Python module compiled and installed, which enables us to pull data via the GPIO integrated circuit.

Test your sensor! We should now be able to pull data from the module with an already provided example script. To do so: sudo /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 2 In your console, you should now receive output similar to: The first argument denotes the model we’re using, the DHT22. The second argument is the GPIO pin # we chose earlier. The temperature received is in Celcius and the humidity is in percent. We’ve successfully pulled data from the sensor module. Let’s configure Zabbix Agent to do the same!

Zabbix Agent configuration We can run Python scripts manually, but luckily the Zabbix Agent can as well. First, install the Zabbix Agent on the Pi: sudo apt-get -y install zabbix-agent We need to slightly modify the Zabbix Agent configuration file. Open the configuration file: sudo nano /etc/zabbix/zabbix_agentd.conf NOTE: The “Server” parameter is used for whitelisting IP addresses you wish to allow communication with the Zabbix Agent. For testing purposes, we’re using the default of “127.0.0.1” which means we can ONLY communicate with the agent locally. This parameter will need to be configured to the correct IP address of your Zabbix server when you wish for the server retrieve data from the agent. Insert this line exactly as is, at the end of the configuration file: UserParameter=dht.pull[*],sudo /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 2 | awk -F[=*%] '{print $$$1}' Ctrl+X to quit, “Y” to save and then <Enter>. NOTE: Some might experience longer than usual polling time when querying environment data from the sensor module. This can result in the Zabbix Agent UserParameter script timing out. To circumvent this issue, you can increase the configuration parameter ‘Timeout’ from the default of 3 seconds, to 5 seconds instead, or longer if necessary. Restart the Zabbix Agent: sudo service zabbix-agent restart We also have to grant the “zabbix” user the correct permissions to pull GPIO data. We need to allow it to act as the root superuser, when executing the AdafruitDHT.py script. Raspbian uses the “sudo” program for this. Open the “sudo” configuration by typing: sudo visudo And the bottom of the file, insert: zabbix ALL=(ALL) NOPASSWD: /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py Ctrl+X to quit, “Y” to save and then <Enter>.