本次將利用QNAP上安裝並執行DOCKER後,利用ubuntu增加NUT服務,並在IFTTT上建立applet觸發TELEGRAM告警。
NUT部分安裝在Docker上面,Docker相關設定請自行google或是參考本篇,我安裝的是ubuntu 18.04
安裝NUT
apt-get install nut
設定
編輯nut.conf
vi /etc/nut/nut.conf
修改mode為netclient
# Since this MODE is opened to the network, a special care should be applied # to security concerns. # - netclient: this mode only requires upsmon. # # IMPORTANT NOTE: # This file is intended to be sourced by shell scripts. # You MUST NOT use spaces around the equal sign! #MODE=none MODE=netclient
編輯upsmon.conf
vi /etc/nut/upsmon.conf
修改以下資訊,以便連結QNAP的UPS。
...以上略 # password = blah # upsmon master (or slave) # # "master" means this system will shutdown last, allowing the slaves # time to shutdown first. # # "slave" means this system shuts down immediately when power goes critical. # # Examples: # # MONITOR myups@bigserver 1 monmaster blah master # MONITOR su700@server.example.com 1 upsmon secretpass slave # MONITOR myups@localhost 1 upsmon pass master (or slave) MONITOR qnapups@192.168.1.1 1 admin 123456 slave # -------------------------------------------------------------------------- # MINSUPPLIES <num> # # Give the number of power supplies that must be receiving power to keep # this system running. Most systems have one power supply, so you would # put "1" in this field. # # Large/expensive server type systems usually have more, and can run with # a few missing. The HP NetServer LH4 can run with 2 out of 4, for example, # so you'd set that to 2. The idea is to keep the box running as long # as possible, right? ....以下略
可以試著啟動看看
service nut-client restart
如果出現Communications with UPS qnapups@192.168.1.1 lost 或是 UPS qnapups@192.168.1.1 is unavailable 則表示你沒有把IP設定在QNAP的UPS通知列表內。
設定成功會出現以下訊息
Broadcast message from user (somewhere) (Fri Apr 10 21:33:33 20 Communications with UPS qnapups@192.168.1.1 established
可以使用 upsc 來擷取UPS的訊息
upsc qnapups@192.168.1.1
Init SSL without certificate database battery.charge: 100 battery.charge.low: 10 battery.charge.warning: 20 battery.mfr.date: CPS battery.runtime: 1110 battery.runtime.low: 300 battery.type: PbAcid battery.voltage: 24.0 battery.voltage.nominal: 24 device.mfr: CPS device.model: CP1000PFCLCD device.serial: 000000000000 device.type: ups driver.name: usbhid-ups driver.parameter.pollfreq: 30 driver.parameter.pollinterval: 2 driver.parameter.port: /dev/ttyS1 driver.parameter.synchronous: no driver.version: 2.7.4 driver.version.data: CyberPower HID 0.4 driver.version.internal: 0.41 input.transfer.high: 139 input.transfer.low: 88 input.voltage: 111.0 input.voltage.nominal: 120 output.voltage: 136.0 ups.beeper.status: enabled ups.delay.shutdown: 20 ups.delay.start: 30 ups.load: 32 ups.mfr: CPS ups.model: CP1000PFCLCD ups.productid: 0501 ups.realpower.nominal: 600 ups.serial: 000000000000 ups.status: OL ups.test.result: No test initiated ups.timer.shutdown: -60 ups.timer.start: -60 ups.vendorid: 0764
接下來設定upsmon.conf
vi /etc/nut/upsmon.conf
...前略 # RUN_AS_USER <userid> RUN_AS_USER nut ...中略... # NOTIFYCMD <command> NOTIFYCMD /sbin/upssched ...中略... # NOTIFYFLAG - change behavior of upsmon when NOTIFY events occur NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC NOTIFYFLAG FSD SYSLOG+WALL+EXEC # NOTIFYFLAG COMMOK SYSLOG+WALL # NOTIFYFLAG COMMBAD SYSLOG+WALL NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC # NOTIFYFLAG NOCOMM SYSLOG+WALL # NOTIFYFLAG NOPARENT SYSLOG+WALL ...後略
與 upssched.conf
vi /etc/nut/upssched.conf
...前略 # CMDSCRIPT <scriptname> CMDSCRIPT /var/ups/upsifttt.sh ...中略... # PIPEFN <filename> PIPEFN /var/run/nut/upssched/upssched.pipe ...中略... # LOCKFN <filename> LOCKFN /var/run/nut/upssched/upssched.lock ...中略... # - EXECUTE <command> AT ONLINE * EXECUTE ups-back-on-line AT ONBATT * EXECUTE ups-on-batt AT LOWBATT * EXECUTE ups-lo-batt AT SHUTDOWN * EXECUTE ups-shutdown AT REPLBATT * EXECUTE ups-batt-fail AT FSD * EXECUTE ups-fsd
在EXECUTE的command可以自由命名。
IFTTT
申請IFTTT webhook applet ( if Webhook then IFTTT )
建立新的applet
選Webhooks,並設定Event Name,我設定為NUT。
再選一個Telegram action,並設定action的Message text。
其中的參數可以自行搭配使用,因為需要的不多,我只用了OccurredAt與Value1。
整體是這樣
然後你的Telegram必須加入@IFTTT這個機器人
完成後回到ubuntu執行命令,複製範例到CMDSCRIPT所定位置
rsync -a /bin/upssched-cmd /var/ups/upsifttt.sh
編輯CMDSCRIPT
vi /var/ups/upsifttt.sh
#! /bin/sh # # This script should be called by upssched via the CMDSCRIPT directive. # # Here is a quick example to show how to handle a bunch of possible # timer names with the help of the case structure. # # This script may be replaced with another program without harm. # # The first argument passed to your CMDSCRIPT is the name of the timer # from your AT lines. event_name='你的webhooks event name' webhook_key='你的webhooks key' msg='' case $1 in upsgone) logger -t upssched-cmd "The UPS has been gone for awhile" ;; ups-back-on-line) msg="好消息,電來了!" ;; ups-on-batt) msg="不好了,停電啦!!!!" ;; ups-lo-batt) msg="UPS電量低!" ;; ups-shutdown) msg="UPS即將關機" ;; ups-batt-fail) msg="UPS電池故障" ;; ups-fsd) msg="收到來自NAS的強制關機訊息" ;; test) msg="測試訊息用" ;; *) logger -t upssched-cmd "Unrecognized command: $1" ;; esac if [ "$msg" != "" ] then curl -l --data-urlencode "value1=${msg}" "https://maker.ifttt.com/trigger/${event_name}/with/key/${webhook_key}" else echo "NOMESSAGE \r" fi
完成後將upsifttt.sh +x
chmod a+x /var/ups/upsifttt.sh
測試,出現Congratulations! You’ve fired the NUT 就成功了
/var/ups/upsifttt.sh test Congratulations! You've fired the NUT event
重啟服務
service nut-client restart * Restarting NUT - power device monitor and shutdown controller nut-client [ OK ]
使用upsmon -c fsd驗證
upsmon -c fsd Network UPS Tools upsmon 2.7.4 Broadcast message from (somewhere) (Sat Apr 11 13:25:12 20 Executing automatic power-fail shutdown Broadcast message from (somewhere) (Sat Apr 11 13:25:12 20 Auto logout and shutdown proceeding
log如下
Apr 11 13:25:12 b0575ed6a125 upsmon[30439]: Signal 10: User requested FSD Apr 11 13:25:12 b0575ed6a125 upsmon[30439]: Executing automatic power-fail shutdown Apr 11 13:25:12 b0575ed6a125 upsmon[30439]: Auto logout and shutdown proceeding Apr 11 13:25:12 b0575ed6a125 upssched[30456]: Executing command: ups-shutdown Apr 11 13:25:17 b0575ed6a125 upsmon[30437]: parent: Unable to call shutdown command: /sbin/shutdown -h +0
Telegram也會同步告知訊息