Thursday, January 17, 2013

Auto-reboot a hung Raspberry Pi using the on-board watchdog timer

A watchdog timer is a special kind of timer commonly found on embedded systems that is used to detect when the running software is hung up on some task. The watchdog timer is basically a countdown timer that counts from some initial value down to zero. When zero is reached, the watchdog timer understands that the system is hung up and resets it. Therefore, the running software must periodically update the watchdog timer with a new value to stop it from reaching zero and causing a reset. When the running software is locked up doing a certain task and cannot update the watchdog timer, the timer will inevitably reach zero and a reset will occur.

Luckily for us, the Broadcom BCM2835 SoC on the Raspberry Pi comes with a hardware-based watchdog timer that can do just that. You will find this specially useful if you have a Raspberry Pi in a remote location and the operating system hangs and there's no one around to reboot it.

Load the bcm2708_wdog kernel module

To load the watchdog kernel module right now, issue the following command:
$ sudo modprobe bcm2708_wdog

If you are running Raspbian, to load the module the next time the system boots, add a line to your /etc/modules file with "bcm2708_wdog". The -a option makes sure tee appends instead.
$ echo "bcm2708_wdog" | sudo tee -a /etc/modules


If you are running Arch Linux, add a file called "bcm2708_wdog.conf" with the text "bcm2708_wdog" in /etc/modules-load.d/ with the following command:
$ echo "bcm2708_wdog" | sudo tee /etc/modules-load.d/bcm2708_wdog.conf

Install the software watchdog daemon

In Raspbian, run the following command:
$ sudo apt-get install watchdog
But in Arch we use pacman:
$ sudo pacman -S watchdog

Then, make sure it runs after every boot.
In Raspbian, run:
$ sudo update-rc.d watchdog defaults
OR
$ sudo chkconfig --add watchdog
In Arch run the following:
$ sudo systemctl enable watchdog

Configure the watchdog daemon

Open /etc/watchdog.conf with your favorite editor (mine is nano).
$ sudo nano /etc/watchdog.conf

Uncomment the line that starts with #watchdog-device by removing the hash (#) to enable the watchdog daemon to use the watchdog device.
Uncomment the line that says #max-load-1 = 24 by removing the hash symbol to reboot the device if the load goes over 24 over 1 minute. A load of 25 of one minute means that you would have needed 25 Raspberry Pis to complete that task in 1 minute. You may tweak this value to your liking.

Start the watchdog daemon

In Raspbian:
$ sudo chkconfig watchdog on
or
$ sudo /etc/init.d/watchdog start

In Arch:
$ sudo systemctl start watchdog.service

That's it!

You are done! You may play around with the settings in /etc/watchdog.conf if you'd like.
The watchdog daemon performs other tests that you will probably want to configure.

Arch Linux users: I'm well aware that the watchdog daemon is not necessary in Arch because you can enable watchdog features with systemd by editing /etc/systemd/system.conf but I prefer the watchdog daemon as it is much more featured.

Updates:
2013-01-17 11:45 AM EST: Fixed the chkconfig command to start and enable the watchdog daemon at startup.

Source: Raspberry Pi @ Gadgetoid

Monday, January 14, 2013

Pushing Raspberry Pi Data to Pachube using bash and cron



Pachube
The Pachube API makes it very easy for you to make your Raspberry Pi part of the Internet of Things (IoT). After successfully following this short tutorial, you or any computer connected to the Internet will be able to keep track of how your Raspberry Pi is doing. Even more useful if you have more than one and wish to track them all at once.
This blog post is the result of a brief tutorial I made in Google+'s Raspberry Pi community for +Kim Huebel.


Requirements

Raspberry Pi
  1. Raspberry Pi connected to the Internet.
  2. Debian-flavor "ifstat command". Run "ifstat -v"; You should have version 1.1.
  3. realpath command for setup.sh. Run "realpath --version" to see if you have it installed. Use apt-get or pacman to install it depending on which linux distro you are using.
Arch Linux users: You WILL have to download the source code for ifstat and compile it yourself if you want to monitor a network interface with this script. Arch Linux's ifstat command output format is significantly different from Debian's ifstat. You will otherwise have to modify rpi2pachube to work with whatever flavor of ifstat Arch Linux uses if you do not wish to do this.

Create a feed on Pachube

  1. Create an account on Pachube if you don't have one already and log in with your account.
  2. Add a new device/feed by clicking on the +Device/Feed button.
    1. Select "Something else" when prompted for device type.
    2. Select "No, I will push data to Cosm" when asked for an existing data source.
    3. Give your device/feed a title. (e.g. raspberry-pi-1)
    4. Optional: Add tags to your new device. (e.g., raspberry pi, network, performance)
    5. Click Create.
    6. Write down the feed ID, as you will need this later for configuring the script.
  3. Click "Keys" in your console and then click the +Key button to add a new key.
    1. Label your key. (e.g. raspberry-pi-update)
    2. Set feed restriction to "Use specific feed(s)" and select the Device/feed you created earlier.
    3. Only "update" is needed in Access Privileges.
    4. Click Create.
Github

Download rpi2pachube from Github

  1. Make a directory where you want rpi2pachube folder to be located. (e.g. ~/scripts).
    • mkdir -p ~/scripts
  2. CD into that directory: (e.g. cd ~/scripts/)
    • cd ~/scripts
  3. Clone the git repository.
    • git clone git@github.com:Cameri/rpi2pachube.git
  4. Run the configuration utility.
    • cd rpi2pachube
    • ./setup.sh
  5. When prompted, answer with your Pachube API Key, Feed ID and a Network Interface to monitor.
The configuration utility creates the file ~/.rpi2pachube.conf where your configuration is stored.
It is also possible to manually create a global configuration file in /etc/rpi2pachube.conf but that is unnecessary and not recommended.
Run the configuration utility (setup.sh) instead of manually editing ~/.rpi2pachube.conf.
Your Raspberry Pi performance values will be pushed to Pachube every 5 minutes. To change it, edit your user's crontab file (e.g. crontab -e).

That is all! Feel free to leave a comment.

Links:
Official Google+ Post in the Raspberry Pi community.

Update (Wed, Jan 23rd, 2013): Added "realpath" as a requirement. Renamed script to rpi2pachube and moved repository to a new location.

Legal disclaimer: All mentions of the letter sequence "cosm" on this post or Github repository (or in combination with other letters, or any other representation) refer ONLY to the on-line database service called Pachube and NOT to Cosm®, a trademark that is registered to Mithral Inc.