Monday, July 23, 2018

Linux Monitor Hard Disks Temperature With hddtemp

https://www.cyberciti.biz/tips/howto-monitor-hard-drive-temperature.html

last updated in Categories CentOS, Debian Linux, Hardware, Howto, Linux, Monitoring, RedHat/Fedora Linux, Suse Linux, Tips, Troubleshooting, Tuning, Ubuntu Linux
There is an excellent little utility to monitor hard drive temperature. Most modern computer hard disk including SSD comes with S.M.A.R.T (Self-Monitoring, Analysis, and Reporting Technology). It is a monitoring system for computer hard disks to detect and report on various indicators of reliability, in the hope of anticipating failures.

The hddtemp or smartctl utility will give you the temperature of your hard drive by reading data from S.M.A.R.T. on drives that support this feature.
Linux Monitor Hard Disks Temperature With hddtemp
Only modern hard drives have a temperature sensor. hddtemp supports reading S.M.A.R.T. information from SCSI drives too. hddtemp can work as a simple command line tool or as a daemon to get information from all servers.

How to install hddtemp on Linux

To install hddtemp on a a Debian / Ubuntu Linux use apt-get command/apt command:
$ sudo apt-get install hddtemp
Sample outputs:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  ksensors
The following NEW packages will be installed:
  hddtemp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 57.8 kB of archives.
After this operation, 184 kB of additional disk space will be used.
Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 hddtemp amd64 0.3-beta15-52+b1 [57.8 kB]
Fetched 57.8 kB in 2s (22.6 kB/s)  
Preconfiguring packages ...
Selecting previously unselected package hddtemp.
(Reading database ... 115096 files and directories currently installed.)
Preparing to unpack .../hddtemp_0.3-beta15-52+b1_amd64.deb ...
Unpacking hddtemp (0.3-beta15-52+b1) ...
Setting up hddtemp (0.3-beta15-52+b1) ...
Processing triggers for systemd (232-25) ...
Processing triggers for man-db (2.7.6.1-2) 
To install hddtemp under a CentOS/RHEL/SL/Oracle Linux, run yum command (first turn on EPEL repo):
$ sudo yum install hddtemp
Type the following pacman command to install hddtemp under an Arch Linux distro:
$ sudo pacman -S hddtemp
Type the following dnf command to install hddtemp under a Fedora Linux distro:
$ sudo dnf install hddtemp
Type the following zypper command to install hddtemp under an OpenSUSE Linux distro :
$ sudo zypper install hddtemp

A note about source code installation

You can also perform source code installation. Download the source code tar ball here.
$ wget http://download.savannah.nongnu.org/releases/hddtemp/hddtemp-0.3-beta15.tar.bz2
Untar and install hddtemp using the following commands:
$ tar -jxvf hddtemp-0.3-beta15.tar.bz2
$ cd hddtemp-0.3-beta15
$ ./configure
$ make
$ sudo make install

Install hard disk temperature database at /usr/share/misc or /etc directory:
$ cd /usr/share/misc
# wget http://download.savannah.nongnu.org/releases/hddtemp/hddtemp.db

Monitor Hard Disks Temperature With hddtemp

Let us see usage, syntax and examples of hddtemp command. The syntax is
hddtemp disk
hddtemp TYPE:disk
hddtemp [options] TYPE:disk

TYPE could be SATA, PATA or SCSI. If omitted hddtemp will try to guess

How do I monitor hard disk temperature on Linux?

To see temperature for the /dev/sda, enter the following command:
$ hddtemp /dev/sda
Sample outputs
/dev/sda: WDC WD2500YS-01SHB1:  25°C
Above output indicate that my hard disk temperature is 25°C. If temperature is higher than 60°C , consider cooling options immediately.

How Do I Find Out Remote Server HDD Temperature?

By default hddtemp bind to TCP/IP port 7634. You need to run hddtemp in daemon mode. Login on remote box and start it as follows to monitor /dev/sda, /dev/sdb, /dev/sdc, and /dev/sdd:
# hddtemp -d /dev/sd[abcd]
Use telnet or nc / netcat command to to get a temperature from a remote box:
$ telnet remotebox 7634
OR
$ nc 192.168.1.100 7634
Sample outputs:
|/dev/sda|Samsung SSD 850 EVO mSATA 500GB|45|C|
You can format it as follows using the awk command:
nc centos7-box 7634 | awk -F'|' '{print $2 " " $4 $5 "(" $3 ")"}'
Sample outputs:
/dev/sda 45C(Samsung SSD 850 EVO mSATA 500GB)

Shutdown Linux Computer If Temperature >= 55

To power off / shutdown computer, run following command via cron tab (cron job) file:
t=$(hddtemp /dev/sda --numeric)
[ $t -ge 55 ] && /sbin/shutdown -h 0

Sample shell script to shutdown box if temperature >= 55°C (download link):
#!/bin/bash
# Purpose: Shutdown server if disk temp crossed $ALERT_LEVEL
# Author: Vivek Gite {https://www.cyberciti.biz/}, under GPL v.2.x
# -----------------------------------------------------------------
HDDS="/dev/sda /dev/sdb /dev/sdc /dev/sdc /dev/sdd /dev/sde"
HDT=/usr/sbin/hddtemp
LOG=/usr/bin/logger
DOWN=/sbin/shutdown
ALERT_LEVEL=60
for disk in $HDDS
do
  if [ -b $disk ]; then
        HDTEMP=$( ${HDT} --numeric ${disk} )
        if [ $HDTEMP -ge $ALERT_LEVEL ];
        then
           $LOG "System going down as hard disk \"$disk\" temperature ($HDTEMP) crossed its limit."
           sync;sync
           $DOWN -h 0
        fi
  fi
done
You can run script using a cron job on Linux or Unix-like system:
$ sudo crontab -e
Append the following to run cron job every 10 minutes to check for disk temp:
*/10 * * * * /root/bin/chk-disk-temp.bash

hddtemp command options

To see usage, run:
$ hddtemp --help
Sample outputs:
  -b   --drivebase   :  display database file content that allow hddtemp to
                        recognize supported drives.
  -D   --debug       :  display various S.M.A.R.T. fields and their values.
                        Useful to find a value that seems to match the
                        temperature and/or to send me a report.
                        (done for every drive supplied).
  -d   --daemon      :  run hddtemp in TCP/IP daemon mode (port 7634 by default.)
  -f   --file=FILE   :  specify database file to use.
  -F   --foreground  :  don't daemonize, stay in foreground.
  -l   --listen=addr :  listen on a specific interface (in TCP/IP daemon mode).
  -n   --numeric     :  print only the temperature.
  -p   --port=#      :  port to listen to (in TCP/IP daemon mode).
  -s   --separator=C :  separator to use between fields (in TCP/IP daemon mode).
  -S   --syslog=s    :  log temperature to syslog every s seconds.
  -u   --unit=[C|F]  :  force output temperature either in Celsius or Fahrenheit.
  -q   --quiet       :  do not check if the drive is supported.
  -v   --version     :  display hddtemp version number.
  -w   --wake-up     :  wake-up the drive if need.
  -4                 :  listen on IPv4 sockets only.
  -6                 :  listen on IPv6 sockets only.

Say hello to smartctl Utility

If you have smartctl utility installed, try it as follows to get temperature data:
# smartctl -d ata -A /dev/sda | grep -i temperature
smartctl -A /dev/sdb | grep -i temperature

Linux Find Hard Disk Temperature Command
Set ALERT_LEVEL as per your requirements. Please refer to your hard disk manual for working temperature guideline. Here is general temperature guideline (extracted from Seagate SV35.2 Series Hard Drives Web Page):
Operating 0 to 60 degrees C
Nonoperating -40 to 70 degrees C
Maximum operating temperature change 20 degrees C per hour
Maximum nonoperating temperature change 30 degrees C per hour
Maximum operating case temperature69 degrees C

A note for MS-Windows XP / Vista/ 7 / 10 / Server Users

Sorry. The hddtemp is UNIX / Linux only program.

Further readings

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

No comments:

Post a Comment