Skip to content

Commit

Permalink
nut (network ups tools) conf and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
StalkR committed Jul 9, 2013
1 parent 4785c04 commit ab37146
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions nut/nut.conf
@@ -0,0 +1 @@
MODE=standalone
18 changes: 18 additions & 0 deletions nut/ups.conf
@@ -0,0 +1,18 @@
# Refer to hardware compatibility list
# http://www.networkupstools.org/stable-hcl.html

[myups]
driver=usbhid-ups
port=auto
desc="Some generic USB UPS"

#[serial]
#driver=genericups
#upstype=7
#port=/dev/ttyS0
#desc="Some generic serial UPS"

#[specific]
#driver=megatec_usb
#port=/dev/usb/hiddev0
#desc="A specific model"
2 changes: 2 additions & 0 deletions nut/upsd.conf
@@ -0,0 +1,2 @@
LISTEN 127.0.0.1
LISTEN ::1
3 changes: 3 additions & 0 deletions nut/upsd.users
@@ -0,0 +1,3 @@
[USER]
password = PASS
upsmon master
18 changes: 18 additions & 0 deletions nut/upsmon.conf
@@ -0,0 +1,18 @@
# upsmon.conf(8) second arg is powervalue: 1 if it powers this system else 0
# use 1 when this system needs shutdown, 0 when just monitoring
MONITOR myups 1 USER PASS master

SHUTDOWNCMD "/sbin/shutdown -h now"

NOTIFYCMD /sbin/upssched

NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC
NOTIFYFLAG FSD SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK SYSLOG+EXEC
NOTIFYFLAG COMMBAD SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+EXEC
NOTIFYFLAG NOCOMM SYSLOG+EXEC
NOTIFYFLAG NOPARENT SYSLOG+EXEC
51 changes: 51 additions & 0 deletions nut/upssched-cmd.sh
@@ -0,0 +1,51 @@
#!/bin/bash
# Dispatch actions on UPS events.

main() {
local ups timer host to
ups=${1%%_*}
timer=${1#*_}
host=$(hostname)
to=root

case "$timer" in
"fsd")
date | mail -s "[$ups@$host] Forced shutdown" $to
shutdown ;;
"onbatt")
date | mail -s "[$ups@$host] Power lost, running on battery" $to
touch /var/run/nut/onbatt ;;
"onbatt-toolong")
date | mail -s "[$ups@$host] Too long on battery, forcing shutdown" $to
touch /var/run/nut/onbatt-toolong
shutdown ;;
"online")
date | mail -s "[$ups@$host] Power is back" $to
rm -f /var/run/nut/onbatt /var/run/nut/onbatt-toolong ;;
"lowbatt")
date | mail -s "[$ups@$host] Low battery" $to ;;
"commok")
date | mail -s "[$ups@$host] Communications OK" $to ;;
"commbad")
date | mail -s "[$ups@$host] Communications lost" $to ;;
"shutdown")
date | mail -s "[$ups@$host] System being shutdown" $to ;;
"replbatt")
date | mail -s "[$ups@$host] Bad battery, replace it" $to ;;
"nocomm")
date | mail -s "[$ups@$host] UPS unavailable" $to ;;
"noparent")
date | mail -s "[$ups@$host] Shutdown impossible, parent died" $to ;;
*)
logger -t upssched-cmd "Unrecognized command: $timer ($1)" ;;
esac
}

shutdown() {
ssh -i /etc/nut/shutdown.key root@othermachine halt
/sbin/upsmon -c fsd
}

if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
main "$@"
fi
27 changes: 27 additions & 0 deletions nut/upssched.conf
@@ -0,0 +1,27 @@
CMDSCRIPT /etc/nut/upssched-cmd.sh

PIPEFN /var/run/nut/upssched.pipe
LOCKFN /var/run/nut/upssched.lock

# Syntax: AT notifytype upsname command

# We choose command format: <upsname>_<triggername>
# so it can be parsed in CMDSCRIPT.

# myups
AT ONLINE myups EXECUTE myups_online
AT ONBATT myups EXECUTE myups_onbatt
AT LOWBATT myups EXECUTE myups_lowbatt
AT FSD myups EXECUTE myups_fsd
AT COMMOK myups EXECUTE myups_commok
AT COMMBAD myups EXECUTE myups_commbad
AT SHUTDOWN myups EXECUTE myups_shutdown
AT REPLBATT myups EXECUTE myups_replbatt
AT NOCOMM myups EXECUTE myups_nocomm
AT NOPARENT myups EXECUTE myups_noparent
# Start a timer that will execute when UPS is on battery for 30 seconds.
AT ONBATT myups START-TIMER myups_onbatt-toolong 30
# Stop if power is back.
AT ONLINE myups CANCEL-TIMER myups_onbatt-toolong

# repeat for other UPSes

0 comments on commit ab37146

Please sign in to comment.