$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/Preselect.php";
// add translations
$_SERVER['REQUEST_URI'] = 'main';
require_once "$docroot/webGui/include/Translations.php";
$disks = array_merge_recursive(@parse_ini_file('state/disks.ini',true)?:[], @parse_ini_file('state/devs.ini',true)?:[]);
require_once "$docroot/webGui/include/CustomMerge.php";
function normalize($text, $glue='_') {
$words = explode($glue,$text);
foreach ($words as &$word) $word = $word==strtoupper($word) ? $word : preg_replace(['/^(ct|cnt)$/','/^blk$/'],['count','block'],strtolower($word));
return "
".ucfirst(implode(' ',$words))." | ";
}
function size($val) {
return str_replace(',','',$val);
}
function duration(&$hrs) {
$time = ceil(time()/3600)*3600;
$run = size($hrs);
$now = new DateTime("@$time");
$poh = new DateTime("@".($time-$run*3600));
$age = date_diff($poh,$now);
$hrs = "$hrs (".($age->y?"{$age->y}y, ":"").($age->m?"{$age->m}m, ":"").($age->d?"{$age->d}d, ":"")."{$age->h}h)";
}
function append(&$ref, &$info) {
if ($info) $ref .= ($ref ? " " : "").$info;
}
$name = $_POST['name']??'';
$port = $_POST['port']??'';
if ($name) {
$disk = &$disks[$name];
$type = get_value($disk,'smType','');
get_ctlr_options($type, $disk);
} else {
$disk = [];
$type = '';
}
$port = port_name($disk['smDevice'] ?? $port);
switch ($_POST['cmd']??'') {
case "attributes":
$select = get_value($disk,'smSelect',0);
$level = get_value($disk,'smLevel',1);
$events = explode('|',get_value($disk,'smEvents',$numbers));
extract(parse_plugin_cfg('dynamix',true));
$max = ($disk['maxTemp'] ?? $display['max'] ?? 0) ?: 0;
$hot = ($disk['hotTemp'] ?? $display['hot'] ?? 0) ?: 0;
$top = $_POST['top'] ?? 120;
$empty = true;
exec("smartctl -n standby -A $type ".escapeshellarg("/dev/$port"),$output);
// remove empty rows
$output = array_filter($output);
$start = 0;
// find start of attributes list (if existing)
foreach ($output as $row) if (stripos($row,'smart attributes data structure')===false) $start++; else break;
if ($start < count($output)-3) {
// remove header part
$output = array_slice($output, $start+3);
foreach ($output as $row) {
$info = explode(' ', trim(preg_replace('/\s+/',' ',$row)), 10);
if (count($info)<10) continue;
$color = "";
$highlight = strpos($info[8],'FAILING_NOW')!==false || ($select ? $info[5]>0 && $info[3]<=$info[5]*$level : $info[9]>0);
if (in_array($info[0], $events) && $highlight) $color = " class='warn'";
elseif (in_array($info[0], [190,194])) {
if (exceed($info[9],$max,$top)) $color = " class='alert'"; elseif (exceed($info[9],$hot,$top)) $color = " class='warn'";
}
if ($info[8]=='-') $info[8] = 'Never';
if ($info[0]==9 && is_numeric(size($info[9]))) duration($info[9]);
echo "".implode('',array_map('normalize', $info))."
";
$empty = false;
}
} else {
// probably a NVMe or SAS device that smartmontools doesn't know how to parse in to a SMART Attributes Data Structure
foreach ($output as $row) {
if (strpos($row,':')===false) continue;
[$name,$value] = array_map('trim',explode(':', $row));
$name = ucfirst(strtolower($name));
$color = '';
switch ($name) {
case 'Temperature':
$temp = strtok($value,' ');
if (exceed($temp,$max)) $color = " class='alert'"; elseif (exceed($temp,$hot)) $color = " class='warn'";
break;
case 'Power on hours':
if (is_numeric(size($value))) duration($value);
break;
}
echo "| - | $name | $value |
";
$empty = false;
}
}
if ($empty) echo "| "._('Attributes not available')." |
";
break;
case "capabilities":
echo '| '._('Feature').' | '._('Value').' | '._('Information').' | ' ;
exec("smartctl -n standby -c $type ".escapeshellarg("/dev/$port")."|awk 'NR>5'",$output);
$row = ['','',''];
$empty = true;
$nvme = substr($port,0,4)=="nvme";
$nvme_section="info" ;
foreach ($output as $line) {
if (!$line) {echo "
" ;continue;}
$line = preg_replace('/^_/','__',preg_replace(['/__+/','/_ +_/'],'_',str_replace([chr(9),')','('],'_',$line)));
$info = array_map('trim', explode('_', preg_replace('/_( +)_ /','__',$line), 3));
if ($nvme && $info[0]=="Supported Power States" ) { $nvme_section="psheading" ;echo "