Setup an UPS monitor
mouse 423 · person cloud · link
Last update
2019-01-12
2019
01-12
«NUT - Network UPS Tools»
1
apt-get install nut # install server

Set MODE=standalone in /etc/nut/nut.conf.

Append the user definition in /etc/nut/upsd.users:

1
2
3
4
[monuser]
  password = ApAssWorD
  allowfrom = localhost
  upsmon master

If you use an USB to serial converter, find it via lsusb:

1
lsusb | grep Serial # Bus 001 Device 009: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

then add a udev rule to fix its device name:

1
2
# /etc/udev/rules.d/10-local.rules
ACTION=="add", KERNEL=="ttyUSB[0-9]*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SYMLINK+="ttyUSB_UPS"

Append the UPS definition in /etc/nut/ups.conf (mine is a powercom model):

1
2
3
4
5
6
[myups]
  driver = powercom
  port = /dev/ttyUSB_UPS
  desc = "Main supply"
  loadPercentage = {6.1343,-0.3808,1.075,0.1811}
  batteryPercentage = {5.0000,0.3268,-825.00,0.46511,0}

Configure the monitor in /etc/nut/upsmon.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
MONITOR myups@localhost 1 monuser ApAssWorD master

# setup a custom script to notify you
NOTIFYCMD /opt/ups/nut_notify_cmd.sh

# customize notification messages
NOTIFYMSG ONBATT "Power failure! UPS %s is on battery!"
NOTIFYMSG SHUTDOWN "UPS %s is low on battery! Shutting down the system..."

# enable some logging
NOTIFYFLAG ONLINE SYSLOG+EXEC
NOTIFYFLAG ONBATT SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL

Add the nut user to dialout group:

1
useradd -G dialout nut

Restart the services and test the ups:

1
2
3
4
5
6
systemctl restart nut-driver
systemctl restart nut-server
systemctl restart nut-monitor

upsc -l     # list all monitored UPSes
upsc myups  # show UPS details and status

Note: To cleanly unmount VeraCrypt volumes you can use this NOTIFYCMD script:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

# /opt/ups/nut_notify_cmd.sh - nut NOTIFYCMD script

# put in /etc/sudoers.d/local:
# nut raoul = (root)  NOPASSWD: /sbin/shutdown, /opt/systemd-units/vc-mounter.rb

if [ "$NOTIFYTYPE" = "ONBATT" ]; then
  /opt/bin/shutdown -h 600 &
elif [ "$NOTIFYTYPE" = "ONLINE" ]; then
  sleep 3 # wait for first command to start
  /opt/bin/shutdown -c     &
fi

that uses this useful shutdown script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/ruby

$VERBOSE = nil

# /opt/bin/shutdown - emulate shutdown command and unmount VeraCrypt volumes

require 'optparse'
require 'shellwords'

opts = {}
OptionParser.new{|o|
  o.banner = "Usage: #{File.basename __FILE__} [options]"

  o.on('-r', '--reboot   [N]', Float, 'Reboot   after N seconds (default=0)') do |n|
    opts.merge! action: :reboot, delay: n.to_f
  end

  o.on('-h', '--poweroff [N]', Float, 'Poweroff after N seconds (default=0)') do |n|
    opts.merge! action: :shutdown, delay: n.to_f
  end

  o.on('-c', '--cancel', 'Cancel a pending operation') do |enabled|
    opts.merge! action: :cancel
  end

  opts.merge! help: o.to_s
}.parse!

case opts[:action]
  when :cancel
    `pgrep -fl vc-mounter.rb`
    if $?.to_i == 0
      puts 'aborted'
      exit
    end
    # kill all but yourself
    pids = `pgrep -f ruby.*shutdown`.split("\n").map(&:to_i) - [Process.pid]
    pids.each{|pid| system "kill #{pid}" }
  when :reboot, :shutdown
    sleep opts[:delay]
    `sudo swapon | grep " file " | cut -f 1 -d " "`.strip.split("\n").
      each{|f| system "sudo swapoff #{f.shellescape}" }
    system 'sudo /opt/systemd-units/vc-mounter.rb try-umount'
    system "sudo /sbin/shutdown -#{opts[:action] == :reboot ? :r : :h} 0 &"
  else
    puts opts[:help]
end