从头开始设置网络管理工具(NUT)以管理本地连接的UPS

以伊顿5E650iUSB UPS为例在Linux上配置网络UPS工具


内容描述


Linux NUT服务(网络UPS工具)是用于各种不间断电源设备(以下称为UPS)的一组监视和控制程序。 通过查看/usr/share/nut/driver.list文件中的驱动程序列表,可以获得支持的模型的完整列表。


该手册以类似Ubuntu的发行版上的Eaton 5E 650iUSB UPS为例,介绍了如何在网络电压丢失的情况下通过NUT代理禁用PC。 要与其他发行版一起使用,请使用发行版的程序包管理器或从源代码进行编译。 可以从链接链接在GitHub上下载最新版本的Network UPS Tools。


安装NUT


首先,您需要安装NUT:


root@hostname~# apt install nut 

使用NUT配置文件制作该文件夹的备份副本:


  root@hostname~# cp -r /etc/nut /etc/nut.orig 

现在添加一条指令,指示UPS已连接到此计算机,而不是远程计算机:


  root@hostname~# echo "MODE=standalone" > /etc/nut/nut.conf 

现在将UPS连接到计算机,并查看lsusb命令的输出:


  root@hostname~# lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 004: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse Bus 001 Device 003: ID 1a2c:2124 China Resource Semico Co., Ltd Bus 001 Device 002: ID 0463:ffff MGE UPS Systems UPS 

我们正在寻找NUT服务是否支持UPS。 您可以手动查看它,也可以显示带有UPS品牌名称的行,例如:


  root@hostname~# grep Eaton /usr/share/nut/driver.list ... "Eaton" "ups" "5" "5E650iUSB" "USB port" "usbhid-ups" ... 

现在我们已经决定了驱动程序,您可以配置NUT。


配置文件保护


应该为NUT配置文件设置正确的权限和所有者


  root@hostname~# chown root:nut /etc/nut/* root@hostname~# chmod 640 /etc/nut/* 

螺母设置


设置驱动程序,连接方法和关机时间


将这些行添加到/etc/nut/ups.conf文件的末尾,或创建一个包含以下内容的新行:


  root@hostname~# nano /etc/nut/ups.conf #    NUT.         [eaton] #   driver = usbhid-ups #  .           port = auto #        offdelay = 90 #          ondelay = 5 #   desc = "Eaton 5E 650iUSB" 

配置NUT系统组通过USB访问UPS


为了使NUT能够访问UPS的USB接口,您需要为udev编写访问规则。


udev是Linux设备管理子系统。 感谢udev,只有当前连接的设备位于/ dev文件夹中。


运行命令


  root@hostname~# lsusb 

并找到与UPS对应的线路。 在我们的示例中


 Bus 001 Device 002: ID 0463:ffff MGE UPS Systems UPS 

ID在哪里之后idVendor:idProduct0463:ffff


现在为udev创建一个规则文件:


  root@hostname~# /etc/udev/rules.d/90-nut-ups.rules # Eaton 5E650iUSB ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0463", ATTR{idProduct}=="ffff", MODE="0660", GROUP="nut" 

创建规则后,必须重新启动udev服务:


  root@hostname~# service udev restart 

之后,从UPS断开并重新连接USB电缆。 之后,运行命令以验证udev规则的运行状况:


 root@hostname~# upsdrvctl start Network UPS Tools - UPS driver controller 2.7.4 Network UPS Tools - Generic HID driver 0.41 (2.7.4) USB communication driver 0.33 Using subdriver: MGE HID 1.39 

如果输出大致相同,则说明所有配置正确。


配置NUT地址和端口以侦听NUT连接


/etc/nut/upsd.conf文件的末尾添加这些行,或创建一个包含以下内容的新行:


  root@hostname~# nano /etc/nut/upsd.conf #  Loopback    3493 LISTEN 127.0.0.1 3493 

请记住,LISTEN必须以大写字母书写,否则将不起作用。


配置用户配置文件以访问NUT


