<?PHP
/* Copyright 2012, Bergware International
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 */
?>
<?
$sf = '/plugins/simpleFeatures';

parse_str($argv[1],$_GET);
$port = isset($_GET['device']) ? $_GET['device'] : '';
$spin = exec("hdparm -C /dev/$port | grep 'active'");

switch ($_GET['cmd']):
case "short":
  exec("smartctl -t short /dev/$port");
  break;
case "long":
  exec("smartctl -t long /dev/$port");
  break;
case "stop":
  exec("smartctl -X /dev/$port");
  break;
case "update":
  $smart = "SMART self-test progress:";
  $result = "Last SMART test result:";
  if (!$spin):
    echo "<td>$result</td><td class='bigger'>Unavailable</td>";
    break;
  endif;
  if (exec("smartctl -l selftest /dev/$port | grep 'in progress'")):
    $percent = 100 - exec("smartctl -l selftest /dev/$port | grep 'in progress' | sed 's:^# :#0:' | awk '{print $8}' | sed 's:%::'");
    echo "<td>$smart</td><td class='bigger'><img src='$sf/images/loading.gif'>&nbsp;&nbsp;$percent % complete</td>";
  elseif (exec("smartctl -c /dev/$port | grep 'execution status' | grep 'in progress'")):
    $percent = 100 - exec("smartctl -c /dev/$port | grep -A 1 'execution status' | grep -v 'execution status' | cut -d% -f1");
    echo "<td>$smart</td><td class='bigger'><img src='$sf/images/loading.gif'>&nbsp;&nbsp;$percent % complete</td>";
  elseif (exec("smartctl -l selftest /dev/$port | grep '# 1' | grep 'Completed without error'")):
    echo "<td>$result</td><td><span class='bigger passed'>Completed without errors</span></td>";
  elseif ((!exec("smartctl -l selftest /dev/$port | grep '# 1'")) || (exec("smartctl -l selftest /dev/$port | grep '# 1' | grep 'No self-tests'"))):
    echo "<td>$result</td><td class='bigger'>No self-tests logged on this disk</td>";
  elseif (exec("smartctl -l selftest /dev/$port | grep '# 1' | grep 'Aborted'")):
    echo "<td>$result</td><td><span class='bigger warning'>Test aborted</span></td>";
  elseif (exec("smartctl -l selftest /dev/$port | grep '# 1' | grep 'Interrupted'")):
    echo "<td>$result</td><td><span class='bigger warning'>Test interrupted</span></td>";
  elseif (!exec("smartctl -l selftest /dev/$port | grep '# 1' | tail -c -2 | grep '-'")):
    echo "<td>$result</td><td><span class='bigger failed'>Errors occurred - Check logs</span></td>";
  else:
    echo "<td>$result</td><td class='bigger'>Unknown</td>";
  endif;
  break;
case "health":
  $poll = $_GET['poll'] ? '' : '-n standby';
  echo exec("smartctl $poll -q silent -H /dev/$port")==0 ? ($spin ? 'on' : 'off') : ($spin ? 'fail' : 'fail off');
  break;
case "status":
  if (exec("smartctl -q silent -H /dev/$port")==0):
    echo "<span class='bigger passed'>Passed</span>";
  else:
    echo "<span class='bigger failed'>Failed</span><br>Get data off disk and run reports below to obtain more information";
  endif;
  break;
case "identity":
  $output = shell_exec("smartctl -i /dev/$port | awk 'NR>4'");
  echo "<pre>$output</pre>";
  break;
case "attributes":
  $output = shell_exec("smartctl -A /dev/$port | awk 'NR>6'");
  echo "<pre>$output</pre>";
  break;
case "capabilities":
  $output = shell_exec("smartctl -c /dev/$port | awk 'NR>4'");
  echo "<pre>$output</pre>";
  break;
case "selftest":
  $output = shell_exec("smartctl -l selftest /dev/$port | awk 'NR>5'");
  echo "<pre>$output</pre>";
  break;
case "errorlog":
  $output = shell_exec("smartctl -l error /dev/$port | awk 'NR>5'");
  echo "<pre>$output</pre>";
  break;
case "sleep":
  exec("echo 'Enter sleep mode' | logger");
// Sleeping...
  if ( !empty($_GET['sleep_cmd']) ) {
    exec(rawurldecode($_GET['sleep_cmd']));
  } else {
    exec("echo -n mem > /sys/power/state");
  }
  exec("echo 'Wake-up from sleep mode' | logger");
  break;
endswitch;
?>