#!/bin/bash #UPS settings HOST="10.10.100.2" COMMUNITY="public" #minimal voltage sufficient for your UPS model to not switch to batteries VOLTS_MIN=160 #minimal runtime you need to properly shutdown all VMs and Xen hosts MINUTES_MIN=15 #administrator email (better if it's crossposted to sms in case shutdown is initiated) EMAIL="admin@somedomain.tld" SNMPGET=`which snmpget 2>/dev/null` SNMPOPTS="-Ov -Oq -v1 -r10 -t2 -c ${COMMUNITY} ${HOST}" #Input voltage OID OID_VOLTS_IN=".1.3.6.1.4.1.318.1.1.1.3.2.1.0" #Battery runtime remaining OID OID_RUNTIME="1.3.6.1.4.1.318.1.1.1.2.2.3.0" VALUE_VOLTS_IN=`${SNMPGET} ${SNMPOPTS} $OID_VOLTS_IN` VALUE_RUNTIME=`${SNMPGET} ${SNMPOPTS} $OID_RUNTIME` #SNMP runtime value reported as D:H:M:S.00 #let's convert it into minutes for convenience OFS="$IFS" IFS=":" arr=( $VALUE_RUNTIME ) IFS="$OFS" DAYS=${arr[0]} HOURS=${arr[1]} MINUTES=${arr[2]} MINUTES_LEFT=$(($DAYS*24*60+$HOURS*60+$MINUTES)) if [ $VALUE_VOLTS_IN -lt $VOLTS_MIN ] then echo "Input voltage is too low ($VALUE_VOLTS_IN). $MINUTES_LEFT minutes of UPS runtime remaining." | mail -s "UPS: Power failure report" $EMAIL fi if [ $MINUTES_LEFT -lt $MINUTES_MIN ] then echo "Achtung! $MINUTES_LEFT minutes of UPS runtime remaining - shutdown initiated." | mail -s "UPS: Xenserver pool shutdown initiated." $EMAIL #let the message pass through: sleep 3 #xe_shutdown_vms_and_hosts fi