Raspberry Pi Tower Monitor

Raspberry Pi Tower Monitor

Having active equipment run at remote sites has always been worrisome with heat waves, but typically you have a few spare devices that you just use to replace failed equipment. Now, cabinets do exist that provide heaters or air conditioning, but lets be real. Those are expensive. More expensive than a couple switch or UPS replacements over 10 years, and you have to perform maintenance on them every year.

Has this ever happened to you? You are poor, and can’t afford a generator to run your tower 24/7 when there is a power outage, so you buy a cheap UPS to run your tower, just long enough to run a small gas generator out to the site. But to know when the power goes out, you put a raspberry pi in the tower to fail when the AC drops and then have your alerting system tell you that you need to hustle a generator out to the tower or it’s going to shut down. Well that pi can tell you a lot more than just a ICMP/SNMP up/down state. I set out in my small amount of free time for side projects to see if I could gather more environmental data. The answer was astoundingly, yes.

Lets not re-invent the wheel here. Temp sensors for RPI’s have existing for a very long time. And there are plenty of documentation available to help you build one. But what about attempting to standardize the process in a way where you can turn these suckers up at a higher rate and monitor them with ease over your already existing SNMP poller? You answered right, yes again.

The Parts

I won’t bore you or spoon-feed you, I got a couple DS18B20 Temperature Sensors and wired them into the GPIO ports of the pi. Using a combination of this tutorial, this one and this one, we had a device that was built, connected and interfaced with the pi.

In order to use the GPIO, we need to modify the pi /boot/config.txt file

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
dtoverlay=w1-gpio,gpiopin=4

Add the modules to /etc/modules

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
w1_gpio
w1_therm

Using commands like

/sys/devices/w1_bus_master1/"28-021564b3ebff"/w1_slave

could print out the temp to the terminal, but this information wasn’t super useful for monitoring. So lets combine a few pieces of information. We need to be able to pull this data on a timer, via monitoring system. So we’ll use SNMP, duh. Install the Debian bc package. Now we need to script it. Each temp sensor has a unique ID, but they are all prefixed with 28-, so we grep for system sensor, run the command and print the information out after cleaning it up a little. Behold, the get-temp.sh script

#!/bin/bash
#
# Usage: gettemp.sh
# e.g. gettemp.sh 28-021564b3ebff
SENSOR=$(ls /sys/bus/w1/devices/ | grep 28-*)
SLAVE="/sys/devices/w1_bus_master1/"$SENSOR"/w1_slave"
OUTPUT=$(/bin/cat $SLAVE | /usr/bin/awk -F 't=' ' { printf $2 } ')
temp=`echo "scale=2; "\`echo ${OUTPUT##*=}\`" / 1000" | bc`
echo $temp

Pretty cool, but lets bridge the SNMPD config with the pi.

rocommunity cabletown
syslocation Little Rock, AR
syscontact John Doe <jdoe@fabrikam.com>

extend .1.3.6.1.3.1.1 temp1 /bin/bash /opt/temp-monitor/get-temp.sh

Pretty cool, do an SNMP poll on .1.3.6.1.4.1.2021.13.16.2.1.3.1, we can get the following

---------------------- New Test -----------------------
Paessler SNMP Tester - 20.2.4 Computername: <LOL>
6/25/2022 2:08:04 PM (7 ms) : Device: 10.10.10.10
6/25/2022 2:08:04 PM (11 ms) : SNMP v2c
6/25/2022 2:08:04 PM (13 ms) : Custom OID .1.3.6.1.4.1.2021.13.16.2.1.3.1
6/25/2022 2:08:05 PM (932 ms) : SNMP Datatype: ASN_UNSIGNED
6/25/2022 2:08:05 PM (935 ms) : -------
6/25/2022 2:08:05 PM (938 ms) : Value: 36750
6/25/2022 2:08:05 PM (941 ms) : Done

36.8 Degrees Celsius, it’s getting warm in that cabinet. Anyway, piping that SNMP into the config to trigger the script makes it possible to graph this sucker and set alerting thresholds.

Now, you can throw the whole thing into puppet, and deploy them with ease.

class profile::rp_temp {
  # The following are present on all base devices

  if ($operatingsystem == 'Raspbian') {
    # BC
    package { "bc":
        ensure => "installed"
    }

    file { '/boot/config.txt':
      ensure  => present,
      owner   => 'root',
      group   => 'root',
      mode    => '0775',
      source  => 'puppet:///modules/site_monitor/config.txt',
    }

    file { '/etc/modules':
      ensure  => present,
      owner   => 'root',
      group   => 'root',
      mode    => '0775',
      source  => 'puppet:///modules/site_monitor/modules',
    }

    file { '/etc/snmp/snmpd.conf':
      ensure  => present,
      owner   => 'root',
      group   => 'root',
      mode    => '0644', 
      source  => 'puppet:///modules/site_monitor/rp-snmpd.conf',
    }

    file { '/opt/temp-monitor': 
      ensure  => 'directory',
      owner   => 'root',
      group   => 'root',
      mode    => '0775'
    }

    file { '/opt/temp-monitor/get-temp.sh':
      ensure  => present,
      owner   => 'root',
      group   => 'root',
      mode    => '0775',
      source  => 'puppet:///modules/site_monitor/get-temp.sh',
    }
  }
  
}
Comments are closed.