Syed Jahanzaib Personal Blog to Share Knowledge !

August 8, 2016

UPS Monitor BASH Script with Multi Matching ‘IF Statements’

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) AND the remaining backup time goes below our defined threshold, then it should send email + sms alert to the admin. So basically we will match at least two conditions.



#!/bin/bash
# Function: DATA CENTER UPS KE INPUT and VOLTAGE ALERT SCRIPT
# Scheduled Script to check data center KE INPUt + UPS Backup remain time via SNMP after every 1 mnt.
# If it found lower than our defined limit, send sms or email Alerts, but do not repeat it until next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Created : 8th-Aug-2016  @ AGP
# 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="ZABBO Pvt Ltd."
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="KANNEL-URL:13013"
KANNELID="kannel"
KANNELPASS="KANNEL-PASS"
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOURMGAILID@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="aacable@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/upsmon.txt"

# UPS IP / SNMP RELATED INFO
UPSIP="$1"
SNMPSTR="public"
# OID For Emerson rack base UPS 10kvs with snmp web card
UPSINPUTOID="1.3.6.1.4.1.13400.2.16.2.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.13400.2.16.2.5.2.0"

# Minimum Backup time threshold limit, below this action can be triggered if matched with second trigger
UPSTIMELIMIT="30"
# Minimum KE INput electric voltages limit, below this , both condition can be matched.
KELIMIT="190"

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

# Check if SNMP query is accessible or not, if not then EXIT immediately
SNMPRESULT="/tmp/snmpoutput.txt"
touch $SNMPRESULT
snmpwalk -v1 -c agp $1 $UPSINPUTOID > $SNMPRESULT
if [ ! -s $SNMPRESULT ]; then
echo "ALERT ALERT: UPS IP $UPSIP - SNMP NOT Responding"
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 -v2c -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
# divide time formula which comes like 2100 , so divide with /100 so we get 21 actually, just an example
KE=$(($KEINPUT / 100))

# Message template for up n down alerts
LOWMSG="$COMPANY Data Center UPS K.E Input have failed.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts
Please check urgent"

OKMSG="$COMPANY Data Center UPS KESC Input have restored.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts"

NORMSG="All Seems OK !"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"

# SMS and email msg fromat for up n down
MSG_UP="$COMPANY Alert:

$OKMSG @ $DATE
$FOOTER"

LOWMSG="$COMPANY Alert:

$LOWMSG @ $DATE
$FOOTER"

# Print Current fetched Values
echo -e "
UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $COL_GREEN $UPSTIMELIMIT mnts $COL_RESET
Minimum KE Input Threshold = $COL_GREEN $KELIMIT volts $COL_RESET
Current Backup Time = $COL_RED $TIMEQ mnts $COL_RESET
Current K.E Input = $COL_RED $KE volts $COL_RESET"

# Check if ke failure alert have already been sent, if yes then inform accordingly.
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
echo -e "$COL_RED $LOWMSG $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED SMS/Email for DOWN have already been sent $COL_RESET"
fi
fi

# Matching Formula starts here .. zaib
# IF KE INPUT result is less than our defined limit and remaining time is low as well, , 
# then send sms and email, IF NOT ALREADY SENT
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "$COL_RED ALERT: $LOWMS $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$LOWMSG" > $DOWNMSG

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

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$LOWMSG @ $DATE" -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

echo "TEMP" > $STATUS_HOLDER
fi
else
echo -e "$COL_GREEN $NORMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $OKMSG $(date) / SENDING OK SMS/Email .... $COL_RESET"
echo "$OKMSG" > $UPMSG

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

# Sending UP / OK Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$OKMSG @ $DATE" -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
sed -i "/TEMP/d" "$STATUS_HOLDER"
fi
fi

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


 

failure

 


Second Version / uPDATED 16-8-2016  12:05pm


#!/bin/bash
#set -x

