At work we use the excellent Nagios software for monitoring our servers and network equipment as well as whole bunch of other stuff. One thing I’ve always wanted though, was to get environmental data from our server room into Nagios. Many commercial products exists out there that can be integrated with Nagios, but they are often quite expensive compared to the features they provide and closed-source. So in the spirit of making, I set out to build my own OSH network sensor to be integrated with our Nagios system. It turned out to be interesting, easy, great fun and immensely satisfying (all while saving us some money). Read on for an in-depth account of making a DYI network sensor for Nagios or similar.
Being my first go at this I wanted to limit the amount of sensors to the three most needed:
- Room temperature
- Relative humidity
- Light levels
These are also the sensors the typical commercial network sensor is equipped with (maybe also with stuff like contact closure and voltage measurement). Also I wanted my project to be networked for flexible placement and integration using the existing infrastructure. Lastly it should be easy to prototype and extend with other sensors. Being quite confident with the Arduino platform, the natural choice seemed to be the Arduino Ethernet as the unit to bridge sensors and ethernet. To measure temperature and humidity I used the awesome SHT11 sensor from Sensirion and for lighting levels the almost equally awesome TSL2561 from AMS (used to be TAOS). Both are quite Arduino friendly with libraries already available, and both can be purchased in breadboard compatible packages from friendly maker stores like Adafruit. First of all I wanted to see the sensors to work with the Arduino so I hooked everything up on a breadboard and wrote a quick sketch that would output the three sensor readings to the serial console.
From this point on it was possible to focus on getting the data from the Ethernet to Nagios across the network. Since Nagios most often rely on ‘active’ checks (Nagios requesting data) one solution is to let the Arduino run a small server that would return the readings in a well-defined format when requested. Luckily the Ethernet library provides basic TCP/IP client/server functionality and some great examples – I was able to write a sketch using almost only copy-paste code from these. In essence the server on the Arduino acts as a very simple web server. It returns an HTTP response – an HTML page with the formatted data – upon receiving an HTTP request, or actually just the final new-line part of the request. The nice thing is you can use a we browser to test the Arduino sketch. I ran a test, everything worked, and the sensors plus some wiring was moved to a perma-proto board for added sturdiness. Everything was then stripped to some random cardboard and ready to go.
Notice the black wires on the right going to ethernet and power plug. It’s a passive PoE injector cable providing power and network over the same standard twisted-pair network cable. It is possible to use ‘true’ PoE with the PoE module from Arduino but I wanted to try this one for size.
With the Arduino part working, it was time to write my first Nagios plugin. It turned out documentation was excellent and that the API is very simple and easy to use. Basically you write some code that outputs a special string to stdout and has a return value indication a certain status. The string is then parsed by Nagios and its parts used in various ways inside Nagios. What was particularly interesting to me is the option to pass performance data appended to this string. Even if the data in this case woud be just a temperature or a percentage, Nagios will store the data using RRDTool and can produce nice looking graphs of the logged values. Without going into to much detail Nagios must have the new plugin configured as a ‘command’ which in turn must be used to configure a service check.
A quickly written Python script got me a working plugin, and we now a completely DIY Nagios integrated network sensor. Notifications and alerts are set to trigger when temperature and humidity reach certain thresholds, while the historical data also gives insight into not so obvious problems (like fluctuations over time). The project was a great success and will surely be followed up by adding sensors and abilities to the platform.
Source code and more can be found on github. Enjoy.