How to power off a Debian server and a UPS on low battery?

How to configure a Debian server with one standalone USB UPS to:

  1. Initiate a UPS power off after one minute when the battery is low.
  2. Shutdown itself.
  3. And to be sure that when mains power is restored (even before the UPS runs out) the Debian server will start.

These steps are described in this question but there is no explanation how to achieve them.

I have:

  • My server configured to always start when the power is restored.
  • Installed nut and when I run upsc myups I see the info from the UPS, including the battery level. So I can easily write a script to monitor the battery and shutdown the server.

My question is:

How to send a power off command to the UPS?

The UPS is: EATON 5E 650i

In /etc/nut/nut.conf I have:

MODE=standalone

In /etc/nut/ups.conf I have:

[myups]
    driver = usbhid-ups
    port = auto
    vendorid = 0463
    pollfreq = 30

upsc myups prints:

Init SSL without certificate database
battery.charge: 100
battery.runtime: 1825
battery.type: PbAc
device.mfr: EATON
device.model: 5E 650i
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.parameter.vendorid: 0463
driver.version: 2.7.4
driver.version.data: MGE HID 1.39
driver.version.internal: 0.41
input.voltage: 230.0
outlet.1.status: on
outlet.desc: Main Outlet
outlet.id: 1
outlet.switchable: no
output.frequency: 49.9
output.frequency.nominal: 50
output.voltage: 230.0
output.voltage.nominal: 230
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.firmware: 03.08.0018
ups.load: 19
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

OK, I think it is working now. Here is what I did:

apt-get install nut-server

apt-get install nut-client

nano /etc/nut/nut.conf

MODE=standalone

nano /etc/nut/ups.conf

[myups]
    driver = usbhid-ups
    port = auto
    vendorid = 0463
    pollfreq = 30

/sbin/upsdrvctl start

upsc myups Prints some problems.

Reboot /usr/sbin/shutdown -r now

Now when you execute upsc myups you should see the info of your UPS:

Init SSL without certificate database
battery.charge: 86
battery.runtime: 956
battery.type: PbAc
device.mfr: EATON
device.model: 5E 650i
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.parameter.vendorid: 0463
driver.version: 2.7.4
driver.version.data: MGE HID 1.39
driver.version.internal: 0.41
input.voltage: 226.0
outlet.1.status: on
outlet.desc: Main Outlet
outlet.id: 1
outlet.switchable: no
output.frequency: 49.9
output.frequency.nominal: 50
output.voltage: 225.0
output.voltage.nominal: 230
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.firmware: 03.08.0018
ups.load: 27
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

If /sbin/upsmon print something like:

Network UPS Tools upsmon 2.7.4
fopen /var/run/nut/upsmon.pid: No such file or directory
Using power down flag file /etc/killpower

Fatal error: insufficient power configured!

Sum of power values........: 0
Minimum value (MINSUPPLIES): 1

Edit your upsmon.conf and change the values.
    

try: nano /etc/nut/upsd.users

[upsmon]
    password  = mypassword
    upsmon master

chmod 640 /etc/nut/upsd.users

ls -lahrt /etc/nut/upsd.users

-rw-r----- 1 root nut 2,2K Dec 13 13:15 /etc/nut/upsd.users

nano /etc/nut/upsmon.conf

MONITOR myups@localhost 1 upsmon mypassword master

systemctl enable nut-monitor

/sbin/upsmon

Network UPS Tools upsmon 2.7.4
fopen /var/run/nut/upsmon.pid: No such file or directory
UPS: myups@localhost (master) (power value 1)
Using power down flag file /etc/killpower

Here is what can be seen in syslog when I tested by cutting off the electricity:

Dec 13 15:47:01 pc upsmon[604]: UPS myups@localhost on battery
... 12 minutes later
Dec 13 15:59:32 pc upsmon[604]: UPS myups@localhost battery is low
Dec 13 15:59:32 pc upsd[601]: Client upsmon@::1 set FSD on UPS [myups]
Dec 13 15:59:32 pc upsmon[604]: Executing automatic power-fail shutdown
Dec 13 15:59:32 pc upsmon[604]: Auto logout and shutdown proceeding
Dec 13 15:59:37 pc systemd[1]: nut-monitor.service: Succeeded.
Dec 13 15:59:37 pc systemd[1]: unattended-upgrades.service: Succeeded.
Dec 13 15:59:37 pc systemd[1]: Stopping Session 39 of user a.
Dec 13 15:59:37 pc systemd[1]: Stopped target Graphical Interface.
Dec 13 15:59:37 pc upsd[601]: mainloop: Interrupted system call
Dec 13 15:59:37 pc systemd[1]: Stopped target Multi-User System.
Dec 13 15:59:37 pc upsd[601]: Signal 15: exiting
Dec 13 15:59:37 pc systemd[1]: Stopping Network UPS Tools - power devices information server...
Dec 13 15:59:37 pc systemd[1]: postgresql.service: Succeeded.
Dec 13 15:59:37 pc systemd[1]: Stopped PostgreSQL RDBMS.
Dec 13 15:59:37 pc systemd[1]: Stopping PostgreSQL Cluster...
Dec 13 15:59:37 pc systemd[1]: Stopping The Apache HTTP Server...
Dec 13 15:59:37 pc systemd[1]: Stopping LSB: disk temperature monitoring daemon...
Dec 13 15:59:37 pc systemd[1]: sysstat.service: Succeeded.
Dec 13 15:59:37 pc systemd[1]: Stopped Resets System Activity Data Collector.
Dec 13 15:59:37 pc smartd[392]: smartd received signal 15: Terminated
Dec 13 15:59:37 pc smartd[392]: Device: /dev/sda [SAT], state written to /var/lib/smartmontools/smartd.xxx.ata.state
Dec 13 15:59:37 pc smartd[392]: Device: /dev/sdb [SAT], state written to /var/lib/smartmontools/smartd.yyy.ata.state
Dec 13 15:59:37 pc smartd[392]: smartd is exiting (exit status 0)
Dec 13 15:59:37 pc systemd[1]: Stopping Self Monitoring and Reporting Technology (SMART) Daemon...
Dec 13 15:59:37 pc systemd[1]: Stopping Regular background program processing daemon...
Dec 13 15:59:37 pc systemd[1]: Stopped target Login Prompts.
Dec 13 15:59:37 pc systemd[1]: Stopping OpenBSD Secure Shell server...
... when power is on again
Dec 13 16:01:28 pc kernel: [    0.000000] Linux version 4.19.0-9-amd64 ([email protected]) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.1$

After the boot when I execute upsmon I see the following:

/sbin/upsmon

Network UPS Tools upsmon 2.7.4
Fatal error: A previous upsmon instance is already running!
Either stop the previous instance first, or use the 'reload' command.

Which means it is running and everything is fine, I guess :)

You can see the meaning of the variables, printed by upsc, such as ups.status, ups.timer.shutdown, battery.charge, here: https://networkupstools.org/docs/user-manual.pdf

Another useful URL is: https://risc-a-day.blogspot.com/2014/09/getting-my-ups-to-work-with-linux.html