Setting up the raspberry pi watchdog

From Domoticz
Revision as of 15:15, 29 March 2019 by Buitje (talk | contribs) (update mutt settings for gmail)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

This guide describes how to install the hardware watchdog, a system that knows when your raspberry is down, and will automatically reboot it in case this happens.

This guide will also work for the cubieboard/truck, if you are using the firmware mentioned on this wiki,
the watchdog is already installed and you can continue to Install the software watchdog daemon

Install watchdog

sudo modprobe bcm2835_wdt
echo "bcm2835_wdt" | sudo tee -a /etc/modules

Install the software watchdog daemon

sudo apt-get install watchdog
sudo update-rc.d watchdog defaults

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.

In some cases you should also add the following line to allow watchdog even to start-up:

watchdog-timeout = 15

The timeout defaults to 60 seconds, but this results sometimes in not starting up at all (probably a watchdog bug). Do not set this value lower than 15.


If you only want to setup the watchdog to restart in case your system hangs, continue to the section Start the watchdog daemon

Use watchdog to check Domoticz

This script relies on the fact that the Domoticz.log is written in /tmp/ newer installs have this option disabled by default. To make this script work you must enable the logfile.

sudo nano /etc/init.d/domoticz.sh

and change the line OPTIONS="-www 8080" to:

OPTIONS="-www 8080 -loglevel=normal -log /tmp/domoticz.log"

(* Alternative ) In my case, the option must be set up like this

DAEMON_ARGS="$DAEMON_ARGS -www 8080 -loglevel=normal"
DAEMON_ARGS="$DAEMON_ARGS -sslwww 443"
DAEMON_ARGS="$DAEMON_ARGS -log /tmp/domoticz.log"


Setup mailer:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install sendmail-bin
sudo apt-get install sensible-mda
sudo apt-get install mutt

Then continue to:

cd /home/pi
create the file .muttrc with the following content: (vi .muttrc or nano .muttrc)
# basic .muttrc for use with Gmail
# Change the following six lines to match your Gmail account details
set imap_user = "[email protected]"
set imap_pass = ""
set smtp_url = "smtps://[email protected]@smtp.gmail.com:465/"
set smtp_pass = ""
set from = "[email protected]"
set realname = "Firstname Lastname"
set use_from = yes
set ssl_force_tls = yes

#
# # Change the following line to a different editor you prefer.
set editor = 'vim + -c "set textwidth=72" -c "set wrap"'
# Basic config
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set imap_check_subscribed=yes
set hostname = gmail.com
set mail_check = 120
set timeout = 300
set imap_keepalive = 300
set postponed = "+[GMail]/Drafts"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set include
set sort = 'threads'
set sort_aux = 'reverse-last-date-received'
set auto_tag = yes
set pager_index_lines = 10
ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc
alternative_order text/plain text/html *
auto_view text/html
bind editor <Tab> complete-query
bind editor ^T complete
bind editor <space> noop
# # Gmail-style keyboard shortcuts
macro index,pager am "<enter-command>unset trash\n <delete-message>" "Gmail archive message" # different from Gmail, but wanted to keep "y" to show folders.
macro index,pager d "<enter-command>set trash=\"imaps://imap.googlemail.com/[GMail]/Bin\"\n <delete-message>" "Gmail delete message"
macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"
macro index,pager gl "<change-folder>?" "Go to 'Label'" # will take you to a list of all your Labels (similar to viewing folders).

edit the first lines to setup your gmail account info now create an app key for the gmail account to be used as the password

Go to your Google Account.
On the left navigation panel, click Security.
On the "Signing in to Google" panel, click App passwords. If prompted, enter your password.
Note: If you can't get to the page, 2-Step Verification is: Not set up for your account or Set up for security keys only
At the bottom, click Select app and choose the app you’re using.
Click Select device and choose the device you’re using.
Click Generate.
Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
Click Done.

Once you are finished, you won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.

Create a mail script in /home/pi:

cd /home/pi/
sudo nano mailIP

Paste the following (repalce YOURMAIL with full email address of where you want the email sent i.e. [email protected]):

 #!/bin/sh
 mailreceiver=YOURMAIL
 today=$(date)
 #my_ip=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'`
 my_ip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
 my_pi="Domoticz RaspberryPi has rebooted! "
 message="Your Pi has rebooted at $today. Current IP address = $my_ip"
 echo $message > message.txt
 mutt -s "${my_pi}" ${mailreceiver} < message.txt && rm message.txt

The commented line fetches the internal ip, example fetches external ip.

Make sure the mailIP can be executed:

sudo chmod +x mailIP

Test the script by running it:

./mailIP

Setup rc.local to send the mail at each reboot: Edit /etc/rc.local:

sudo nano /etc/rc.local

Add this above the exit 0 line:

sudo /home/pi/mailIP & 

Make sure the rc.local can be executed:

sudo chmod 0755 /etc/rc.local
sudo bash
cp /home/pi/.muttrc /root



sudo nano /etc/watchdog.conf

Uncomment the lines file and change and set them to:

file                    = /tmp/domoticz.log
change                  = 300

This setting will restart the pi when domoticz.log hasn't been updated in 5 minutes, the time is in seconds (tested).

Be sure to remember this when stopping the domoticz service by hand!

and for new system , that do nothing yet, you can prevent unwanted reboot by adding a script like this :

cd /home/pi/domoticz/scripts/lua
nano script_time_domotizAlive.lua
commandArray = {}
print('Domoticz alive !')
return commandArray

It will update the log every minute.

Start the watchdog daemon

sudo service watchdog start

Source: [1] Source: [2]