-
-
Save fabrizziosoares/28a25b884b37b4118eaefaac49dcaae2 to your computer and use it in GitHub Desktop.
Network UPS Tools on macOS High Sierra (netclient)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #install homebrew | |
| /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
| #install network ups tools | |
| brew update | |
| brew install nut | |
| #create directories and change permissions | |
| sudo mkdir -p /var/state/ups/upssched/ | |
| sudo chown -R `whoami`:nobody /var/state/ups | |
| sudo chmod 775 /var/state/ups/ | |
| sudo chmod 664 /var/state/ups/* | |
| sudo chmod 775 /var/state/ups/upssched/ | |
| sudo chmod 640 /usr/local/Cellar/nut/2.7.4/etc/* | |
| sudo chmod o+r /usr/local/Cellar/nut/2.7.4/etc/upssched.conf | |
| cd /usr/local/Cellar/nut/2.7.4/etc/ | |
| mv nut.conf.sample nut.conf | |
| vi nut.conf | |
| MODE=netclient | |
| :wq | |
| mv upsmon.conf.sample upsmon.conf | |
| vi uspmon.conf | |
| # Set your remote server host and password | |
| MONITOR ups@remotehost:3493 1 upsmon remotepassword slave | |
| # Set your admin user that will execute the shutdown script | |
| SHUTDOWNCMD "sudo -u localuser osascript /usr/local/Cellar/nut/2.7.4/bin/shutdown.applescript" | |
| NOTIFYCMD "/usr/local/sbin/upssched" | |
| 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" | |
| 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" | |
| NOTIFYMSG NOPARENT "upsmon parent process died - shutdown impossible" | |
| NOTIFYFLAG ONLINE SYSLOG+EXEC+WALL | |
| NOTIFYFLAG ONBATT SYSLOG+EXEC+WALL | |
| NOTIFYFLAG LOWBATT SYSLOG+EXEC+WALL | |
| NOTIFYFLAG FSD SYSLOG+EXEC+WALL | |
| NOTIFYFLAG COMMOK SYSLOG+EXEC+WALL | |
| NOTIFYFLAG COMMBAD SYSLOG+EXEC+WALL | |
| NOTIFYFLAG SHUTDOWN SYSLOG+EXEC+WALL | |
| NOTIFYFLAG REPLBATT SYSLOG+EXEC+WALL | |
| NOTIFYFLAG NOCOMM SYSLOG+EXEC+WALL | |
| NOTIFYFLAG NOPARENT SYSLOG+EXEC+WALL | |
| :wq | |
| #create LaunchDaemon that will fire up upsmon | |
| sudo vi /Library/LaunchDaemons/org.networkupstools.upsmon.plist | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>KeepAlive</key> | |
| <true/> | |
| <key>Label</key> | |
| <string>org.networkupstools.upsmon</string> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>/usr/local/sbin/upsmon</string> | |
| <string>-D</string> | |
| </array> | |
| <key>RunAtLoad</key> | |
| <true/> | |
| <key>StandardOutPath</key> | |
| <string>/Library/Logs/upsmon.log</string> | |
| <key>StandardErrorPath</key> | |
| <string>/Library/Logs/upsmon.log</string> | |
| </dict> | |
| </plist> | |
| :wq | |
| #launch daemon | |
| sudo launchctl load /Library/LaunchDaemons/org.networkupstools.upsmon.plist | |
| cd /usr/local/Cellar/nut/2.7.4/etc/ | |
| mv upssched.conf.sample upssched.conf | |
| #configuring for shutdown after 2min | |
| vi upssched.conf | |
| CMDSCRIPT /usr/local/Cellar/nut/2.7.3/bin/upssched-cmd | |
| PIPEFN /var/state/ups/upssched/upssched.pipe | |
| LOCKFN /var/state/ups/upssched/upssched.lock | |
| AT ONLINE * EXECUTE ONLINE | |
| AT ONLINE * CANCEL-TIMER SHUTDOWN | |
| AT ONBATT * EXECUTE ONBATT | |
| AT ONBATT * START-TIMER SHUTDOWN 120 | |
| AT LOWBATT * EXECUTE LOWBATT | |
| AT LOWBATT * START-TIMER FSD 5 | |
| AT FSD * EXECUTE FSD | |
| AT COMMOK * EXECUTE COMMOK | |
| AT COMMBAD * EXECUTE COMMBAD | |
| AT SHUTDOWN * EXECUTE SHUTDOWN | |
| AT REPLBATT * EXECUTE REPLBATT | |
| AT NOCOMM * EXECUTE NOCOMM | |
| AT NOPARENT * EXECUTE NOPARENT | |
| :wq | |
| vi uspmon.conf | |
| #unfortunately, we can't use SHUTDOWNCMD "/usr/bin/osascript -e 'tell app \"loginwindow\" to «event aevtrsdn»'" | |
| #because of angle brakets (upsmon filtering them), so we'll create shutdown.applescript | |
| #but if you don't need additional time for cancel shutdown - use second SHUTDOWNCMD | |
| SHUTDOWNCMD "sudo -u Username /usr/local/Cellar/nut/2.7.3/bin/shutdown.applescript" | |
| #SHUTDOWNCMD "/usr/bin/osascript -e 'tell app \"System Events\" to shut down'" | |
| NOTIFYCMD "/usr/local/sbin/upssched" | |
| 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" | |
| 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" | |
| NOTIFYMSG NOPARENT "upsmon parent process died - shutdown impossible" | |
| NOTIFYFLAG ONLINE SYSLOG+EXEC | |
| NOTIFYFLAG ONBATT SYSLOG+EXEC | |
| NOTIFYFLAG LOWBATT SYSLOG+EXEC | |
| NOTIFYFLAG FSD SYSLOG+EXEC | |
| NOTIFYFLAG COMMOK SYSLOG+EXEC | |
| NOTIFYFLAG COMMBAD SYSLOG+EXEC | |
| NOTIFYFLAG SHUTDOWN SYSLOG+EXEC | |
| NOTIFYFLAG REPLBATT SYSLOG+EXEC | |
| NOTIFYFLAG NOCOMM SYSLOG+EXEC | |
| NOTIFYFLAG NOPARENT SYSLOG+EXEC | |
| :wq | |
| vi /usr/local/Cellar/nut/2.7.3/bin/shutdown.applescript | |
| #!/usr/bin/osascript | |
| tell application "loginwindow" to «event aevtrsdn» | |
| :wq | |
| vi /usr/local/Cellar/nut/2.7.3/bin/upssched-cmd | |
| case $1 in | |
| ONLINE) | |
| MSG="UPS on line power" | |
| upscmd -u Username -p Password upsname@localhost shutdown.stop | |
| ;; | |
| ONBATT) | |
| MSG="UPS on battery" | |
| ;; | |
| LOWBATT) | |
| MSG="UPS battery is low" | |
| ;; | |
| FSD) | |
| MSG="UPS forced shutdown in progress" | |
| /usr/local/sbin/upsmon -c fsd | |
| ;; | |
| COMMOK) | |
| MSG="Communications with UPS established" | |
| ;; | |
| COMMBAD) | |
| MSG="Communications with UPS lost" | |
| ;; | |
| SHUTDOWN) | |
| MSG="Auto logout and shutdown proceeding" | |
| upscmd -u User -p pass upsname@localhost shutdown.return | |
| ;; | |
| REPLBATT) | |
| MSG="UPS battery needs to be replaced" | |
| ;; | |
| NOCOMM) | |
| MSG="UPS is unavailable" | |
| ;; | |
| NOPARENT) | |
| MSG="upsmon parent process died - shutdown impossible" | |
| ;; | |
| *) | |
| MSG="Unrecognized command: $1" | |
| ;; | |
| esac | |
| :wq | |
| #it's done | |
| #check status in Console.app, under /Library/Logs | |
| #to debug | |
| #change debug level from -D to -DDD at least - <string>-DDD</string> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>KeepAlive</key> | |
| <true/> | |
| <key>Label</key> | |
| <string>org.networkupstools.upsmon</string> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>/usr/local/sbin/upsmon</string> | |
| <string>-D</string> | |
| </array> | |
| <key>RunAtLoad</key> | |
| <true/> | |
| <key>StandardOutPath</key> | |
| <string>/Library/Logs/upsmon.log</string> | |
| <key>StandardErrorPath</key> | |
| <string>/Library/Logs/upsmon.log</string> | |
| </dict> | |
| </plist> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/osascript | |
| tell application "loginwindow" to «event aevtrsdn» |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case $1 in | |
| ONLINE) | |
| MSG="UPS on line power" | |
| upscmd -u Username -p Password upsname@localhost shutdown.stop | |
| ;; | |
| ONBATT) | |
| MSG="UPS on battery" | |
| ;; | |
| LOWBATT) | |
| MSG="UPS battery is low" | |
| ;; | |
| FSD) | |
| MSG="UPS forced shutdown in progress" | |
| /usr/local/sbin/upsmon -c fsd | |
| ;; | |
| COMMOK) | |
| MSG="Communications with UPS established" | |
| ;; | |
| COMMBAD) | |
| MSG="Communications with UPS lost" | |
| ;; | |
| SHUTDOWN) | |
| MSG="Auto logout and shutdown proceeding" | |
| upscmd -u Username -p Password upsname@localhost shutdown.return | |
| ;; | |
| REPLBATT) | |
| MSG="UPS battery needs to be replaced" | |
| ;; | |
| NOCOMM) | |
| MSG="UPS is unavailable" | |
| ;; | |
| NOPARENT) | |
| MSG="upsmon parent process died - shutdown impossible" | |
| ;; | |
| *) | |
| MSG="Unrecognized command: $1" | |
| ;; | |
| esac | |
| /usr/local/bin/growlnotify -s -n "UPSNAME" -m "$MSG" -P "PA$$W0RD" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment