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 built in 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:
- Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]
- Ubuntu 12.4
- SendEmail application to send email alert via GMAIL account
- 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 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 ######################## # set Temperaturte limit ######################## TEMPLIMIT="25" # 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.2" UPS2IP="10.0.0.3" UPSTEMPR="1.3.6.1.4.1.13400.2.62.2.1.2.0" UPSHUMIDITY="1.3.6.1.4.1.13400.2.62.2.1.3.0" SNMPCOM="XXX" # Check if UPS is accessible or not, if not then use 2nd UPS IP, since we have 2 ups so we will sue 2nd one (like failover) echo "Checking UPS interface ping ..." if [[ $(ping -q -c 2 $UPSIP) == @(*100% packet loss*) ]]; then echo "ALERT ! UPS IP $UPSIP is DOWN ..." UPSIP="$UPSIP2" #exit 1 else echo -e "UPS Ping $COL_GREEN OK $COL_RESET ." fi # COMPANY NAME COMPANY="XXX" FOOTER="Scripting by XXX IS Dept./Syed Jahanzaib" # Hostname HOSTNAME=`hostname` # KANNEL SMS Gateway Info KANNELURL="xxxx.xxxx.net:13013" KANNELID="kannel" KANNELPASS="xxxx" # Jahanzaib Cell CELL1="03333021909" # GMAIL DETAILS GMAILID="XXXmonitor@gmail.com" GMAILPASS="xxxXXX" TO1="aacableAThotmail.com" SENDMAILAPP="sendemail" #/usr/bin/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 # 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 $UPSTEMPR` HUMIDITY=`snmpwalk -v2c -Oqv -c $SNMPCOM $UPSIP $UPSHUMIDITY` # 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)) HUMIDFINAL=$(($HUMIDITY / 100)) # Recording the entries in local text file HIGHMSG="Data Center Temperature is above $TEMPFINAL c / Humidity = $HUMIDFINAL % @$DATE" OKMSG="Data Center Temperature is OK below $TEMPFINAL c / Humidity = $HUMIDFINAL % @$DATE" UPMSG="/tmp/upmsg.sms" DOWNMSG="/tmp/downmsg.sms" # SMS and email msg fromat for up n down MSG_UP="$COMPANY NOC Temperature Info: $OKMSG $DATE $FOOTER" MSG_DOWN="$COMPANY NOC Temperature 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 / Humidity = $HUMIDFINAL %$COL_RESET" else echo -e "Current Temperature = $COL_GREEN $TEMPFINAL c / Humidity = $HUMIDFINAL %$COL_RESET" fi # Matching 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 "HIGH TEMP ALERT: $TODAY $TIME = $TEMPFINAL" >> $LOGFILE 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 $TO1 -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 "NORMAL TEMP ALERT: $TODAY $TIME = $TEMPFINAL" >> $LOGFILE echo "$MSG_UP" > $UPMSG # Sending UP SMS via KANNEL #cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1+$CELL4+$CELL5" -G --data-urlencode text@- # Sending Email via sendEmail tool app using GMAIL $SENDMAILAPP -u "$OKMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $TO1 -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
CronTAB !
# Monitor Data Center Temperature and send email/sms alerts */1 * * * * /temp/noctempr.sh >/dev/null 2>&1
Result:
- High Temperature Alert Email Sample:
Low Temperature Alert Email Sample: