Welcome to Server Circle. It's a friendly site and all levels of experience are welcome. Be aware that we use cookies for your login.
Server Circle - Ask questions about Servers and get answers from experts.
Beta (0.30 sec)
REWARD: Bash script help please

I need a script to check temperature in a server and then email me if it gets past 50 degrees celcius. Using a hard drive's temperature would be fine I think.

I've tried writing my own script using hddtemp but I'm no good at Bash.

A £5 Amazon voucher is up for grabs for helping me out. Thanks.
Asked by:
poppy
1375 points
 Report Abuse
 Share Page - Category: Shell Scripting - Tags: REWARD: Bash script help please
 Enter your response
Please use Pastie.org to paste lengthy code or to fix formatting issues with code
  • Responses in reverse (5)

Best Answer

You state you wish to check the servers temperature but using hddtemp would be "fine". Sounds like you'd rather get your data from somewhere else.

I suggest using lm-sensors which will most likely by available to you via your own distribution repository. For example, to install it under Debian/Ubuntu: sudo apt-get install lm-sensors

The use of lm-sensors will allow your bash script to be portable amongst different distributions and releases. It gives you the flexibility to pick and choose what sensors you wish to monitor.

Once installed, open/shh into terminal and execute the command sensors. This will list all the sensors installed on your system, each with a name and a reading, choose which one you wish to monitor and replace SENSORNAME with the sensor name in the bash script below:



#!/bin/bash

# Email an alert should be sent to in the event the reading exceeds the trigger

email=your@email.com

# Trigger amount in Celsuis

temp_trigger=50

# Extract the reading of our desired sensor using the 'sensors' command

temp=$(sensors|grep 'SENSORNAME' | awk '{print $2}' | cut -b2,3,4,5)

# Round our temperature reading to the nearest whole number

rounded_temp=$(echo $temp | awk '{printf("%d\n",$1 + 0.5)}')

# Compare the temperature reading with our alert trigger

if [ $rounded_temp -ge $temp_trigger ]; then

# Send an email alerting the admin!

echo "Temperature is $temp Celsius" | mailx -s "SERVER TEMP TRIGGER" $email

else

# All is okay :)

echo "Temperature is $temp Celsius"

fi



You can have as many of these scripts as you want to monitor any sensor you wish. If the readings from sensors don't come out in the format of: +65.0°C . The above code may need to tweaked. If so, you can just email me and I will change it for you: smasterross [at] gmail [dot] com

Response by:
stevenrossuk
1325 points
For disk, see the following example

hddtemp /dev/sda | cut -f3 -d" "

output is

50°C

Response by:
jalal
3623 points
Thank you to all three people who answered. I was really struggling with who to reward the voucher to but the answer with the most detail won the day. The voucher is going to stevenrossuk.

Response by:
poppy
1375 points
This variation avoids leaving (two) temporary files lying around:



#!/bin/bash

my_email=my@email.address

alert_temp=50

tempC=$(hddtemp -q /dev/sda | awk '{ print 0+$3 }')

if [ $tempC -ge $alert_temp ]; then

echo "Temperature is $tempC Celsius" | mailx -s "ALARM" $my_email

elif [ -t 0 ]; then

echo Temperature is $tempC Celsius

fi


Response by:
gedge
690 points
Something like this should do it, if hddtemp is installed:

#!/bin/bash

value=0;

hddtemp -q /dev/sda > /tmp/.check_temp; value=`awk '{ print $3 }' /tmp/.check_temp | sed -e ' s/°C//'`;

if [ $value -gt 49 ]

then echo "Temperature is $value degrees C " > /tmp/mail_file; mailx -s "ALARM" mail@domain.com < /tmp/mail.file

else exit 0

fi

Response by:
JimmyC
945 points


  • Related Questions
About Us : Contact Us : Etiquette : Terms : CDN Failover : ShorterURL : CDN Fallback : © 2013 Server Circle