实现非管理型UPS在linux主机上的停电自动关机

买了个山特的SANTAK TG-BOX 850 UPS,自带USB通讯线缆。本以为官方软件提供Linux下的CLI命令以监控UPS状态.

官网提供的下载链接巨慢无比不说,CLI下只提供了安装脚本,没有状态监控程序。我TM……

算了,当个非管理型的UPS用吧。搜索了下网上提供的脚本,感觉写的都不太合适。自己重新修改了一下。

#!/bin/sh
while true
do
    ping -c 1 192.168.50.2 > /dev/null
    ret=$?
    if [ $ret -ne 0 ]
    then
    echo -e "AC POWER LOSS! UPS WORKING NOW!\n-------------------------------\nSave your jobs and a further check will be execute in 3 mins,if it still loss,system will be shutdown immediately!" | wall
    sleep 180
    ping -c 1 192.168.50.2 > /dev/null
    ret=$?
        if [ $ret -ne 0 ]
        then
        echo -e "AC POWER STILL LOSS! SYSTEM WILL SHUTDOWN NOW" | wall
        poweroff
        fi
    fi
sleep 5
done

然后把ups.sh的功能注册成服务,方便操作:

新建/usr/lib/systemd/system/ups.service

[Unit]
Description=UPS monitor and shutdown automaticlly
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=nohup /root/ups.sh &
ExecStop=/bin/kill $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

启动服务:

systemctl start ups.service

让服务开机自动运行

systemctl enable ups.service

 

done.

-------------------后续----------------------

经评论区klever同学提醒,apcupsd是支持这个型号的UPS的。

我原以为apcupsd只支持APC的UPS。

实际试了一下,效果不错:

[root@NAS ~]# apcaccess
APC : 001,028,0668
DATE : 2020-09-29 14:57:55 +0800
HOSTNAME : NAS
VERSION : 3.14.14 (31 May 2016) redhat
UPSNAME : SANTAK-850
CABLE : USB Cable
DRIVER : USB UPS Driver
UPSMODE : Stand Alone
STARTTIME: 2020-09-29 14:57:51 +0800
MODEL : SANTAK TG-BOX
STATUS : ONLINE
LOADPCT : 14.0 Percent
BCHARGE : 100.0 Percent
TIMELEFT : 40.0 Minutes
MBATTCHG : 10 Percent
MINTIMEL : 10 Minutes
MAXTIME : 0 Seconds
OUTPUTV : 230.0 Volts
DWAKE : -1 Seconds
LOTRANS : 184.0 Volts
HITRANS : 264.0 Volts
ALARMDEL : 30 Seconds
NUMXFERS : 0
TONBATT : 0 Seconds
CUMONBATT: 0 Seconds
XOFFBATT : N/A
STATFLAG : 0x05000008
SERIALNO : Blank
END APC : 2020-09-29 14:57:56 +0800

posted @ 2019-09-22 17:36  Mrcoool  阅读(3310)  评论(4编辑  收藏  举报