个人工具

“UbuntuHelp:Smartmontools”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/Smartmontools}}
 
{{From|https://help.ubuntu.com/community/Smartmontools}}
 
{{Languages|UbuntuHelp:Smartmontools}}
 
{{Languages|UbuntuHelp:Smartmontools}}
This how to will help you to configure Smartmontools to do actions as shut down the computer or send an e-mail when the disk is going to fail.
+
== Introduction ==
 +
Smartmontools is a set of applications that can test hard drives and read their hardware SMART statistics. Note: SMART data may not accurately predict future drive failure, however abnormal error rates may be an indication of possible hardware failure or data inconsistency.
 +
This how to will help you to configure Smartmontools to do actions such as shut down the computer or send an e-mail when the disk is going to fail.
 
=== Prerequisites ===
 
=== Prerequisites ===
* A modern hard disk S.M.A.R.T. capable
+
* A modern S.M.A.R.T. capable hard disk
 +
== Setting up ==
 
=== Installation ===
 
=== Installation ===
First of all, we have to install Smartmontools package. Install the <code><nowiki>smartmontools</nowiki></code> package in the Synaptic Package Manager. See [[UbuntuHelp:SynapticHowto|SynapticHowto]].
+
You can install the <code><nowiki>smartmontools</nowiki></code> package from the Synaptic Package Manager (see [[UbuntuHelp:SynapticHowto|SynapticHowto]]), or by typing the following into the terminal:
To display all the SMART information for an IDE drive, run
+
 
<pre><nowiki>
 
<pre><nowiki>
sudo smartctl -a /dev/hda
+
sudo apt-get install smartmontools </nowiki></pre>
</nowiki></pre>
+
=== Checking a drive for SMART Capability ===
To display all the SMART information for a SATA drive, run
+
To ensure that your drive supports SMART, type:
 +
<pre><nowiki>
 +
smartctl -i /dev/sda </nowiki></pre>
 +
where /dev/sda is your hard drive. This will give you brief information about your drive. The last two lines may look something like this:
 +
<pre><nowiki>
 +
SMART support is: Available - device has SMART capability.
 +
SMART support is: Enabled </nowiki></pre>
 +
=== Enabling SMART ===
 +
In the case that SMART is not enabled for your drive, you can enable it by typing:
 +
<pre><nowiki>
 +
smartctl -s on /dev/sda </nowiki></pre>
 +
== Testing a Drive ==
 +
You may run any type of test while the drive is mounted although there may be some drop in performance. There are three types of test that can be conducted on a drive:
 +
<ol><li>Short
 +
</li><li>Extended (Long)
 +
</li><li>Conveyance</li></ol>
 +
 
 +
To find an estimate of the time it takes to conduct each test, type:
 +
<pre><nowiki>
 +
smartctl -c /dev/sda </nowiki></pre>
 +
The most useful test is the extended test (long). You can initiate the test by typing:
 +
<pre><nowiki>
 +
smartctl -t long /dev/sda </nowiki></pre>
 +
=== Results ===
 +
You can view a drive's test statistics by typing:
 +
<pre><nowiki>
 +
smartctl -l selftest /dev/sda </nowiki></pre>
 +
To display detailed SMART information for an IDE drive, type:
 +
<pre><nowiki>
 +
sudo smartctl -a /dev/hda </nowiki></pre>
 +
To display detailed SMART information for a SATA drive, type:
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo smartctl -a -d ata /dev/sda
 
sudo smartctl -a -d ata /dev/sda
 
</nowiki></pre>
 
</nowiki></pre>
 
Note: This also works for IDE drives in new kernels that are being run through the SCSI stack and show up as /dev/sdX
 
Note: This also works for IDE drives in new kernels that are being run through the SCSI stack and show up as /dev/sdX
----
+
=== Suggested application: GSmartControl ===
Now we have to edit some files. Open the file <code><nowiki>/etc/default/smartmontools</nowiki></code> with your favourite text editor, for example, vim: <code><nowiki>sudo vim /etc/default/smartmontools</nowiki></code> an uncomment the line <code><nowiki>start_smartd=yes</nowiki></code> Then, we are going to edit the daemon configuration. Again, use your favourite text editor to open the file <code><nowiki>/etc/smartd.conf</nowiki></code>. Comment out the line that contains <code><nowiki>DEVICESCAN</nowiki></code>, by adding # in the beginning of the line. Then, add this to the end of the file:
+
Take a look at [http://gsmartcontrol.berlios.de/home/index.php/en/Home GSmartControl]. It's a nice graphical frontend to smartctl; it shows all SMART values, and highlights those that indicate old age or impending failure, plus you may run tests on demand:
 +
[[UbuntuHelp:attachment:gsmartcontrol - failing.png|https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=gsmartcontrol+-+failing.png_GSmartControl+main+window_width_177]] [[UbuntuHelp:attachment:gsmartcontrol - info_failing.png|https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=gsmartcontrol+-+info_failing.png_GSmartControl+attribute+list_width_350]]
 +
As usual, you may install it from Synaptic or running <code><nowiki>sudo apt-get install gsmartcontrol</nowiki></code>.
 +
== Advanced: Running as Smartmontools as a Daemon ==
 +
You can run Smartmontools in the background and have it check drives and email when there are issues:
 +
Open the file <code><nowiki>/etc/default/smartmontools</nowiki></code> with your favourite text editor. For example (using vim): <code><nowiki>sudo vim /etc/default/smartmontools</nowiki></code>. Uncomment the line <code><nowiki>start_smartd=yes</nowiki></code>.
 +
How <code><nowiki>smartd</nowiki></code> is going to scan the disks and what it will do in case of errors is controlled by the daemon configuration file, <code><nowiki>/etc/smartd.conf</nowiki></code>. Again, use your favourite text editor to open this file. There should be one uncommented line, similar to:
 
<pre><nowiki>
 
<pre><nowiki>
/dev/hda \
+
DEVICESCAN -m root -M exec /usr/share/smartmontools/smartd-runner</nowiki></pre>
-H \
+
In this example (which is the default for Karmic), <code><nowiki>smartd</nowiki></code> will:
-l error -l selftest \
+
* scan for all ATA/SCSI devices (<code><nowiki>DEVICESCAN</nowiki></code>). The rest of the file will be ignored;
-s (O/../../5/11|L/../../5/13|C/../../5/15) \
+
* mail a report to the 'root' account in case of trouble (<code><nowiki>-m</nowiki></code>);
-m ThisIsNotUsed -M exec /usr/local/bin/smartd.sh</nowiki></pre>
+
* but instead of the <code><nowiki>mail</nowiki></code> command, it will execute <code><nowiki>/usr/share/smartmontools/smartd-runner</nowiki></code> and feed the report to it (<code><nowiki>-M exec program</nowiki></code>).
You must replace <code><nowiki>/dev/hda</nowiki></code> with the name of the device which you'd like to be monitored.
+
<code><nowiki>/usr/share/smartmontools/smartd-runner</nowiki></code> is a script that basically saves the report to a temporary file, and then runs anything it finds in <code><nowiki>/etc/smartmontools/run.d/</nowiki></code>; take a look there to understand what you already have (there should be a script that mails the report).
With this, the daemon will check the disk from time to time, and will execute <code><nowiki>/usr/local/bin/smartd.sh</nowiki></code> if any error is encountered.
+
There are several <code><nowiki>-M</nowiki></code> directives that change when and how often reports are sent. You need to specify (<code><nowiki>-m something</nowiki></code>) in order to use them, even if you're not sending any mail.
Now, we are going to make the script which is going to shut down the computer. Create a text file called <code><nowiki>smartd.sh</nowiki></code> in <code><nowiki>/usr/local/bin/</nowiki></code> (you could use the command <code><nowiki>sudo vim /usr/local/bin/smartd.sh</nowiki></code> to do so) and add the following to the file:
+
You may include some useful options:
 
<pre><nowiki>
 
<pre><nowiki>
 +
DEVICESCAN -H -l error -l selftest -f -s (O/../../5/11|L/../../5/13|C/../../5/15) -m root -M exec /usr/share/smartmontools/smartd-runner</nowiki></pre>
 +
In this example, <code><nowiki>smartd</nowiki></code> will:
 +
* check the SMART health status (<code><nowiki>-H</nowiki></code>);
 +
* report increases in both SMART error logs (<code><nowiki>-l</nowiki></code>);
 +
* check for failure of any Usage Attributes (<code><nowiki>-f</nowiki></code>);
 +
* schedule an Offline Immediate Test every Friday at 11 am, a Long Self-Test every Friday at 1 pm, and a Conveyance Self-Test every Friday at 3 pm (<code><nowiki>-s</nowiki></code>) -- see the <code><nowiki>smartd</nowiki></code> manual page for what these tests do so you can choose what suits you.
 +
You may also replace DEVICESCAN with the path of the device which you'd like to be monitored (e.g. <code><nowiki>/dev/sda</nowiki></code>), and the daemon will only monitor this drive. You'll need one such line for each device.
 +
=== Actions in case of trouble ===
 +
You'll want to configure the actions <code><nowiki>smartd</nowiki></code> will take in case of trouble. If all you want is a notification shown on your desktop, skip to "Personal computer" below.
 +
Most of the time, you only need to place a script in <code><nowiki>/etc/smartmontools/run.d/</nowiki></code>. Whenever <code><nowiki>smartd</nowiki></code> wants to send a report, it will execute <code><nowiki>smart-runner</nowiki></code> and the latter will run your script.
 +
You have several variables available to your script (again, see the <code><nowiki>smartd</nowiki></code> manpage). These come from a test run:
 +
<pre><nowiki>
 +
SMARTD_MAILER=/usr/share/smartmontools/smartd-runner
 +
SMARTD_SUBJECT=SMART error (EmailTest) detected on host: XXXXX
 +
SMARTD_ADDRESS=root
 +
SMARTD_TFIRSTEPOCH=1267409738
 +
SMARTD_FAILTYPE=EmailTest
 +
SMARTD_TFIRST=Sun Feb 28 21:45:38 2010 VET
 +
SMARTD_DEVICE=/dev/sda
 +
SMARTD_DEVICETYPE=sat
 +
SMARTD_DEVICESTRING=/dev/sda
 +
SMARTD_FULLMESSAGE=This email was generated by the smartd daemon running on:
 +
SMARTD_MESSAGE=TEST EMAIL from smartd for device: /dev/sda
 +
</nowiki></pre>
 +
Your script also has a ''temporary'' copy of the report available as "$1". It will be deleted after you finish but the same content is written to <code><nowiki>/var/log/syslog</nowiki></code>.
 +
==== Personal computer ====
 +
For a visual notification, you may just install <code><nowiki>smart-notifier</nowiki></code>. You will see a large popup with the report:
 +
https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=smart-notifier_warning.png
 +
Alternatively, you may create a custom notification (bubble) as seen in other GNOME programs.
 +
You will need to install the <code><nowiki>libnotify-bin</nowiki></code> package:
 +
<pre><nowiki>
 +
sudo aptitude install libnotify-bin</nowiki></pre>
 +
Now create a text file called <code><nowiki>60notify</nowiki></code> in <code><nowiki>/etc/smartmontools/run.d</nowiki></code>:
 +
<pre><nowiki>
 +
sudo vi /etc/smartmontools/run.d/60notify</nowiki></pre>
 +
and add the following to the file:
 +
<pre><nowiki>#!
 
#!/bin/bash
 
#!/bin/bash
LOGFILE="/var/log/smartd.log"
+
 
echo -e "$(date)\n$SMARTD_MESSAGE\n" >> "$LOGFILE"
+
DISPLAY=:0.0 notify-send --icon=important "Possible disk failure" "$SMARTD_DEVICE may have a problem"
mail admin[at]alcalleop[dot]net < $LOGFILE
+
</nowiki></pre>
sleep 40s
+
(The <code><nowiki>DISPLAY=:0.0</nowiki></code> part is a variable assignment that helps programs to locate your X server. It's already set for your terminal, but the script lacks it since it is being run inside a different session).
 +
Now give it execute permissions:
 +
<pre><nowiki>
 +
sudo chmod +x /etc/smartmontools/run.d/60notify</nowiki></pre>
 +
This will produce a nice libnotify bubble with a warning icon:
 +
https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=smart_notification.png
 +
You may also experiment with Zenity:
 +
<pre><nowiki>#!
 +
#!/bin/bash
 +
 
 +
DISPLAY=:0.0 zenity --text-info --filename="$1" --title="smartd: $SMARTD_DEVICE may have a problem"
 +
</nowiki></pre>
 +
'''Notice:''' Be very careful with these scripts as they are run under the root account.
 +
==== Server ====
 +
Here, you may wish to handle things differently. In this example we want to mail an admin ''and'' shut down the server.
 +
Comment out the line that contains <code><nowiki>DEVICESCAN</nowiki></code>, by adding # to the beginning of the line. Then, add this to the end of the file:
 +
<pre><nowiki>
 +
/dev/hda -H -l error -l selftest -f -s (O/../../5/11|L/../../5/13|C/../../5/15) \
 +
-m [email protected] -M exec /usr/share/smartmontools/smartd-runner</nowiki></pre>
 +
(Be sure not to add any whitespace after the "\")
 +
Now, we are going to make the script which is going to shut down the computer *after* we mail the admin. Create a text file called <code><nowiki>99shutdown</nowiki></code> in <code><nowiki>/etc/smartmontools/run.d</nowiki></code> and add the following to the file:
 +
<pre><nowiki>#!
 +
#!/bin/bash
 +
 
 +
sleep 40
 
shutdown -h now</nowiki></pre>
 
shutdown -h now</nowiki></pre>
This will send the log file by e-mail to admin[at]alcalleop[dot]net, wait 40 seconds and then will shut down the computer. You can customize the script to change what the computer will do in a case of disk error. For example, perhaps you do not want the computer to shut down.
+
The number ''99'' at the start of the filename is to ensure that it is called last when <code><nowiki>smartd-runner</nowiki></code> runs. It will wait 40 seconds and then shut down the computer. Of course, you may customize this at will; you may not wish to turn off the server.
Now, it is time to start the daemon: <code><nowiki>sudo /etc/init.d/smartmontools start</nowiki></code>
+
Now, it is time to start the daemon: <pre><nowiki>
If you want to test all of this, add <code><nowiki>-M test</nowiki></code> after <code><nowiki>-M exec /usr/local/bin/smartd.sh</nowiki></code> and restart the daemon. This will force daemon to execute the script immediately. '''Now, every time the computer is turned on, the script will shut down the computer, so you will have to start the computer in recovery mode and remove <code><nowiki>-M test</nowiki></code> option of <code><nowiki>/etc/smartd.conf</nowiki></code> file.
+
sudo service smartmontools start</nowiki></pre>
 +
==== Testing ====
 +
If you want to test all these actions, add <code><nowiki>-M test</nowiki></code> after <code><nowiki>exec /usr/share/smartmontools/smartd-runner</nowiki></code> and restart the daemon (<code><nowiki>sudo service smartmontools restart</nowiki></code>). When the daemon comes up, it will execute the script immediately with a test message. '''Notice:''' If you included the <code><nowiki>shutdown -h</nowiki></code> line, the script will shut down the computer as soon as the service starts. To fix this, you will have to start the computer in recovery mode and remove the <code><nowiki>-M test</nowiki></code> option from <code><nowiki>/etc/smartd.conf</nowiki></code>.
 
Based on [http://gentoo-wiki.com/HOWTO_Monitor_your_hard_disk(s)_with_smartmontools Gentoo Wiki: HOWTO Monitor your hard disk(s)withsmartmontools].
 
Based on [http://gentoo-wiki.com/HOWTO_Monitor_your_hard_disk(s)_with_smartmontools Gentoo Wiki: HOWTO Monitor your hard disk(s)withsmartmontools].
-----
+
==== Note ====
 
Before running this, be sure to check that you have a "mail" command, and do a test first to your address. On my default Fiesty:
 
Before running this, be sure to check that you have a "mail" command, and do a test first to your address. On my default Fiesty:
 
jim@beorn:~$ mail
 
jim@beorn:~$ mail
第46行: 第147行:
 
Make sure you have the 'universe' component enabled
 
Make sure you have the 'universe' component enabled
 
bash: mail: command not found
 
bash: mail: command not found
----
+
== Utility: Checking all disks at once ==
Note: Following the Gentoo Wiki i made a modified script which checks all the disk in /dev/disk/by-id/
+
Note: Following the Gentoo Wiki I made a modified script which checks all the disk in <code><nowiki>/dev/disk/by-id/</nowiki></code>
Just use the command
+
Just invoke the script below as follows:
 
<pre><nowiki>
 
<pre><nowiki>
sh smart.sh short|long|offline
+
./smart.sh short|long|offline
 
</nowiki></pre>
 
</nowiki></pre>
The script make a directory smart-logs and stores all the files there.
+
The script creates a directory named <code><nowiki>smart-logs</nowiki></code> and stores all the files there.
<pre><nowiki>
+
<pre><nowiki>#!
 
#!/bin/sh
 
#!/bin/sh
  
第95行: 第196行:
 
echo "All tests have completed"
 
echo "All tests have completed"
 
</nowiki></pre>
 
</nowiki></pre>
 +
(Remember to give execute permissions to the script with <code><nowiki>chmod +x smart.sh</nowiki></code>).
 
----
 
----
 
[[category:CategoryHardware]]
 
[[category:CategoryHardware]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月20日 (四) 00:13的最新版本


Introduction

Smartmontools is a set of applications that can test hard drives and read their hardware SMART statistics. Note: SMART data may not accurately predict future drive failure, however abnormal error rates may be an indication of possible hardware failure or data inconsistency. This how to will help you to configure Smartmontools to do actions such as shut down the computer or send an e-mail when the disk is going to fail.

Prerequisites

  • A modern S.M.A.R.T. capable hard disk

Setting up

Installation

You can install the smartmontools package from the Synaptic Package Manager (see SynapticHowto), or by typing the following into the terminal:

sudo apt-get install smartmontools 

Checking a drive for SMART Capability

To ensure that your drive supports SMART, type:

smartctl -i /dev/sda 

where /dev/sda is your hard drive. This will give you brief information about your drive. The last two lines may look something like this:

SMART support is: Available - device has SMART capability.
SMART support is: Enabled 

Enabling SMART

In the case that SMART is not enabled for your drive, you can enable it by typing:

smartctl -s on /dev/sda 

Testing a Drive

You may run any type of test while the drive is mounted although there may be some drop in performance. There are three types of test that can be conducted on a drive:

  1. Short
  2. Extended (Long)
  3. Conveyance

To find an estimate of the time it takes to conduct each test, type:

smartctl -c /dev/sda 

The most useful test is the extended test (long). You can initiate the test by typing:

smartctl -t long /dev/sda 

Results

You can view a drive's test statistics by typing:

smartctl -l selftest /dev/sda 

To display detailed SMART information for an IDE drive, type:

sudo smartctl -a /dev/hda 

To display detailed SMART information for a SATA drive, type:

sudo smartctl -a -d ata /dev/sda

Note: This also works for IDE drives in new kernels that are being run through the SCSI stack and show up as /dev/sdX

Suggested application: GSmartControl

Take a look at GSmartControl. It's a nice graphical frontend to smartctl; it shows all SMART values, and highlights those that indicate old age or impending failure, plus you may run tests on demand: https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=gsmartcontrol+-+failing.png_GSmartControl+main+window_width_177 https://help.ubuntu.com/community/Smartmontools?action=AttachFile&do=get&target=gsmartcontrol+-+info_failing.png_GSmartControl+attribute+list_width_350 As usual, you may install it from Synaptic or running sudo apt-get install gsmartcontrol.

Advanced: Running as Smartmontools as a Daemon

You can run Smartmontools in the background and have it check drives and email when there are issues: Open the file /etc/default/smartmontools with your favourite text editor. For example (using vim): sudo vim /etc/default/smartmontools. Uncomment the line start_smartd=yes. How smartd is going to scan the disks and what it will do in case of errors is controlled by the daemon configuration file, /etc/smartd.conf. Again, use your favourite text editor to open this file. There should be one uncommented line, similar to:

DEVICESCAN -m root -M exec /usr/share/smartmontools/smartd-runner

In this example (which is the default for Karmic), smartd will:

  • scan for all ATA/SCSI devices (DEVICESCAN). The rest of the file will be ignored;
  • mail a report to the 'root' account in case of trouble (-m);
  • but instead of the mail command, it will execute /usr/share/smartmontools/smartd-runner and feed the report to it (-M exec program).

/usr/share/smartmontools/smartd-runner is a script that basically saves the report to a temporary file, and then runs anything it finds in /etc/smartmontools/run.d/; take a look there to understand what you already have (there should be a script that mails the report). There are several -M directives that change when and how often reports are sent. You need to specify (-m something) in order to use them, even if you're not sending any mail. You may include some useful options:

DEVICESCAN -H -l error -l selftest -f -s (O/../../5/11|L/../../5/13|C/../../5/15) -m root -M exec /usr/share/smartmontools/smartd-runner

In this example, smartd will:

  • check the SMART health status (-H);
  • report increases in both SMART error logs (-l);
  • check for failure of any Usage Attributes (-f);
  • schedule an Offline Immediate Test every Friday at 11 am, a Long Self-Test every Friday at 1 pm, and a Conveyance Self-Test every Friday at 3 pm (-s) -- see the smartd manual page for what these tests do so you can choose what suits you.

You may also replace DEVICESCAN with the path of the device which you'd like to be monitored (e.g. /dev/sda), and the daemon will only monitor this drive. You'll need one such line for each device.

Actions in case of trouble

You'll want to configure the actions smartd will take in case of trouble. If all you want is a notification shown on your desktop, skip to "Personal computer" below. Most of the time, you only need to place a script in /etc/smartmontools/run.d/. Whenever smartd wants to send a report, it will execute smart-runner and the latter will run your script. You have several variables available to your script (again, see the smartd manpage). These come from a test run:

SMARTD_MAILER=/usr/share/smartmontools/smartd-runner
SMARTD_SUBJECT=SMART error (EmailTest) detected on host: XXXXX
SMARTD_ADDRESS=root
SMARTD_TFIRSTEPOCH=1267409738
SMARTD_FAILTYPE=EmailTest
SMARTD_TFIRST=Sun Feb 28 21:45:38 2010 VET
SMARTD_DEVICE=/dev/sda
SMARTD_DEVICETYPE=sat
SMARTD_DEVICESTRING=/dev/sda
SMARTD_FULLMESSAGE=This email was generated by the smartd daemon running on:
SMARTD_MESSAGE=TEST EMAIL from smartd for device: /dev/sda

Your script also has a temporary copy of the report available as "$1". It will be deleted after you finish but the same content is written to /var/log/syslog.

Personal computer

For a visual notification, you may just install smart-notifier. You will see a large popup with the report: Smartmontools?action=AttachFile&do=get&target=smart-notifier_warning.png Alternatively, you may create a custom notification (bubble) as seen in other GNOME programs. You will need to install the libnotify-bin package:

sudo aptitude install libnotify-bin

Now create a text file called 60notify in /etc/smartmontools/run.d:

sudo vi /etc/smartmontools/run.d/60notify

and add the following to the file:

#!
#!/bin/bash

DISPLAY=:0.0 notify-send --icon=important "Possible disk failure" "$SMARTD_DEVICE may have a problem"

(The DISPLAY=:0.0 part is a variable assignment that helps programs to locate your X server. It's already set for your terminal, but the script lacks it since it is being run inside a different session). Now give it execute permissions:

sudo chmod +x /etc/smartmontools/run.d/60notify

This will produce a nice libnotify bubble with a warning icon: Smartmontools?action=AttachFile&do=get&target=smart_notification.png You may also experiment with Zenity:

#!
#!/bin/bash

DISPLAY=:0.0 zenity --text-info --filename="$1" --title="smartd: $SMARTD_DEVICE may have a problem"

Notice: Be very careful with these scripts as they are run under the root account.

Server

Here, you may wish to handle things differently. In this example we want to mail an admin and shut down the server. Comment out the line that contains DEVICESCAN, by adding # to the beginning of the line. Then, add this to the end of the file:

/dev/hda -H -l error -l selftest -f -s (O/../../5/11|L/../../5/13|C/../../5/15) \
-m [email protected] -M exec /usr/share/smartmontools/smartd-runner

(Be sure not to add any whitespace after the "\") Now, we are going to make the script which is going to shut down the computer *after* we mail the admin. Create a text file called 99shutdown in /etc/smartmontools/run.d and add the following to the file:

#!
#!/bin/bash

sleep 40
shutdown -h now

The number 99 at the start of the filename is to ensure that it is called last when smartd-runner runs. It will wait 40 seconds and then shut down the computer. Of course, you may customize this at will; you may not wish to turn off the server.

Now, it is time to start the daemon:
sudo service smartmontools start

Testing

If you want to test all these actions, add -M test after exec /usr/share/smartmontools/smartd-runner and restart the daemon (sudo service smartmontools restart). When the daemon comes up, it will execute the script immediately with a test message. Notice: If you included the shutdown -h line, the script will shut down the computer as soon as the service starts. To fix this, you will have to start the computer in recovery mode and remove the -M test option from /etc/smartd.conf. Based on Gentoo Wiki: HOWTO Monitor your hard disk(s)withsmartmontools.

Note

Before running this, be sure to check that you have a "mail" command, and do a test first to your address. On my default Fiesty: jim@beorn:~$ mail The program 'mail' can be found in the following packages:

  • mailx
  • mailutils

Try: sudo apt-get install <selected package> Make sure you have the 'universe' component enabled bash: mail: command not found

Utility: Checking all disks at once

Note: Following the Gentoo Wiki I made a modified script which checks all the disk in /dev/disk/by-id/ Just invoke the script below as follows:

./smart.sh short|long|offline

The script creates a directory named smart-logs and stores all the files there.

#!
#!/bin/sh

# Script by Meliorator. irc://irc.freenode.net/Meliorator
# modified by Ranpha
[ ! "$@" ] && echo "Usage: $0 type [type] [type]"

[ ! -e smart-logs ] && mkdir smart-logs
[ ! -d smart-logs ] && Can not create smart-logs dir && exit 1

a=0

for t in "$@"; do

        case "$t" in
                offline)  l=error;;
                short|long)  l=selftest;;
                *) echo $t is an unrecognised test type. Skipping... && continue
        esac

       for hd in  /dev/disk/by-id/ata*; do
                r=$(( $(smartctl -t $t -d ata $hd | grep 'Please wait' | awk '{print $3}') ))
                echo Check $hd - $t test in $r minutes
                [ $r -gt $a ] && a=$r
       done
     echo "Waiting $a minutes for all tests to complete"
		sleep $(($a)) m

	for hd in /dev/disk/by-id/ata*; do
                smartctl -l $l -d ata $hd 2>&1 >> smart-logs/smart-${t}-${hd##*/}.log
        done

	
done

for i in {1..10}; do
        sleep .01
        echo -n -e \\a
done

echo "All tests have completed"

(Remember to give execute permissions to the script with chmod +x smart.sh).