2016年2月19日 星期五

Raspberry Pi: Setting Up The Watchdog

since: 2016/02/19
update: 2016/02/19

reference:
1. 樹莓派硬體看門狗(Watchdog):當機時自動重新開機 - G. T. Wang
2. ssmtp to send emails – Raspberry Pi Projects


A. Sending Email From Your System with sSMTP
    1. 安裝套件:
        $ sudo apt-get install ssmtp
        $ sudo apt-get install mailutils

    2. 編輯設定檔:
        $ sudo nano /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=lanlixxxx@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=raspberrypi

# Auth
AuthUser=pythonicxxxx@gmail.com
AuthPass=pythonxxxx


# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES


    3. 降低寄發郵件的安全性設定: (AuthUser)
        > GMail 低安全性應用程式 - 帳戶設定

    4. 寄發測試郵件:
        $ echo "Hello world email body" | mail -s "Test Subject" lanlixxxx@gmail.com

    5. 寄發 "附加檔案" 測試郵件:
        $ sudo apt-get install mpack
        $ mpack -s "Test" somefile.txt lanlixxxx@gmail.com

-----------------------------------------------------------------------------------------------

B. 安裝 watchdog

    1. 啟用 bcm2708_wdog 核心模組
        $ sudo nano /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.

i2c-dev

bcm2708_wdog

    2. 立即啟用:
        $ sudo modprobe bcm2708_wdog

    3. 安裝 watchdog 監控 Daemon
        $ sudo apt-get install watchdog

-----------------------------------------------------------------------------------------------

C. 編輯 watchdog 設定檔
    $ sudo nano /etc/watchdog.conf
#ping                   = 172.31.14.1
#ping                   = 172.26.1.255
#interface              = eth0
#interface              = wlan0
# 監控檔案是否可以正常存取

#file                   = /var/log/messages
#change                 = 1407

# Uncomment to enable test. Setting one of these values to '0' disables it.
# These values will hopefully never reboot your machine during normal use
# (if your machine is really hung, the loadavg will go much higher than 25)
max-load-1              = 24
max-load-5              = 18
max-load-15             = 12


# Note that this is the number of pages!
# To get the real size, check how large the pagesize is on your machine.
# 查看自己系統的 page size
# getconf PAGESIZE (4096)

#
#min-memory             = 1 (一張 page 的大小就是 4096 bytes)
#allocatable-memory     = 1

#repair-binary          = /usr/sbin/repair
#repair-timeout         =
#test-binary            =
#test-timeout           =

watchdog-device = /dev/watchdog

# Defaults compiled into the binary

// 檢查 CPU 溫度  (單位: 攝氏千分之一度)
// cat /sys/class/thermal/thermal_zone0/temp     (35780)
#temperature-device     =  /sys/class/thermal/thermal_zone0/temp
#max-temperature        = 120

#max-temperature        = 80000
# Defaults compiled into the binary
#admin                  = root
admin                   = lanlixxxx@gmail.com
#interval               = 1
#logtick                = 1
#log-dir                = /var/log/watchdog

# This greatly decreases the chance that watchdog won't be scheduled before
# your machine is really loaded
realtime                = yes
priority                = 1

# Check if rsyslogd is still running by enabling the following line
#pidfile                = /var/run/rsyslogd.pid

-----------------------------------------------------------------------------------------------

D. 調整: (for 新版 Pi OS: 2016-02-09-raspbian-jessie; still may not work)
     $ sudo nano /lib/systemd/system/watchdog.service
[Unit]
Description=watchdog daemon
Conflicts=wd_keepalive.service
After=multi-user.target
OnFailure=wd_keepalive.service

[Service]
Type=forking
EnvironmentFile=/etc/default/watchdog
ExecStartPre=/bin/sh -c '[ -z "${watchdog_module}" ] || [ "${watchdog_module}" = "none" ] || /sbin/modprobe $watchdog_module
ExecStart=/bin/sh -c '[ $run_watchdog != 1 ] || exec /usr/sbin/watchdog $watchdog_options'
ExecStopPost=/bin/sh -c '[ $run_wd_keepalive != 1 ] || false'

[Install]

WantedBy=multi-user.target

$ systemctl enable watchdog

-----------------------------------------------------------------------------------------------

E. 啟動看門狗
    1. 立即啟動 watchdog 常駐程式
        $ sudo service watchdog start

    2. 開機時自動啟動 watchdog
        $ sudo update-rc.d watchdog defaults

    3. 檢查
        $ ps aux | grep watchdog
root       786  0.0  0.3   1888  1760 ?        SLs  14:34   0:00 /usr/sbin/watchdog
pi         822  0.0  0.3   4276  1956 pts/0    S+   14:36   0:00 grep --color=auto watchdog


    4. 測試       
        kill 掉 watchdog 本身, 會重新開機
        $ sudo kill -9 786