Skip to content

Instantly share code, notes, and snippets.

@h4cc
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4cc/a2163b697469d847cd4a to your computer and use it in GitHub Desktop.
Save h4cc/a2163b697469d847cd4a to your computer and use it in GitHub Desktop.
which smartctl || sudo apt-get -y install smartmontools
which php || sudo apt-get -y install php5-cli
[ -f smart.php ] || wget https://gist.githubusercontent.com/h4cc/a2163b697469d847cd4a/raw/a04e2645d3c5f22625f3efaa28a4f604912b2792/smart.php
for disk in /dev/sd[a-z]
do
echo $disk
sudo smartctl --attributes $disk | php smart.php
done
<?php
/**
* Script searching for broken harddisks by SMART Values.
*
* @author Julius Beckmann
*
* Will search for "sector" or "reallo"cated values != 0
*
* Usage:
* sudo smartctl --attributes /dev/sda | php smart.php
*/
$content = file_get_contents('php://stdin');
$lines = explode("\n", $content);
$data = array();
foreach($lines as $line) {
$line = trim($line);
$line = preg_replace("/(\s){2,}/", '$1', $line);
$parts = explode(' ', $line);
if(count($parts) >= 10) {
$data[$parts[0]] = array(
'name' => $parts[1],
'value' => $parts[9],
);
}
}
$searchWords = array('sector', 'reallo');
foreach($data as $id => $entry) {
foreach($searchWords as $searchWord) {
if(false !== stripos($entry['name'], $searchWord)) {
//var_dump($id, $entry);
if(0 != $entry['value']) {
echo "\n---------------------\n--- BROKEN DISK!? ---\n---------------------\n\n".$content;
exit(1);
}
break;
}
}
}
echo "No SMART Problems found.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment