Create a gist now

Instantly share code, notes, and snippets.

Example Network UPS Tool (NUT) configs
#!/usr/bin/python
import os
import sys
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64
def sendMail(subject, text):
gmailUser = 'sender@example.com'
gmailPassword = 'pa55Word'
recipient = 'recipient@example.org'
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print'Sent email to %s' % recipient
sendMail(sys.argv[1], sys.argv[2])
MONITOR PowerWalker2200@localhost "Garage UPS"
MODE=netserver
[PowerWalker2200]
driver = blazer_usb
port = auto
LISTEN 0.0.0.0
[ups_admin]
password = pa55Word
upsmon master
MONITOR PowerWalker2200@_server_IP_ 1 ups_admin pa55Word master
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD /sbin/upssched
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
case $1 in
ups-on-batt)
gmail.py "Garage has lost power" "Running on battery"
;;
onbattwarn)
gmail.py "Garage power still down after 30s" "Running on battery"
;;
ups-back-on-power)
gmail.py "Garage power restored" "Running on mains again"
;;
*)
logger -t upssched-cmd "Unrecognized command: $1"
;;
esac
CMDSCRIPT /bin/upssched-cmd
PIPEFN /var/run/nut/upssched/upssched.pipe
LOCKFN /var/run/nut/upssched/upssched.lock
AT ONBATT * EXECUTE ups-on-batt
AT ONBATT * START-TIMER onbattwarn 30
AT ONLINE * CANCEL-TIMER onbattwarn
AT ONLINE * EXECUTE ups-back-on-power
@cpswan
Owner
cpswan commented Jan 22, 2017

To use in anger:

Replace instances of pa55Word with a real password of your choosing.

Put the actual IP of your server running NUT into _server_IP_ in upsmon.conf

Substitute these lines in gmail.py with real values for the account you're using to send and the address you want to get alerts from.

gmailUser = 'sender@example.com'
gmailPassword = 'pa55Word'
recipient = 'recipient@example.org'

Also note that you'll have to jump through some hoops with the sending gmail account to allow it to be used this way (as gmail has tightened up security defaults).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment