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 10kva UPS with web snmp card installed. We can monitor the temperature via manageable ups builtin feature as well, but I required some customization so I enabled the SNMP feature in ups web card and made following Linux bash script to do the tasks. I am sharing it for general public as an example.


Hardware/Software Used:

  1. Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]
  2. Ubuntu 12.4
  3. SendEmail application to send email alter via GMAIL account
  4. Kannel as SMS gateway to send sms alert

Following is an example code of SNMP query of temperature monitor from Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]. First we will inquire the UPS by snmpwalk query to confirm the OID and response.

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
#Result could be something like below as we need only the numerical value of temperature
2070

THE sCRIPT !


#!/bin/bash
# Function: DATA CENTER TEMPERATURE ALERT SCRIPT
# Scheduled Script to check data center temperature via SNMP after every 5 minutes.
# In this example we are using Emerson 10kva UPS with web snmp card attached to the network.
# If it found temperature above out defined threshold , it will send sms or email Alerts,
# but it iwll not repeat the sending alert untill next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Created : 2015
# Last modified : July, 2017
#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`
TODAY=$(date +"%Y-%m-%d")
TIME=$(date +"%H:%M:%S")

# Variables for UPS IP / OID for tmeprature / SNMP community string
UPSIP="10.0.0.1"
UPSOID="1.3.6.1.4.1.13400.2.62.2.1.2.0"
SNMPCOM="PUBLIC"

# Check if UPS is accessibel or not, if not then EXIT immediately with error / zaib
echo "Checking UPS interface ping ..."
if [[ $(ping -q -c 2 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ! UPS IP $UPSIP is DOWN ..."
exit 1
else
echo -e "UPS Ping $COL_GREEN OK $COL_RESET ."
fi

 

# COMPANY NAME
COMPANY="AGP Pvt Ltd."
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="10.0.0.2:13013"
KANNELID="kannel"
KANNELPASS="KANNEL_PASSWORD"
# Jahanzaib Cell
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOUR_GMAIL_ID@gmail.com"
GMAILPASS="GMAIL_PASSWORD"
ADMINMAIL1="TO@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/datacentertemperature.txt"
LOGFILE="/temp/noctempraturelog.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

########################
# set Temperaturte limit
########################

TEMPLIMIT="26"

# 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 $SNMPCOM $UPSIP $UPSOID`

# Check that the snmp query should return only Numeric value (only temperature, it must not contain other words , else consider invalid response)
# zaib
if [[ $TEMPRATURE =~ ^[-+]?[0-9]+$ ]]; then
echo "" > /dev/null
else
echo -e "$COL_RED Unable to get any Numerical digit via SNMP Query therefore Exiting , SNMP nor working? check .... $COL_RESET"
exit 1
fi

# divide temperature formula which comes like 2100 , so divide with /100 so we get 21 actuall, just an example
TEMPFINAL=$(($TEMPRATURE / 100))

# Recording the entries in local text file

#echo "$TODAY $TIME = $TEMPFINAL" >> $LOGFILE

HIGHMSG="Data Center Temperature is HIGH, value > $TEMPFINAL c"
OKMSG="Data Center Temperature is OK , value > $TEMPFINAL c"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"

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

$OKMSG

$DATE

$FOOTER"

MSG_DOWN="$COMPANY Alert:

$HIGHMSG

$DATE

$FOOTER"

# Print Values
echo -e "Maximum Temperature Allowed = $COL_GREEN $TEMPLIMIT c $COL_RESET"
#Current Temperature = $COL_RED $TEMPFINAL c$COL_RESET"

if [ "$TEMPFINAL" -gt "$TEMPLIMIT" ]; then
echo -e "Current Temperature = $COL_RED $TEMPFINAL c$COL_RESET"
else
echo -e "Current Temperature = $COL_GREEN $TEMPFINAL c$COL_RESET"

fi

 

# 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
sendemail -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 $OKMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $HEADING $OKMSG $(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
sendemail -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

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


Result:

noc tempr command result.JPG

 

  • High Temperature Alert Email Sample:

mailhigh

 

Low Temperature Alert Email Sample:

lowtemp


SMS RESULT:

2016-07-30 06.21.20

Advertisements

%d bloggers like this: