📜 ⬆️ ⬇️

Setting up from scratch the Network UPS Tools UPS Management Service (NUT) to manage a locally connected UPS

Setting up Network UPS Tools on Linux using the example of Eaton 5E650iUSB UPS


Description


The Linux NUT service (Network UPS Tools) is a set of programs for monitoring and managing various uninterruptible power supply units (hereinafter referred to as UPS). A complete list of supported models can be obtained by looking at the list of drivers in the /usr/share/nut/driver.list file.


The manual describes how to configure the PC to turn off the NUT agent when power is lost on the network using the example of Eaton 5E 650iUSB UPS on Ubuntu-like distributions. For use under other distributions, use the package manager of your distribution or build from source codes. The latest version of Network UPS Tools can be downloaded on GitHub via the link link .


NUT setting


First you need to set NUT:


root@hostname~# apt install nut 

Make a backup copy of the NUT configuration files folder:


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

Now add a directive indicating that the UPS is connected to this computer, and not to the remote computer:


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

Now connect the UPS to the computer and see the output of the lsusb command:


  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 

We are looking for whether the UPS is supported by the NUT service. You can either view it manually or print lines that mention the UPS brand, for example:


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

Now that we have decided on the driver, we can configure the NUT.


Configuration file protection


You must set the correct permissions and owners for the NUT configuration files.


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

NUT setup


Setting up the driver, the connection method and the off time


We add lines to the end of the /etc/nut/ups.conf file or create a new one with the following contents:


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

Configuring the access of the NUT system group to the UPS via USB


In order for NUT to have access to the USB interface of the UPS, you need to write an access rule for udev .


udev is the Linux device management subsystem. Thanks to udev, only the currently connected devices are in the / dev folder.


Execute the command


  root@hostname~# lsusb 

and find the line corresponding to the UPS. In our example, this


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

where id follows idVendor: idProduct ( 0463: ffff )


Now create a file with the rule for 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" 

After creating the rule, restart the udev service:


  root@hostname~# service udev restart 

After that, disconnect and reconnect the USB cable from the UPS. After that, run the command to check the operation of the udev rules:


 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 

If the output is approximately the same, then everything is set up correctly.


Configuring addresses and ports listening connections to the NUT


We add lines to the end of the /etc/nut/upsd.conf file or create a new one with the following contents:


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

Keep in mind that LISTEN should be written in capital letters, or nothing will work.


Setting a user profile to access the NUT


We create the user upsmonitor without the right of login and home folder, with a UID less than 1000, so that it is considered a service and it was not on the users login screen, and with GID'om group nut .


Find out the nut GID group:


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

Now we will select the UID for the user. Either choose one that is hardly used by any program, for example, 339, or execute:


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

and if nothing is output, the UID is free and can be used to create the user upsmonitor :


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

Now add this user to the NUT user profiles file /etc/nut/upsd.users :


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

NUT monitoring settings


We add lines to the end of the /etc/nut/upsmon.conf file or create a new one with the following contents:


 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 

Compound test


Let's see that the UPS can report its status:


  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 

All fields are less obvious or were defined by us in the configuration files earlier. UPS status codes are displayed in the ups.status: field and can take values:



Internal commands and UPS variables


The UPS can be given commands directly from the console, using the upscmd command . The list of available commands can be obtained by running the command:


  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 

The beeper. * Settings control the UPS signaling in the event of mains voltage loss, load.off turns off the PC immediately, load.off.delay delay in seconds before turning off the PC, shutdown.stop - command to interrupt the shutdown process of the PC.


Some UPSs have other options, such as a battery test or shutdown.return , which, if used, will turn off the computer, but the UPS will send a power-on signal to the PC as soon as the mains power is restored. In order for this to work, a corresponding function must be enabled in the PC BIOS, which is usually located somewhere in the area of ​​the power settings.


For example, turn off the squeak of the UPS when the mains power fails:


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

To enable, replace beeper.disable with beeper.enable .


Configuring the NUT task scheduler upssched


We add lines to the end of the /etc/nut/upssched.conf file or create a new one with the following contents:


  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 

Now you need the script /etc/nut/cmd.sh . Create it, set permissions and fill it in:


  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 

Conclusion


This completes the setup. You can test, trite pulling the UPS from the outlet.


')

Source: https://habr.com/ru/post/443736/


All Articles