Wednesday, March 20, 2013

Raspberry Pi and snmp polling using ds18b20 temp sensors

Finally, after visiting the end of the internet and talking with countless people about hacking code up, I think I have put together a decent little system for polling temp readings off of a digital DS18B20 probe. Ill attempt to map out everything I have done from start to finish. Here goes:



Current Setup:

I got the Wheezy build from here and followed their instructions on how to put it on the SD card in the paragraphs above the download section. Depending on what OS you are using, there are obviously different programs to use.

I hooked the Pi up to a screen, keyboard, mouse and network, stuck the SD card in and, after a little initial setup, i was off and running. I gave it a static by adding these lines to the /etc/network/interfaces file:

iface eth0 inet static
        address IP ADDRESS
        netmask NETMASK
        gateway GW
        dns-nameservers DNS

I commented out the line that said "iface eth0 inet dhcp" because we don't need it for a static. Either one or the other. Now, either restart the Pi or restart the daemon for the network settings to take effect. I've had more luck rebooting....

Following directions from here, 

Step One: Updating the Kernel
  • The first step is to change where our Pi updates from, by editing a text file. We need to open up the file /etc/apt/sources.list.d/raspi.list as root, so type:
    sudo leafpad /etc/apt/sources.list.d/raspi.list
  • Now change the line in this file so that it reads "deb http://archive.raspberrypi.org/debian/ wheezy main untested", then save and close the file.
  • Next, do the following commands:
    apt-get update
    apt-get upgrade
Next comes wiring the probe and connecting it to the Pi. Using the directions here, I wired it to look similar to this pic:

Obviously, it can be set up any way but it must be connected correctly. I'm talking about using different pins on the breadboard and placement and such.

After that is done and the ribbon cable is connected, I powered the device on. You should feel the probe during this initial power-up. If it gets hot, which it will within seconds, turn it off and check your wiring. 

Running these two commands:

Sudo modprobe w1-gpio
sudo modprobe w1-therm

will fire up the probe. These will need to be put in a start-up file upon boot if ever the device is restarted. After those two commands complete, it will be dumping data into a file located at 

/sys/bus/w1/devices/SERIAL OF PROBE/w1_slave

The serial of the probe can be found by doing an "ls" inside the /sys/bus/w1/devices folder. In the pic at the top, I found what that number was and made a yellow label for it since that serial will never change. Easier to label them rather than trying to remember! :)

Running "cat w1_slave" inside that serial folder will produce two lines of output. the very last few digits will be the temperature in Celsius. Its at the thousands place so the python code further down that links page will tell us how to divide by 1k.

The following code is taken from that page with a little tweak at the end to convert it to Fahrenheit:

# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.

tfile = open("/sys/bus/w1/devices/28-00000474e0b1/w1_slave")

# Read all of the text in the file.

text = tfile.read()

# Close the file now that the text has been read.

tfile.close()

# Split the text with new lines (\n) and select the second line.

secondline = text.split("\n")[1]

# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).

temperaturedata = secondline.split(" ")[9]

# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.

temperature = float(temperaturedata[2:])

# Put the decimal point in the right place and display it.

temperature = temperature / 1000

# Convert Temperature to Fahrenheit

temperature = (temperature * 9) / 5 + 32

# Print Temperature

print int(temperature)

The 28-00000474e0b1 part in that first line is the serial of MY probe. That will be different for you, of course. Copy and paste that, using your serial number, into a file called temp.py and place it wherever. to run it, type:

python temp.py

and it should return a rounded-like (because of the int() statement) integer that is the Celsius degrees converted to Fahrenheit. The probe works and the temp is being converted. That step is done.

Once we have all that done, we can install SMNPD and get it to respond to queries from a remote machine. This proved to be the most difficult for me because, up until this point, I always piggybacked on an existing SNMP install with the OIDs already there. Building this from scratch was definitely tricky for an SNMP nub like me.

So, taking directions from a lot of places, Ill post links to the most helpful places here.

Following directions from here, I installed SNMPD by running:

sudo apt-get update
sudo apt-get install snmpd
sudo apt-get install snmp

Im not 100% sure if i needed the client portion, but I installed it anyway during troubleshooting steps. 

Please be aware that this is for a totally internal setup with no outward facing access therefore its pretty wide open. You'll want to check up on securing your SNMP install if you choose to have it facing the internet. 

You'll need to edit your snmpd.conf file by typing:

sudo nano /etc/snmp/snmpd.conf

Here is where it got tricky. I had a line in there that read:

agentAddress  udp:127.0.0.1:161

which i commented out and added this line below it:

agentAddress udp:161

which I'm assuming from what I read allows for any machine with explicit access to get queries. Then, further down in that config, I found the rocommunity lines. I added these lines:

rocommunity public IP OF REMOTE MACHINE
rocommunity public localhost

not sure if that last one was there or not to begin with. Then commented out:

rocommunity public  default    -V systemonly

just for good measure! :) it looked like it might have caused a problem. Not sure if it was needed but, again, SNMP newb here. Make sure you restart the daemon to make your changes stick:

sudo /etc/init.d/snmpd restart

once those are done, from the local machine, you should be able to type:

snmpwalk -v 1 -c public localhost

and it will throw a ton of stuff at you from the console. you can also try that same command from a remote machine instead putting in the IP of the Pi rather than localhost and it should do the same thing.

From this moment on it gets a touch more tricky. Getting an OID to match to the temp probe output. remember the temp probe? yeah its been a while....

Continuing to follow the instructions here, you must do a few things which I will list:
  • make a script to call the python file we created in the first steps above
  • config a new OID inside the snmpd.conf to point to this new script
Easy. Kinda. If you originally knew what you were doing....

I grabbed the executable from that link above and changed it a bit:

#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.8.0
echo gauge
python /usr/local/bin/temp.py
fi
exit 0

copy and paste that above into a file called snmp-temp. Then run this:

sudo chmod 755 snmp-temp

which will make it executable and try running it:

./snmp-temp -g

Please notice that I changed the directories where these files are located which you will need to do as well for this to work. If they are all in the same place calling the same files, you should be ok. SNMP likes them to be in that /usr/local/bin directory. i was NOT able to run them out of my home directory where i was originally building these files. i moved both the python scripts and the shell scripts to that location and everything is working fine.

once you run that file, you should get output in three lines:

1.3.6.1.2.1.25.1.8.0
gauge
OUTPUT TEMP FROM PROBE

Now that that is working, we need to add a line to our snmpd.conf file to join that script with that OID. So... Edit the snmpd.conf:

sudo nano /etc/snmp/snmpd.conf

and add this line down where the other "PASS" lines are:

pass .1.3.6.1.2.1.25.1.8.0      /bin/sh         /usr/local/bin/snmp-temp -g

Again, please remember that I moved files around to that /usr/local/bin directory.

So.... Now if we run this (either locally or from the remote machine - ill use localhost for this example):

snmpget -v 1 -c public localhost .1.3.6.1.2.1.25.1.8.0

it should return:

iso.3.6.1.2.1.25.1.8.0 = Gauge32: CURRENT TEMP FROM PROBE

So for understanding, this is what happens:
  1. snmpget goes out via version 1 looking for the public community on localhost for the OID labeled .1.3.6.1.2.1.25.1.8.0
  2. Once it finds that OID in the snmpd.conf, it runs the associated script called snmp-temp inside /usr/local/bin
  3. That script calls the python script that actually does all the work; grabbing the output of the probe, parsing all the gibberish and converting it to Fahrenheit.
  4. snmp-get returns the results of the temp.py into a nicely formatted snmp query that can then be put into any program.
I use nagios to poll a ton of other devices so this was an easy setup inside nagios once the output was generated and the ability to query against it was done.

Friday, March 1, 2013

SNMP polling temp as Celsius and changing it to Fahrenheit nagios/opsview

A little while ago, I created a Opsview install that monitors all aspects of the network from temp/humidty/air flow readings off our WeatherGeese devices to APC loads and charges to server uptime and service states. This turned out quite nicely. I used a tutorial over at Spiceworks.com to get it started and been hacking it ever since.

My latest issue with it is that some of our environmental devices are either too old, coded wrong or have the wrong temp sensors in them to report Fahrenheit temps. they are only in Celsius. Now, even tho nagios does a good job at reporting red/yellow/green indicators (using the rawNumbers gadget), it is still in Celsius.

I sent a few pings out to my nerd community and they answered. Here is the entire post of what followed at this link:

http://community.spiceworks.com/topic/301466-opsview-check_snmp-c-to-f

Long story short, here is the complete code that you can use and adjust to your liking. It took quite a bit of hacking apart and I appreciate George1421's assistance. I do not know how to code. I'm only VERY dangerous using multiple languages. I think we got through it! :)

https://dl.dropbox.com/u/2210016/check_weathergoose.pl.txt

This will allow you to poll the device using the syntax that is allowable (its defined in the help or at the bottom of the code). I added the functionality of the OID in the parameters so its not hard coded in anymore.

