装ups的另外一个目的就是为了保证树莓派的存储卡不会因为意外断电而出现问题。其实要解决这个问题个人觉得也简单,毕竟也是个linux啊。可以基于nut实现。
The primary goal of the Network UPS Tools (NUT) project is to provide support for Power Devices, such as Uninterruptible Power Supplies, Power Distribution Units, Automatic Transfer Switches, Power Supply Units and Solar Controllers. NUT provides a common protocol and set of tools to monitor and manage such devices, and to consistently name equivalent features and data points, across a vast range of vendor-specific protocols and connection media types.
NUT provides many control and monitoring features, with a uniform control and management interface. If you are just getting acquainted with NUT, that page also explains the technical design and some possible set-ups.
More than 170 different manufacturers, and several thousands of models are compatible.
https://networkupstools.org/index.html
为了要实现树莓派在断电之后还能和venus通讯需要将华为的路由器也切换到ups系统上,只有这样在断电之后树莓派才能直到断电了。晚上重新梳理了一下家的网络拓普(不是专业网络工程师,图例可能用的不太对)
于是又让我想到了联通的工程师来家里换光纤的时候直摇头,哈哈哈。现在组网方式包括5g桥接,光纤,网线,哈哈哈。这个对接种类神奇了。一台企业路由+一台企业交换机+2套光转换+4台无线路由器。好处就是现在家里无线网络无死角覆盖,速度嘛也非常ok。就是太费电,但是我也不想再改了啊,太麻烦了。
都切换到ups供电之后在树莓派上就可以开始配置了:
1.安装nut client
sudo apt install nut
2.修改配置文件:
编辑/etc/nut/nut.conf,MODE设置如下: MODE=netclient 编辑/etc/nut/upsmon.conf,MONITOR设置如下: MONITOR ups@192.168.1.12 1 monuser secret slave 说明: MONITOR <system> <powervalue> <username> <password> ("master"|"slave") #<system>设置为ups@服务器地址或域名,这里是ups@192.168.1.12 #<powervalue>设置为1 #<username>群晖UPS服务器的默认用户名是monuser #<password>群晖UPS服务器的默认密码是secret #("master"|"slave"),客户机设置为slave
3.测试链接,需要修改venus添加允许的ip地址,如果看到下面的内容那就成功啦:
4. 启动服务:
systemctl start nut-client
5.设置服务开机自启动
systemctl enable nut-client
如果出现下面的错误,可以检查服务器ip白名单,群晖修改白名单之后要点击右下角的应用才会生效。
延伸设置—自定义关机设置:
默认关机逻辑貌似是:nut服务会在UPS发送LOWBATT
时通知机器关机,触发时机默认为ups电量剩余20%
。
如果要自定义关机设置需要进行如下设置(因为群辉提供的ups服务器在ups断电之前就关闭了,不清楚服务器在关机之前是不是会发送lowbatt消息)
1.编辑upsmon.conf,添加以下内容:
vim /etc/nut/upsmon.conf
NOTIFYCMD /sbin/upssched NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
通知类型定义:
NOTIFYMSG type message upsmon comes with a set of stock messages for various events. You can change them if you like. NOTIFYMSG ONLINE "UPS %s is getting line power" NOTIFYMSG ONBATT "Someone pulled the plug on %s" Note that %s is replaced with the identifier of the UPS in question. The message must be one element in the configuration file, so if it contains spaces, you must wrap it in quotes. NOTIFYMSG NOCOMM "Someone stole UPS %s" Possible values for type: ONLINE UPS is back online ONBATT UPS is on battery LOWBATT UPS is on battery and has a low battery (is critical) FSD UPS is being shutdown by the primary (FSD = "Forced Shutdown") COMMOK Communications established with the UPS COMMBAD Communications lost to the UPS SHUTDOWN The system is being shutdown REPLBATT The UPS battery is bad and needs to be replaced NOCOMM A UPS is unavailable (can’t be contacted for monitoring) NOTIFYFLAG type flag[+flag]… By default, upsmon sends walls global messages to all logged in users) via /bin/wall and writes to the syslog when things happen. Except for Windows where upsmon only writes to the syslog by default. You can change this. Examples: NOTIFYFLAG ONLINE SYSLOG NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC Possible values for the flags: SYSLOG Write the message to the syslog WALL Write the message to all users with /bin/wall EXEC Execute NOTIFYCMD (see above) with the message IGNORE Don’t do anything If you use IGNORE, don’t use any other flags on the same line.
2.修改upssched.conf添加以下内容
vim /etc/nut/upssched.conf
CMDSCRIPT /etc/nut/upssched-cmd #编写此脚本设置 # NOTIFYCMD /sbin/upssched # NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC PIPEFN /etc/nut/upssched.pipe LOCKFN /etc/nut/upssched.lock AT ONBATT * START-TIMER power-off 60 AT ONLINE * CANCEL-TIMER power-off AT ONLINE * EXECUTE power-on
3.编辑upssched-cmd脚本
vim /etc/nut/upssched-cmd
文件内容:
#!/bin/sh case $1 in onbatt) logger -t upssched-cmd "UPS running on battery" # do somethings ,e.g.send email \ wechat /usr/sbin/upsmon -c fsd ;; power-off) logger -t upssched-cmd "UPS running on battery power off" /usr/sbin/upsmon -c fsd ;; shutdowncritical) logger -t upssched-cmd "UPS on battery critical, forced shutdown" /usr/sbin/upsmon -c fsd ;; upsgone) logger -t upssched-cmd "UPS has been gone too long, can't reach" ;; *) logger -t upssched-cmd "Unrecognized command: $1" ;; esac
修改完成之后重启服务。
参考链接:
https://typecho.leosutopia.cn/index.php/archives/248/#comment-13
https://networkupstools.org/docs/man/index.html#User_man
https://networkupstools.org/docs/man/upssched.conf.html
https://networkupstools.org/docs/man/upsmon.conf.html
https://www.cnblogs.com/LandWind/articles/pve-nut-config.html
https://www.storyxc.com/pages/41ed75/#%E7%BC%96%E5%86%99%E8%84%9A%E6%9C%AC
5 comments
还在想着如何实现服务器对接UPS,刚好试试你的方法!
嗯嗯,还是可以的。可以一试~~
有一个小问题,当切换UPS供电,树莓派多久会关机?
默认的关机逻辑貌似是这样的:nut服务会在UPS发送LOWBATT时通知机器关机,触发时机默认为ups电量剩余20%
可以自定义事件处理逻辑,功能还是蛮强大的