Syed Jahanzaib Personal Blog to Share Knowledge !

July 30, 2016

Data Center Temperature Monitoring via BASH Script

Filed under: Linux Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 11:29 AM

2016-07-30 06.21.20


Following is an bash script to monitor data center temperature via SNMP query from Emerson UPS. We can monitor the temperature via manageable ups builtin feature as well, but since i was required to do some extra functions which were not supported by the ups web panel, so i made following. although i removed few functions, but sharing it for general public as an example.

Following is an example code of SNMP query of temperature monitor from Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]

root@linux:/temp# snmpwalk -v2c -Oqv -c public  10.0.0.2  1.3.6.1.4.1.13400.2.62.2.1.2.0

2070

#!/bin/bash
# Function: DATA CENTER TEMPERATURE ALERT SCRIPT
# Scheduled Script to check data center temperature via SNMP after every 5 minutes.
# 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
#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"

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="10.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="kannelpass"
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOURGMAILID@gmail.com"
GMAILPASS="YOURGMAIL-PASS"
ADMINMAIL1="ADMIN-MAIL@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/datacentertemperature.txt"

# 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

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

$LOWMSG $DATE
$FOOTER"

MSG_DOWN="$COMPANY Alert:

$HIGHMSG @ $DATE
$FOOTER"

# UPSIP , example i m using is emerson UPS
UPSIP="10.0.0.2"
UPSTEMPOID="1.3.6.1.4.1.13400.2.62.2.1.2.0"
UPSSNMPCOM=public"

# set Temperaturte limit
TEMPLIMIT="24"

# Check for temperature via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TEMPRATURE=`snmpwalk -v2c -Oqv -c $UPSSNMPCOM $UPSIP $UPSTEMPOID`
# divide temperature formula which comes like 2100 , so divide with /100 so we get 21 actuall, just an example
TEMPFINAL=$(($TEMPRATURE / 100))

HIGHMSG="$COMPANY Data Center Temperature is HIGH, that is $TEMPFINAL"
LOWMSG="$COMPANY Data Center Temperature is OK , that is $TEMPFINAL"
FOOTER="Powered by Syed Jahanzaib"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"
# Print Values
echo -e "Maximum Temperature Allowed = $COL_GREEN $TEMPLIMIT c $COL_RESET
Current Temperature = $COL_RED $TEMPFINAL c$COL_RESET"

# Matchign Formula starts here .. zaib
# IF temperature result is greater the our defined limit, then give alert
if [ "$TEMPFINAL" -gt "$TEMPLIMIT" ]; then
echo -e "$COL_RED $HIGHMSG $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED SMS/Email have already been sent $COL_RESET"
fi
fi

# IF temperature result is greater the our defined limit, then send sms and email, IF NOT ALREAY SENT
if [ "$TEMPFINAL" -gt "$TEMPLIMIT" ]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "$COL_RED ALERT: $HIGHMSG $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$MSG_DOWN" > $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 "$HIGHMSG @ $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 $LOWMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $HEADING $LOWMSG $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$MSG_UP" > $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 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=$UPMSG -o message-content-type=text
sed -i "/TEMP/d" "$STATUS_HOLDER"
fi
fi

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

alert


Result:

High Temperature Alert Email Sample:

mailhigh

 

Low Temperature Alert Email Sample:

lowtemp


SMS RESULT:

2016-07-30 06.21.20

Create a free website or blog at WordPress.com.