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:
- Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]
- Ubuntu 12.4
- SendEmail application to send email alter 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 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 : Novermber, 2017 # To debug, un-comment following #set -x #################### # set Temperature 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.1" # Temprature OID UPSTEMPR="1.3.6.1.4.1.13400.2.62.2.1.2.0" # Humidity OID UPSHUMIDITY="1.3.6.1.4.1.13400.2.62.2.1.3.0" #Snmp Community string 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="ZAIBOOOO" FOOTER="Scripting by IT Dept." # Hostname HOSTNAME=`hostname` # KANNEL SMS Gateway Info KANNELURL="10.0.0.10:13013" KANNELID="kannel" KANNELPASS="kannel_pass" # Jahanzaib Cell CELL1="03333021909" # GMAIL DETAILS GMAILID="YOURGMAIL@gmail.com" GMAILPASS="GMAIL_PASS" ADMINMAIL1="your_account_@mail.xyz" SENDMAILAPP="/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 #echo "$TODAY $TIME = $TEMPFINAL" >> $LOGFILE HIGHMSG="Data Center Temperature is above $TEMPFINAL c / Humidity = $HUMIDFINAL %" OKMSG="Data Center Temperature is now OK below $TEMPFINAL c / Humidity = $HUMIDFINAL %" 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 / Humidity = $HUMIDFINAL %$COL_RESET" else echo -e "Current Temperature = $COL_GREEN $TEMPFINAL c / Humidity = $HUMIDFINAL %$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 $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 $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 $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 # Script Ends Here # Syed Jahanzaib / aacable @ hotmail . com # http:// aacable . wordpress . com
Result:
- High Temperature Alert Email Sample:
Low Temperature Alert Email Sample:
Leave a Reply