Paessler Blog - All about IT, Monitoring, and PRTG

How To Monitor Server Room Temperature With PRTG On A Budget

Written by Torsten Lindner | May 15, 2017

We want to monitor the temperature of our server room, and we want to do it on a budget!Maybe because we have to, or maybe just because we can. What do we need?

  • Raspberry Pi Zero W: ca. $10
  • Power supply for the Pi: ca. $10 (you can even use a Powerbank to make this thing mobile!)
  • Mirco SD Card for the Pi: ca. $8
  • Case for the Pi: ca $5
  • Enviro pHAT for the Pi: ca. $16
  • Obviously we need PRTG Network Monitor with its HTTP Push Data Advanced Sensor

As we use the free version of PRTG (limited to 100 sensors), that brings us to a grand total of 49$. If that's not on a budget, I don't know what is. And we get to have fun and play with a Raspberry PI and even do a bit of soldering if that's your thing. If you prefer to connect the Enviro pHAT not via soldering, there are solder-free options offered from certain RaspberryPi oriented shops, just run it through Google.

You can also opt for a full Raspberry Pi, that will drive up the cost to about 30-35$, but gets you the benefit of wired Ethernet connectivity.

As a prerequisite, the Raspberry Pi should be ready to start, SD card inserted, with an either full or lite installation of Raspbian, SSH access enabled, and Python3 installed (it's part of the full Raspbian Installation, but not the lite one, so you may need to install it via sudo apt-get). And it needs to be able to connect to your PRTG installation, either wirelessly or via Ethernet.

 

iA server is a computer or system that provides resources, data, services, or programs to other computers, known as clients, over a network. In theory, whenever computers share resources with client machines they are considered servers. There are many types of servers, including web servers, mail servers, and virtual servers. Read more ...

Hardware Assembly

This is pretty straight forward, plug or solder the Enviro pHAT onto the Pi, and connect it to the power supply. Voila.

The Software side of things

This has a few more steps.

Install the necessary package on the Pi.

There are three options to do this, choose the one which suits your python work flow best:

  • Do a full install of the package, directly from pimoroni (the vendor of the Enviro pHAT):
    curl https://get.pimoroni.com/envirophat| bash
  • Global installation for the entire Pi, do this if the Pi won't do anything else:
    sudo apt-get install python3-envirophat
  • Installation within a virtual environment just for this project (if the Pi is running other Python scripts / applications, and you don't want to risk any interferences with their libraries / modules):
    sudo /home/pi/enviro_env/pip3 install envirophat (The path is just an example of course.)

That's it, let's get into the script!

The Python Script, Which Collects The Data And Sends It To PRTG

Modules, what do we need? All of them! j/k. We need:

  • import time
  • import requests
  • from envirophat import weather, light

Time is necessary for setting the script to sleep for 60 seconds after its tasks have been done, because we are letting it start when the Pi boots, and then run it in an endless loop. requests helps us sending the data to PRTG and weather and light from envirophat are necessary to measure the values in the first place.

Then we need connection data so that the script knows where to send the data:

prtg_host = 'IP_OF_THE_PRTG_PROBE'
prtg_host_port = '5050'
prtg_sensor_token = '1420F0AD-2249-428A-B176-089E113EA551'

If you want to run the HTTP Push Data Advanced Sensor, the prtg_host needs to be the IP/hostname of the Remote Probe of course. Port & Token can be set and then taken from the HTTP Advanced Sensor:

Port and Token from the HTTP Advanced Sensor

Houston we have a function! That reads the temperature, air pressure and light, packing them into a nice json structure, which it returns to the main part of the script.

Take a look:

temperature = weather.temperature()
pressure = round(weather.pressure(), 2)
light = light.light()
json_response = {
   "prtg": {
      "result": [
      {
         "channel": "temperature",
         "float": 1,
         "value": temperature
      },
      {
         "channel": "pressure",
         "float": 1,
         "value": pressure
      },
      {
         "channel": "ambient light",
         "float": 1,
         "value": light
      }
      ]
   }
}
return json_response

The 'rest' is a never-ending while loop. It calls the function to gather the values, and send the values to the set PRTG instance. If it cannot reach PRTG (for example, because PRTG is being updated in that moment), it will wait for the next turn.

try:
   json_response = get_values()
   # print output for debugging
   # print(json_response)
   json_string = str(json_response)
   json_string = str.replace(json_string, '\'',
'\"')
   prtg_request_URL = 'http://' + prtg_host +
':' + prtg_host_port + '/' + prtg_sensor_token +
'?content=' + json_string
   # print(prtg_request_URL)
   request = requests.get(prtg_request_URL)
   # print(request.status_code)
except:
   pass

 

After the values are collected, requests is used to transmit them to PRTG with an HTTP GET request. The prints of the request URL, status code and before the json content, are intended for debugging, you can of course remove them once the script is running smoothly.

And we're done. Look Ma'! Values in PRTG: 

 
 

The interval is set to 60 seconds for the example. 5 minutes would be good just as well. While air pressure may not be that interesting, the light - value can be used a presence indicator (alert if someone is in the server room, when there shouldn't be!).

The Enviro pHAT even offers a motion sensor. This could be used to read motion data to serve as tempering- /burglar alert depending how you mount the Pi/ Enviro pHAT.

One final thought, if the Enviro pHAT is mounted directly onto the Pi, the temperature reading will have a certain offset, because of the proximity to the CPU of the Pi. In a server room this off set should be quite stable, so you can use a Factory Sensor in PRTG to subtract it. Or you'll bring a bit of distance between the Pi and the Enviro pHAT with cables to connect them.

Get The Full Code

For the full code in a piece please check this link.

The HTTP Push Data Advanced Sensor

If it's about IoT and integrating devices without IP-address into PRTG, the http Push Data Advanced Sensor is a very powerful tool. Learn more about it at the PRTG manual.

The Author

Before joining our tech support team about 9 years ago, Torsten used to work as an IT administrator in Germany and China. In his free time he likes to play around with tech stuff to enhance his programming skills, and he writes about it in his private blog.