Syed Jahanzaib Personal Blog to Share Knowledge !

August 8, 2016

UPS Monitor BASH Script with SMS / Email Alerts

Filed under: Linux Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 1:26 PM

Following script is just For my personal reference Purpose only!!!

 

script ups output

Emerson UPS Info via DUDE

kesc

 


Scenario:

In our datacenter , we have Emerson 10kva UPS x 2 in failover + load balancing mode. Yesterday we got some electric failure resulting in UPS shutdown after its batteries banks got drained. This UPS doesn’t have advance alerting system other than simple traditional alerts of light failure and restore via email. Therefore I made some customized scripts which are now monitoring the UPS status with more deeper digging.

Example: If the Electricity failure occurs (or low voltage under xxx value) then it should send email + sms alert to the admin. So basically we will match at least two conditions.

In this script we have use following software hardware

  • Ubuntu 12.4 Server Edition with this bash script
  • Network Support UPS with SNMP card
  • Kannel SMS gateway in same ubuntu box for sending SMS using locally attached GSMmodem
  • Sendemail application for sending emails alerts (Its not SENDMAIL server, its small app name sendemail which we can use to send email via Gmail as well, it comes handy 🙂 )

Before proceeding, make sure that

  • You can ping the UP ip properly,
  • You can query the UPS via SNMP query like snmpwalk -v1 -c PUBLIC UPSIP
  • You can query UPS input OID and get result like 220 (or if you get result like 2200 use the formula in the script in later section)


#!/bin/bash
# Function: DATA CENTER UPS KE INPUT ONLY - SCRIPT [KE is the company name wihch provides electricity in Karcahi city - Karachi Electric
# Scheduled Script to check data center KE INPUt + UPS Bacnkup remain time via SNMP after every 1 mnt or as per scheudled cron job.
# If it found no input , it wll send sms or email Alerts, but donot repeat it untill next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Cration Date: 2015
# Last revision DAte: 7-June-2018]
#set -x
# Colors Config . . . [[ JZ . . . ]]

ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# COMPANY NAME
COMPANY="ZAIB-LTD"
FOOTER="Powered by Syed Jahanzaib."

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="KANNEL-PASSWORD"
CELL1="03333021909"
SENTSMSRESULT="/temp/kemonsentsmsresult.txt"
LOCATION="FSD"

# GMAIL DETAILS
GMAILID="GMAIL-ID@gmail.com"
GMAILPASS="GMAIL-PASS"
ADMINMAIL1="YOUREMAIL@hotmail.com"

# to install sendemail, use apt-get install sendemail
SENDMAILAPP="sendemail"
STATUS_HOLDER="/temp/upskemon.txt"
SNMPRESULT="/temp/kesnmpoutput.txt"
touch $SNMPRESULT

# UPS RELATED INFO
UPSIP="$1"
SNMPSTR="PUBLIC"

# For APC SMART 1500 UPS Model, You must change them as per your version
UPSINPUTOID="1.3.6.1.4.1.318.1.1.1.3.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.318.1.1.1.2.2.1.0"
UPSTIMELIMIT="30"
KELIMIT="150"

# Check if UPS is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 3 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... UPS IP $UPSIP is DOWN ..."
exit 1
fi

snmpwalk -v1 -c $SNMPSTR $1 $UPSINPUTOID > $SNMPRESULT
if [ ! -s $SNMPRESULT ]; then
echo "ALERT ALERT: UPS IP $UPSIP - SNMP NOT Repsonding"
exit 1
fi

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

# Check for KE INPUT via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TIMEQ=`snmpwalk -v1 -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v1 -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
#KE=$(($KEINPUT / 100))
KE="$KEINPUT"

NORMSG="All Seems OK !
$FOOTER"
UPMSG="/temp/upmsg.sms"
DOWNMSG="/temp/downmsg.sms"

# MESSAGES
# SMS and email msg fromat for up n down
MSG_UP_SUB="$COMPANY - $LOCATION - UPS INPUT restored @ $DATE"
MSG_DOWN_SUB="$COMPANY - $LOCATION - UPS INPUT failed @ $DATE"
LOWMSG="$COMPANY - $LOCATION - UPS Info: UPS Voltage Input failed @ $DATE
Current Volate Input = $KE volts

$FOOTER"
OKMSG="$COMPANY - $LOCATION - UPS Info: Voltage Input restored @ $DATE
Current Voltage Input = $KE volts

$FOOTER"

# Print Values
echo -e "$COMPANY - $LOCATION - SCRIPT TO MONITOR ELECTRIC INPUT VOLTAGE in UPS ONLY *******
UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $UPSTIMELIMIT mnts
Minimum KE Input Threshold = $KELIMIT volts
Current Backup Time = $TIMEQ mnts
Current K.E Input = $KE volts

"

# Matching Formula starts here .. zaib
# IF KE INPUT result is greater the our defined limit, then give alert
if [ "$KE" -lt "$KELIMIT" ]; then
echo -e "$LOWMSG "
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "SMS/Email for DOWN have already been sent "
fi
fi

# IF KE INPUT result is less then our defined limit, then send sms and email, IF NOT ALREAY SENT
if [ "$KE" -lt "$KELIMIT" ]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "ALERT: $LOWMS $(date) / SENDING SMS/Email .... "
echo "$LOWMSG" > $DOWNMSG
echo "TEMP" > $STATUS_HOLDER

# Sending DOWN SMS via KANNEL
echo $DATE > $SENTSMSRESULT
cat $DOWNMSG | curl "http://$KURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- >> $SENTSMSRESULT

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$MSG_DOWN_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text
fi
else
echo -e "$NORMSG ... "
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COMPANY ALERT : $OKMSG $(date) / SENDING OK SMS/Email .... "
echo "$OKMSG" > $UPMSG
sed -i "/TEMP/d" "$STATUS_HOLDER"

# Sending UP SMS via KANNEL
echo $DATE > $SENTSMSRESULT
cat $UPMSG | curl "http://$KURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@- >> $SENTSMSRESULT

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$MSG_UP_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text
fi
fi

# Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com


TEST RUN !

Now execute the script like

/temp/kemon.sh 192.168.0.1

x.JPG

 


Regard’s

Syed Jahanzaib

 

Advertisements

3 Comments »

  1. Hi this is great I really enjoy your blog and the great work you do. I am currently setting up freeradius with an external mysql db. I am however experiencing some issue with authentication on Mikrotik. When using Ntradping it works however on mikrotik no. your help will be highly appreciated.

    Like

    Comment by Ian Kasyoki — August 9, 2016 @ 7:57 PM

  2. is there any thing for p2p caching and http caching

    Like

    Comment by s.m.b sri balaji balaji — August 21, 2016 @ 8:34 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: