树莓派UPS自定义关机脚本(NUT)

NUT 树莓派自定义关机逻辑,上一篇文章太长了,拉出来一些:

默认关机逻辑貌似是: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://h4ck.org.cn/2023/02/%E6%98%AFups%E5%90%96%EF%BC%88%E4%B8%89%EF%BC%89-%E6%A0%91%E8%8E%93%E6%B4%BE/

 

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=155gv09voxk2

☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《树莓派UPS自定义关机脚本(NUT)》
* 本文链接:https://lang.bi/2023/02/11197
* 短链接:https://oba.by/?p=11197
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

12 comments

  1.   Level 6
    Google Chrome 109 Google Chrome 109 Mac OS X 10.15 Mac OS X 10.15 cn浙江省杭州市 华数

    树莓派是 arm 架构的太麻烦了。所以我搞了个工控机~

    1. 公主 Queen 
      Google Chrome 102 Google Chrome 102 Mac OS X 10.15 Mac OS X 10.15 cn山东省青岛市 移动

      其实多数情况下linux系统的arm支持还算不错,使用起来体验差别不大。就当成个普通linux系统用就可以啦~~

    1. 公主 Queen 
      Google Chrome 108 Google Chrome 108 Windows 10 Windows 10 cn山东省青岛市 联通

      是的,我是看杜老师挂了硬盘之后买的。比他晚了几天~~

      1. Level 3
        Firefox 109 Firefox 109 Windows 10 Windows 10 cn辽宁省沈阳市 联通

        如果是以前说不定我也想整一个试试看,挂博客玩玩,现在是真没这个动力了。

        看看你们折腾挺好的,虽然代码编译看不懂 哈哈。

        1. 公主 Queen 
          Google Chrome 108 Google Chrome 108 Windows 10 Windows 10 cn山东省青岛市 联通

          降低希望值一切都好了,现在我也是自娱自乐啊

  2. Level 4
    Safari 16 Safari 16 iPhone iOS 16.3 iPhone iOS 16.3 cn四川省雅安市 电信

    最近都在折腾ups吖,我是来看图的,技术文我不行

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注