SHARE
TWEET

ESXi NUT shutdown scripts

osnwt Jan 29th, 2013 4,148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # These are config files to my answer to the following question:
  2. # http://serverfault.com/questions/462993/vmware-esxi-shutdown-triggered-by-apc-ups-connected-via-usb
  3.  
  4. /etc/nut/nut.conf:
  5.  
  6. MODE=netserver
  7. /etc/nut/ups.conf
  8. # UPS Variables:
  9. #   battery.runtime.low: 660
  10. #   input.sensitivity: low
  11. #   ups.beeper.status: disabled
  12.  
  13. [smart-ups]
  14.     driver = usbhid-ups
  15.     port = auto
  16.     desc = "ESXi Server APC Smart-UPS"
  17.     offdelay = 180
  18.     ondelay = 240
  19.     pollfreq = 30
  20.  
  21.  
  22. /etc/nut/upsd.conf:
  23.  
  24. LISTEN 127.0.0.1
  25. LISTEN 172.16.4.250
  26.  
  27.  
  28. /etc/nut/upsd.users:
  29.  
  30. [upsmon]
  31.     password = ********
  32.     upsmon master
  33.     actions = SET
  34.     instcmds = ALL
  35.  
  36. [esxi]
  37.     password = ********
  38.     upsmon slave
  39.  
  40.  
  41. /etc/nut/upsmon.conf:
  42.  
  43. MONITOR smart-ups@localhost 1 upsmon ******** master
  44.  
  45. #SHUTDOWNCMD "/sbin/shutdown -H now"
  46. SHUTDOWNCMD "/usr/bin/logger \"Should run /sbin/shutdown -H now, but waiting for ESXi host instead\""
  47.  
  48. NOTIFYCMD /sbin/upssched
  49.  
  50. NOTIFYFLAG ONLINE       SYSLOG+WALL+EXEC
  51. NOTIFYFLAG ONBATT       SYSLOG+WALL+EXEC
  52. NOTIFYFLAG COMMBAD      SYSLOG+WALL+EXEC
  53. NOTIFYFLAG NOCOMM       SYSLOG+WALL+EXEC
  54.  
  55.  
  56. /etc/nut/upssched.conf:
  57.  
  58. CMDSCRIPT /usr/local/sbin/upssched-cmd
  59.  
  60. PIPEFN /var/run/nut/upssched.pipe
  61. LOCKFN /var/run/nut/upssched.lock
  62.  
  63. AT ONBATT * START-TIMER ups-on-battery 300
  64. AT ONLINE * CANCEL-TIMER ups-on-battery
  65.  
  66. AT NOCOMM * START-TIMER ups-no-comm 30
  67. AT COMMBAD * START-TIMER ups-comm-bad 30
  68.  
  69.  
  70. /usr/local/sbin/upssched-cmd:
  71.  
  72. #!/bin/sh
  73. NAME=`basename $0`
  74. LOGGER="/usr/bin/logger -t $NAME"
  75. UPSCMD="/bin/upscmd -uupsmon -p*******"
  76. UPSMON="/sbin/upsmon"
  77.  
  78. case $1 in
  79.     ups-on-battery)
  80.         $LOGGER "Timer event '$1' fired, executing UPS panel test..."
  81.         $UPSCMD smart-ups test.panel.start
  82.         $UPSMON -c fsd
  83.         ;;
  84.     ups-no-comm|ups-comm-bad)
  85.         $LOGGER "Timer event '$1' fired, restarting NUT..."
  86.         /usr/sbin/service nut stop
  87.         sleep 15
  88.         /usr/sbin/service nut start
  89.         ;;
  90.     *)
  91.         $LOGGER "Unrecognized command: '$1'"
  92.         ;;
  93. esac
  94.  
  95.  
  96. /etc/vmware-tools/scripts/poweroff-vm-default.d/ups-monitor:
  97.  
  98. #!/bin/bash
  99. #
  100. # /etc/vmware-tools/scripts/poweroff-vm-default.d/ups-monitor
  101. # Written by Oleg Semyonov
  102. #
  103. # This script should shutdown UPS if powerdown flag is set (/etc/killpower, for intance).
  104. # It is called by the ESXi when it powers off all VMs (and this one should be last).
  105. # UPS should give enought time to ESXi to completely shutdown. Usually it takes around 2-3
  106. # minutes since all VMs are already suspended to this moment.
  107.  
  108. UPSMON=/etc/init.d/ups-monitor
  109. MAXWAIT=15
  110.  
  111. if [ -x $UPSMON ]; then
  112.     # Looking for the powerdown flag. If not found - wait for some time.
  113.     # The ESXi host may be quicker than local upsmon and shutdown this
  114.     # machine before the local upsmon creates the powerdown flag.
  115.     flag=`sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf`
  116.  
  117.     echo -n "Looking for the powerdown flag ($MAXWAIT seconds max)"
  118.     for ((i = 1; i <= $MAXWAIT; i++)); do
  119.         [ -f "$flag" ] && break
  120.         sleep 1
  121.         echo -n "."
  122.     done
  123.  
  124.     # Finally shutdown the UPS if flag has appeared (UPS-initiated shutdown)
  125.     if [ ! -f "$flag" ]; then
  126.         echo " - not found, just halt the machine"
  127.     else
  128.         echo " - found"
  129.         $UPSMON poweroff
  130.     fi
  131. fi
  132.  
  133. # Return success regardless of script result
  134. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top