# Function: DATA CENTER UPS KE INPUT and VOLTAGE ALERT SCRIPT
# Scheduled Script to check data center KE INPUt + UPS Bacnkup remain time via SNMP after every 1 mnt.
# If it found high or above our defined limit, 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
# 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="*AGP*"
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."
TEMPWORD="UPSMON"

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="KANNEL-USER"
KANNELPASS="KANNEL-PASS"
CELL1="03333XXXXXX"

# GMAIL DETAILS
GMAILID="YOURGMAIL@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="TO-1@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/upsmon.txt"

# UPS RELATED INFO
UPSIP="$1"
SNMPSTR="public"
UPSINPUTOID="1.3.6.1.4.1.13400.2.16.2.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.13400.2.16.2.5.2.0"
UPSTIMELIMIT="10"
KELIMIT="180"

# Check if UPS is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... UPS IP $UPSIP is DOWN ..."
exit 1
fi
SNMPRESULT="/tmp/snmpoutput.txt"
touch $SNMPRESULT
snmpwalk -v1 -c agp $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 -v2c -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
KE=$(($KEINPUT / 100))

# divide time formula which comes like 2100 , so divide with /100 so we get 21 actuall, just an example
LOWMSG="UPSMON - $COMPANY UPS CRITICAL ALERT: Data Center UPS working on batteries @$DATE & remainging timings are $TIMEQ mnts.

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
###############################################################
FINAL RESULT = ALERT / K.E failed. UPS working on Batteries. #
###############################################################

$FOOTER"

OKMSG="UPSMON - $COMPANY Data Center UPS K.E Input have restored @ $DATE

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

#################################################################
FINAL RESULT = OK now / K.E restored. UPS working on K.E Input #
#################################################################

$FOOTER"

NORMSG="All Seems OK @ $DATE !"
UPMSG="/tmp/upmsg.msg"
DOWNMSG="/tmp/downmsg.msg"

UPMSG_SMS="/tmp/upmsg.sms"
DOWNMSG_SMS="/tmp/downmsg.sms"
# SMS and email msg fromat for up n down
MSG_UP_SUB="UPSMON - $COMPANY UPS Info: Data Center UPS K.E restored @ $DATE"
MSG_DOWN_SUB="UPSMON - $COMPANY UPS CRITICAL Alert: UPS working on batteries @$DATE & remainging timings are $TIMEQ mnts."
# Print Ok or ALERT Values according to the following formula
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
# PRINT FAILURE MESSAGE BY ZAIB ---------
echo "
######################################################################
UPSMON - Script to test KE. Input and battery remaining time @$DATE. #
######################################################################

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

###############################################################
FINAL RESULT = ALERT / K.E failed. UPS working on Batteries. #
###############################################################

$FOOTER"
else
echo "
######################################################################
UPSMON - Script to test KE. Input and battery remaining time @$DATE. #
######################################################################

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

#########################
FINAL RESULT = ALL OK. #
#########################

$FOOTER"
fi

# Matching Formula starts here .. zaib
# IF KE INPUT result is greater the our defined limit, then give alert
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
echo ""
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 1 ]; then
echo "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 [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "$KELIMIT" ]]; then
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 0 ]; then
echo "
$MSG_DOWN_SUB / SENDING SMS/Email ....
"
echo "$LOWMSG" > $DOWNMSG
echo "$MSG_DOWN_SUB" > $DOWNMSG_SMS

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

# 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

echo "$TEMPWORD" > $STATUS_HOLDER
fi
else
# echo -e "$NORMSG ..."
echo -e ""
if [ $(grep -c "$TEMPWORD" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "
$MSG_UP_SUB / SENDING OK SMS/Email ....
"
echo "$OKMSG" > $UPMSG
echo "$MSG_UP_SUB" > $UPMSG_SMS
# Sending UP SMS via KANNEL
cat $UPMSG_SMS | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# 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

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


 

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: