Jun 082015

In the previous post, I got the Raspberry Pi outputting the values from the temperature sensors via SNMP.

In this post, I’ll be configuring Zabbix to monitor and record these values so that I can see how cold it was last night.
You’ll need to note down the OIDs that were configured on the SNMP agent in the last post so you can use them in this post.

Monitoring the Pi

Adding it in

The Raspberry Pi will need to be monitored in some way by Zabbix in order for us to record the temperatures from the SNMP agent that we’ve set up.
You’ll need to add the Raspberry Pi as a host in Zabbix, and give it an IP address under the SNMP interfaces section. I’ve added the SNMP OS Linux template to monitor the rest of the Pi stats, but that’s not required for monitoring temperatures.

Continue reading »

Jun 042015

So I got a bit sidetracked from my tear down of the Ritmo CE-3590 NAS, and ended up building a temperature sensor with some DS18B20s and a Raspberry Pi.

Today’s post will detail how I configured the Pi to output the temperature values to the SNMP agent that I’m running on the Pi.

Plugging The Sensors In

Since I used the DS18B20s, they are polled over a single wire with a 4K7 resistor across the VCC and Data wires.
I ended up using a whole bunch of CD Audio cables like this one –
CD Audio Cable
to create the wiring for the sensors. If I had the pins to use between the cables, I could have just joined cables together, but as I didn’t, I cut and soldered multiple cables together. At the end of the wires that will plug into the Pi, I spliced in the 4K7 resistor and heatshrinked it so hopefully it won’t short anything out.
Temp Sensor Wires
It ended up very messy, but it seems to have worked.

I used the Red wire for 3.3V, the white wire as data, and the black as ground.
Red then gets plugged into the Pi header at Pin 1 which is 3.3V power
White gets plugged into Pin 7 which is GPIO 4
Black gets plugged into Pin 6, which is a ground. This could be any of the other grounds, but this is the closest one.

Configuring the Pi

I’m using Raspbian on my Pi, so any instructions are done with a Raspbian flavour. Most distros should have the same setup though.
The Pi needs to be configured to use the w1-gpio device tree overlay (dtoverlay).
This is done by adding the line dtoverlay=w1-gpio to the end of the config.txt file on the /boot partition.
Once this dtoverlay has been activated, you should be able to see your sensors under /sys/devices/w1_bus_master1/. They should appear as 28-0215**. On my Pi, the sensors appear like so –


Catting one of the w1_slave nodes under the sensor should output something like this –

57 01 55 00 7f ff 0c 10 05 : crc=05 YES
57 01 55 00 7f ff 0c 10 05 t=21437

The YES confirms the sensor is working, and the temperature is displayed after the t=
In this example, the temperature is 21.437 Degrees Celsius. The output needs to be divided by 1000 to get the temperature to the correct amount of decimal places.

Configuring the SNMP agent

With the sensors plugged in, it’s time to extend the SNMP agent to allow us to poll the temperature sensors over SNMP.
I’ve written a script that will output just the temperature to the SNMP agent, I’ve placed this script in /opt/scripts but you’re free to put it wherever you want. You’ll need to keep the location handy for when we extend the SNMP agent.
I’ve named this script gettemp.sh

# Usage: gettemp.sh
# e.g. gettemp.sh 28-0215012018ff
OUTPUT=$(/bin/cat $SLAVE | /usr/bin/awk -F 't=' ' { printf $2 } ')
echo $OUTPUT

With this script, you should be able to call the script from a command line, and it should output the temperature for that sensor.

# /opt/scripts/gettemp.sh 28-021500ce50ff

With this script operational, we’ll need to setup the SNMP agent to run this script to provide us the output of this script over SNMP.
To do this, we need to modify /etc/snmp/snmpd.conf. At the end of the file, put in this line –
Make sure you replace the sensor id with your own sensor id. If you want to monitor more than one sensor, add more extend lines, and give them differing names, like temp2, temp3, etc.

extend . temp1 /bin/bash /opt/scripts/gettemp.sh 28-021500ce50ff

After you modify the snmpd.conf file, make sure you restart SNMP agent with /etc/init.d/snmpd restart or service snmpd restart.

Once the SNMP agent is back up and running, you should be able to poll the OID we specified above (. and get the output of the command. I’ve run the snmpwalk on the Pi itself, polling itself.

# snmpwalk -v 2c -c public .
iso. = INTEGER: 1
iso. = STRING: "/bin/bash"
iso. = STRING: "/opt/scripts/gettemp.sh 28-021500ce50ff"
iso. = ""
iso. = INTEGER: 5
iso. = INTEGER: 1
iso. = INTEGER: 1
iso. = INTEGER: 4
iso. = INTEGER: 1
iso. = STRING: "21437"
iso. = STRING: "21437"
iso. = INTEGER: 1
iso. = INTEGER: 0
iso. = STRING: "21437"

As you can see from the example, the temperature is output in a couple of different places.
Polling the SNMP agent with one of those locations directly should yield only the temperature.

# snmpwalk -v 2c -c public .
iso. = STRING: "21437"

With that, we now have SNMP set up on the Pi, ready to be polled for the temperature.

Mar 042013

I have now built a script to do this automatically.
The script will minimise the size as much as possible.
Script can be found here

I recently had to resize a Raspbian Server Edition image into a 1 gigabyte image. As I didn’t actually have a 1 gigabyte SD card to use as a template, I generated my own by resizing the original 2 gigabyte image.

This was done on another computer, but this can also be done on the Pi itself, and you can even flash the resulting image to an SD card if you have a USB SD Card reader on the Pi.

Also, most commands will need to be run as root, or use sudo to run the commands.

Step 1 : Mounting the image

First thing I did was make a copy of the 2gb image to work on, this is not essential if you downloaded the image originally, but I’m working on the original RSE image.

# cp RSEv2.3.img RSE1g.img

Similar to part 1, we need to mount the image via loopback.

# losetup -f --show RSE1g.img

This will mount the image file as a loopback device, and show you which one it was mounted as. By default, it should be /dev/loop0

Step 2: Resizing the Partition

Once the image is mounted, we then need to run parted on the image file, and get it to print out the current partitions.

# parted /dev/loop0
GNU Parted 2.3
Using /dev/loop0
Welcome to GNU Parted! Type 'help' to view a list of commands.

Running the print command will output the current setup.

(parted) print
Model: Loopback device (loop)
Disk /dev/loop0: 2003MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 62.9MB 58.7MB primary fat16 lba
2 62.9MB 1940MB 1877MB primary ext4


As you can see, the 2nd partition, which is the linux partition, is 1877 megabytes. To resize the partition, we need to remove it, and recreate it with a new size.

Running the command rm 2 will remove the 2nd partition.

(parted) rm 2

To recreate it, we need to use mkpart command, and specify that we will be creating it as a primary partition, starts at 62.9MB, and ends at 900MB to give us a partition of 837 MB.

(parted) mkpart primary 62.9 900

Once the partition has been created, running print again will show us the new setup.

(parted) print
Model: Loopback device (loop)
Disk /dev/loop0: 2003MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 62.9MB 58.7MB primary fat16 lba
2 62.9MB 900MB 837MB primary ext4

We will also need to get the amount of sectors in the new partition so we can resize the filesystem to the right size later. To do that, we change the units to sectors, and then run the print command again

(parted) unit s

Now when we run the print command, the partitions will show up with sectors as the units instead

(parted) print
Model: Loopback device (loop)
Disk /dev/loop0: 3911680s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 8192s 122879s 114688s primary fat16 lba
2 122880s 1757183s 1634304s primary ext4

We’ll need to take a note of the size of the number 2 partition (1634304 sectors) and the sector size (512 bytes), and also the start of the partition (sector 122880).

Step 3: Resizing the Filesystem

Once the partition has been resized, we will then need to resize the filesystem that resides in the partition.

We need to calculate where the partition starts so we can mount the specific partition rather than the whole image. To calculate it, we take the starting sector, and multiply it by the sector size.

I’m using bc to do the calculation here

# echo '122880 * 512' | bc

So to mount the partition itself, we need to mount the image with an offset. This should mount it on /dev/loop1

# losetup -f --show -o 62914560 RSE1g.img

Before we can resize the filesystem, we need to check it for errors first. We’ll need to force it as the filesystem is meant to be clean.

# e2fsck -f /dev/loop1
e2fsck 1.42.5 (29-Jul-2012)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop1: 28754/114688 files (0.1% non-contiguous), 153181/458240 blocks

Once the filesystem has been verified as clean, we can then use resize2fs to resize the filesystem.
We will need to calculate the size of the new filesystem to resize it correctly. To do that, we need to take the size of the partition in sectors (1634304), and divide by 8 (4KB blocks divided by 512 bytes/sector), assuming we are using 4KB blocks in the filesystem.

# echo '1634304 / 8' | bc

We can use resize2fs now to resize the filesystem to the correct size.

# resize2fs /dev/loop1 204288
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/loop1 to 204288 (4k) blocks.
The filesystem on /dev/loop1 is now 204288 blocks long.

Once the resize is done, we can now remove all loopback devices

# losetup -d /dev/loop0 /dev/loop1

Step 4: Resize the image file

Once the resize is all completed, we just need to lop off the end of the image file that has all the free space.
Since the 2nd partition ends at the 900MB mark, we can lop it off right there with the truncate command

# truncate -s 900M RSE1g.img

The filesize will now be 900 Megabytes which is small enough to fit onto any 1GB SD Card.