我们创建的用户upsmonitor没有登录名和home文件夹的权限,UID小于1000,因此它被视为一项服务,不在用户登录屏幕上,并且具有螺母组的GID。


找出螺母组的GID:


  root@hostname~# cat /etc/group | grep nut:x: nut:x:134: 

现在,我们为用户选择UID。 我们要么选择一个几乎不被任何程序使用的程序(例如339),要么执行:


 root@hostname~# cat /etc/passwd | grep [  100  1000] 

如果未显示任何内容,则说明该UID是免费的,可用于创建upsmonitor用户:


 root@hostname~# useradd -d /dev/null -s /usr/sbin/nologin -u 339 -g 134 -p UPSPASS upsmonitor 

现在将此用户添加到NUT用户配置文件/etc/nut/upsd.users中


 root@hostname~# nano /etc/nut/upsd.users #   [upsmonitor] #   password = UPSPASS #      ,     .. actions = SET #          upscmd instcmds = ALL #       NUT upsmon master 

NUT监控设置


将这些行添加到/etc/nut/upsmon.conf文件的末尾,或创建一个包含以下内容的新行:


 root@hostname~# nano /etc/nut/upsmon.conf #   eaton  localhost     upsmonitor MONITOR eaton@localhost 1 upsmonitor UPSPASS master #    MINSUPPLIES 1 #    NUT,      NOTIFYCMD /sbin/upssched #           POLLFREQ 5 #           POLLFREQALERT 5 #      NUT    HOSTSYNC 15 #  NUT                "" DEADTIME 15 #  ,  ,           POWERDOWNFLAG /etc/killpower #          NOTIFYMSG ONLINE "UPS %s on line power" #           NOTIFYMSG ONBATT "UPS %s on battery" #             NOTIFYMSG LOWBATT "UPS %s battery is low" #          NOTIFYMSG FSD "UPS %s: forced shutdown in progress" #           NOTIFYMSG COMMOK "Communications with UPS %s established" #           , ,    USB  NOTIFYMSG COMMBAD "Communications with UPS %s lost" #                        NOTIFYMSG SHUTDOWN "Auto logout and shutdown proceeding" #            NOTIFYMSG REPLBATT "UPS %s battery needs to be replaced" #            NOTIFYMSG NOCOMM "UPS %s is unavailable" #            NUT upsmon NOTIFYMSG NOPARENT "upsmon parent process died - shutdown impossible" #     ,   NOTIFYFLAG. # SYSLOG        ,   /var/log/syslog # WALL          # EXEC   ,    /etc/nut/upsshed.conf,    NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC NOTIFYFLAG FSD SYSLOG+WALL+EXEC NOTIFYFLAG COMMOK SYSLOG+WALL+EXEC NOTIFYFLAG COMMBAD SYSLOG+WALL+EXEC NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC NOTIFYFLAG NOCOMM SYSLOG+WALL+EXEC NOTIFYFLAG NOPARENT SYSLOG+WALL+EXEC #           (12 ) RBWARNTIME 43200 #  NUT      ,    NOCOMM  300  NOCOMMWARNTIME 300 #       FINALDELAY 5 

连接测试


让我们看看UPS可以报告其状况:


  root@hostname~# upsc eaton@localhost battery.charge: 100 battery.runtime: 1964 battery.type: PbAc device.mfr: EATON device.model: 5E 650i device.type: ups driver.name: usbhid-ups driver.parameter.offdelay: 60 driver.parameter.pollfreq: 30 driver.parameter.pollinterval: 2 driver.parameter.port: auto driver.parameter.synchronous: no driver.version: 2.7.4 driver.version.data: MGE HID 1.39 driver.version.internal: 0.41 input.voltage: 228.0 outlet.1.status: on outlet.desc: Main Outlet outlet.id: 1 outlet.switchable: no output.frequency: 50.0 output.frequency.nominal: 50 output.voltage: 226.0 output.voltage.nominal: 230 ups.beeper.status: enabled ups.delay.shutdown: 60 ups.firmware: 03.08.0018 ups.load: 18 ups.mfr: EATON ups.model: 5E 650i ups.power.nominal: 650 ups.productid: ffff ups.start.battery: yes ups.status: OL ups.timer.shutdown: -1 ups.vendorid: 0463 

所有字段或多或少是显而易见的,或者是我们之前在配置文件中定义的。 UPS状态代码显示在ups.status:字段中,并可以采用以下值:


  • OL-系统由网络供电;
  • OB-系统依靠电池供电;
  • LB-系统以电量耗尽的电池运行。

UPS内部命令和变量


UPS可以使用upscmd命令直接从控制台发出命令 。 可以通过运行以下命令获得可用命令的列表:


  root@hostname~# upscmd -l eaton@localhost Instant commands supported on UPS [eaton]: beeper.disable - Disable the UPS beeper beeper.enable - Enable the UPS beeper beeper.mute - Temporarily mute the UPS beeper beeper.off - Obsolete (use beeper.disable or beeper.mute) beeper.on - Obsolete (use beeper.enable) load.off - Turn off the load immediately load.off.delay - Turn off the load with a delay (seconds) shutdown.stop - Stop a shutdown in progress 

蜂鸣器*设置控制发生断电时UPS的警报, load.off立即关闭PC, load.off.delay延迟以秒为单位,然后关闭PC, shutdown.stop-命令以中断关闭PC的过程。


在某些UPS中,还有其他选项,例如,电池测试或shutdown.return ,在这种情况下,计算机将关闭,但只要市电恢复,UPS就会发送PC启动信号。 为此,必须在PC的BIOS中启用相应的功能,该功能通常位于电源设置区域中的某个位置。


例如,在断电时关闭UPS的吱吱声:


  root@hostname~# upscmd -u upsmonitor -p UPSPASS eaton@localhost beeper.disable 

要启用,请将beeper.disable替换为beeper.enable


向上配置NUT Task Scheduler


/etc/nut/upssched.conf文件的末尾添加这些行,或创建一个包含以下内容的新行:


  root@hostname~# nano /etc/nut/upssched.conf # ,       CMDSCRIPT /etc/nut/cmd.sh PIPEFN /tmp/upspipe LOCKFN /tmp/upslock #     case    /etc/nut/cmd.sh AT COMMBAD * EXECUTE commbad AT COMMOK * EXECUTE commok AT NOCOMM * EXECUTE nocomm #      .         AT ONBATT * EXECUTE powerout #  ,      shutdownnow  cmd.sh AT ONBATT * START-TIMER shutdownnow 90 AT LOWBATT * EXECUTE shutdowncritical AT ONLINE * CANCEL-TIMER shutdownnow AT ONLINE * EXECUTE powerup 

现在我们需要脚本/etc/nut/cmd.sh 。 创建它,设置权限并填写:


  root@hostname~# touch /etc/nut/cmd.sh root@hostname~# chmod 666 /etc/nut/cmd.sh root@hostname~# nano /etc/nut/cmd.sh #!/bin/sh # logger     syslog case $1 in commbad) logger "UPS communications failure" ;; commok) logger "UPS communications restored" ;; nocomm) logger "UPS communications cannot be established" ;; powerout) #         logger "UPS on battery. Shutdown in 90 seconds...." upscmd -u upsmonitor -p UPSPASS eaton@localhost shutdown.return ;; shutdownnow) logger "UPS has been on battery for 120 seconds. Starting orderly shutdown" #     upsmon -c fsd ;; shutdowncritical) logger "UPS battery level CRITICAL. Shutting down NOW!!!!" #     upsmon -c fsd ;; powerup) logger "UPS on line. Shutdown aborted." #     upscmd -u upsmon -p pass mustek@localhost shutdown.stop ;; *) logger "Unrecognized command: $1" ;; esac 

结论


这样就完成了设置。 您可以通过从插座中拔出UPS进行测试。

Source: https://habr.com/ru/post/zh-CN443736/


All Articles