Tuesday, July 10, 2012

Barracuda Web Filter 310 New enhancements?

Apparently, and I'm not sure when this went into effect, but there is an area under Block/Exempt-Applications that allows/disallows certain programs and/or protocols.

Well, a few months ago, email started to not flow in so in troubleshooting, I found that the web filter was causing this issue so I took it out of the chain and everything seemed to flow again. I left it out for a few weeks and tried to put it back in, thinking it was a cache issue or something bogging the filter down and with a couple weeks out of commission, we would be ok. Negative.

I had a chance to turn it on this past weekend and call barracuda if everything wasnt ok, so I did. And I called. Well the tech said he has seen this before so.....

By selecting the UnAuthenticated dropdown in the top right, I was able to see that all protocols were being blocked when they were unauthenticated, which email and all my SNMP traps were. I allowed all protocols while being UnAuthenticated and we are back in business.

Yeah. Wow.

Monday, January 23, 2012

AppleTV 2 and XBMC

I recently upgraded to an AppleTV from my Western Digital Live HD device to serve my movies. Granted i had no intention of doing anything with iTunes. I only wanted the device because it is practically a turn-key solution to a newer streaming device. XBMC allows for better movie info and just a much better user interface than the Western Digital device. Dont get me wrong, the WD device kicks butt. Its just that its not pretty! :) (note: i have never had a problem with this device. It plays everything that I need it to.)

So for $99 at Best Buy (yeah i know. I had a gift card so its ok), I bought one and jailbroke it to get XBMC on it. Here are the steps:

1. Jailbreak it: http://forum.xbmc.org/showthread.php?t=99315

2. Put XBMC on it: http://wiki.xbmc.org/index.php?title=HOW-TO:Install_XBMC_on_Apple_TV_2

3. ????

4. Profit

I also used these two programs before even turning on the ATV2:

TheRenamer: http://www.therenamer.com
Media Companion: http://mediacompanion.codeplex.com/

TheRenamer is an EXCELLENT tool that will take all your movies, move them into their own folders, check them with IMDB.com and post a year at the end of that folder name and get them ready for a VERY quick scraping of all movie content.

Media Companion will import all those movies into its own database and then go out to IMDB.com and parse the movies page (that was correctly identified using TheRenamer) and bring down that movies info; actors names, plot, posters, fan art, etc.

After doing both of those, launch into XBMC and add a folder and make the scraper IMDB. It will then go through your local share for info that you have just downloaded with Media Companion.

Granted, this took me the better part of last week and the weekend only because my library wasnt ready for this drastic move but I can assure you that my work was not lost. This device is doing exactly what I want it to do.

Wednesday, July 6, 2011

Google Plus profile transfer

Sooo.... I was lucky enough to get an invite to Google+ from a buddy of mine and I am now in the process of moving everything over. Pictures turned out to be a toughy because I didnt have them saved on my desktop. Uploaded them one at a time over time.

I did, however, find this neat little Chrome Extension:

http://omgeureka.blogspot.com/2011/04/download-facebook-photo-albums.html

and it worked like a charm. Niiiice!!!

Monday, July 19, 2010

XBox 360 Modded Controller

Well, I cracked open an XBox 360 controller this weekend and installed a modchip for a neighbor. It was relatively easy. Nothing really to report. It was an 8 mode chip purchased off ebay for under 20 bux shipped. 6 wires. power, ground, led hookup (to see the blinks for what mode you are in), the mode button and the two triggers. A little soldering and it was firing on all cylinders. Rapidly.

Only thing was the placement of the chip and the mode button. i had to cut a little of the case in order for the controller board to fit back in (nothing was cut on outside of case, jut a little plastic on the inside) and then bend the masts of the mode button down but it seems to be working just fine. Took about 20 minutes.

Wednesday, July 7, 2010

SUA1500RM2U clicking

I bought a rack mountable APC unit for all my machines to plug into. Its been very nice. Saved me a bunch of times. I have a LOT of stuff plugged into it but it still has barely a load registering.

Since i have had my AC on, it has been doing a lot of clicking, going back and forth between On Battery and On Line. the past week, it was doing it so much that it drained the batteries and then subsequently shut off which then shut everything plugged into it off. Not good. So, after some troubleshooting with a buddy, we came to the conclusion that it wasnt getting enough juice off the circuit. We believe the amps were dropping and the devices i have plugged into it, even tho they were shoing a major load, they were at the threshold of amps that that thing can sustain.

We are going to run a dedicated circuit to it and ill post results.