#!/usr/bin/php -q 0 ? (($val||$last) ? ($val.' '.$word.($val!=1?'s':'').($last ?'':', ')) : '') : ''; } function my_temp($value) { global $unraid; $unit = $unraid['display']['unit']; return ($unit=='F' ? round(9/5*$value+32) : str_replace('.', $unraid['display']['number'][0], $value))." $unit"; } function my_disk($name) { return ucfirst(preg_replace('/^(disk|cache)([0-9]+)/','$1 $2',$name)); } function my_scale($value,&$unit,$precision = NULL) { global $unraid; $scale = $unraid['display']['scale']; $number = $unraid['display']['number']; $units = ['B','KB','MB','GB','TB','PB']; if ($scale==0 && $precision===NULL) { $unit = ''; return number_format($value, 0, $number[0], ($value>=10000 ? $number[1] : '')); } else { $base = $value ? floor(log($value, 1000)) : 0; if ($scale>0 && $base>$scale) $base = $scale; $value = round($value/pow(1000, $base), $precision===NULL ? 2 : $precision); if ($value>=1000 && $scale<0) { $value = 1; $base++; } $unit = $units[$base]; return number_format($value, $precision===NULL ? (($value-intval($value)==0 || $value>=100) ? 0 : ($value>=10 ? 1 : 2)) : $precision, $number[0], ($value>=10000 ? $number[1] : '')); } } function my_check($time,$speed) { if (!$time) return 'unavailable (no parity-check entries logged)'; $days = floor($time/86400); $hmss = $time-$days*86400; $hour = floor($hmss/3600); $mins = $hmss/60%60; $secs = $hmss%60; return plus($days,'day',($hour|$mins|$secs)==0).plus($hour,'hour',($mins|$secs)==0).plus($mins,'minute',$secs==0).plus($secs,'second',true).". Average speed: $speed"; } function read_write_parity_log($epoch,$duration,$speed,$status) { $log = '/boot/config/parity-checks.log'; $timestamp = str_replace(['.0','.'],[' ',' '],date('M.d H:i:s',$epoch)); if (file_exists($log)) { $handle = fopen($log, 'r'); while (($line = fgets($handle)) !== false) { if (strpos($line,$timestamp)!==false) break; } fclose($handle); } if (empty($line)) { $line = "$timestamp|$duration|$speed|$status"; file_put_contents($log,"$line\n",FILE_APPEND); } return $line; } function check_temp($name,$temp,$text,$info) { global $notify,$disks,$saved,$unraid,$server; $disk = &$disks[$name]; $hot = strlen($disk['hotTemp']) ? $disk['hotTemp'] : $unraid['display']['hot']; $max = strlen($disk['maxTemp']) ? $disk['maxTemp'] : $unraid['display']['max']; $warn = $temp>=$max && $max>0 ? 'alert' : ($temp>=$hot && $hot>0 ? 'warning' : ''); $item = 'temp'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : 0; if ($warn) { if ($temp>$last) { exec("$notify -e \"unRAID $text temperature\" -s \"".ucfirst($warn)." [$server] - $text ".($warn=='alert'?'overheated (':'is hot (').my_temp($temp).")\" -d \"$info\" -i \"$warn\""); $saved[$item][$name] = $max>0 && $temp<=$max ? $max : $temp; } } else { if ($last) { exec("$notify -e \"unRAID $text message\" -s \"Notice [$server] - $text returned to normal temperature\" -d \"$info\""); unset($saved[$item][$name]); } } } function check_smart($name,$port,$text,$info) { global $var,$disks,$notify,$saved,$server,$numbers; $disk = &$disks[$name]; $select = isset($disk['smSelect']) ? $disk['smSelect'] : -1; if ($select==-1) $select = isset($all['smSelect']) ? $var['smSelect'] : 0; $level = isset($disk['smLevel']) ? $disk['smLevel'] : -1; if ($level==-1) $level = isset($var['smLevel']) ? $var['smLevel'] : 1; $events = isset($disk['smEvents']) ? explode('|',$disk['smEvents']) : (isset($var['smEvents']) ? explode('|',$var['smEvents']) : $numbers); $type = isset($disk['smType']) ? $disk['smType'] : -1; if ($type==-1) $type = isset($var['smType']) ? $var['smType'] : ''; if ($type) { $ports = []; if (!empty($disk['smDevice'])) $port = $disk['smDevice']; if (!empty($disk['smPort1'])) $ports[] = $disk['smPort1']; if (!empty($disk['smPort2'])) $ports[] = $disk['smPort2']; if (!empty($disk['smPort3'])) $ports[] = $disk['smPort3']; if ($ports) { $glue = isset($disk['smGlue']) ? $disk['smGlue'] : ','; $type .= ','.implode($glue,$ports); } } $file = "/var/local/emhttp/smart/$name"; exec("awk 'NR>7{print $1,$2,$4,$6,$9,$10}' $file 2>/dev/null", $codes); $item = 'smart'; foreach ($codes as $code) { if (!$code) continue; list($id,$class,$value,$thres,$when,$raw) = explode(' ',$code); $fail = strpos($when,'FAILING_NOW')!==false; if (!$fail && !in_array($id,$events)) continue; $word = str_replace(['_',' (-)'],[' ',''],strtolower("$class ($when)")); $ack = "$name.ack"; switch ($select) { case 0: $attr = "$name.$id"; $last = isset($saved[$item][$attr]) ? $saved[$item][$attr]*$level : 0; if ($raw>0 || $fail) { if ($raw>$last) { exec("$notify -e \"unRAID $text SMART health [$id]\" -s \"Warning [$server] - $word is $raw\" -d \"$info\" -i \"warning\""); $saved[$item][$attr] = $raw; unset($saved[$item][$ack]); } } else { if ($last>0) { exec("$notify -e \"unRAID $text SMART message [$id]\" -s \"Notice [$server] - $word returned to normal value\" -d \"$info\""); unset($saved[$item][$attr]); unset($saved[$item][$ack]); } } break; case 1: $attr = "$name.${id}n"; $last = isset($saved[$item][$attr]) ? $saved[$item][$attr] : 255; if (($thres>0 && $value<=$thres*$level) || $fail) { if ($value*($value>$thres?$level:1)<$last) { exec("$notify -e \"unRAID $text SMART health [$id]\" -s \"Warning [$server] - $word is $value\" -d \"$info\" -i \"warning\""); $saved[$item][$attr] = $value; unset($saved[$item][$ack]); } } else { if ($last<255) { exec("$notify -e \"unRAID $text SMART message [$id]\" -s \"Notice [$server] - $word returned to normal value\" -d \"$info\""); unset($saved[$item][$attr]); unset($saved[$item][$ack]); } } break; } } $file .= '.ssa'; if (!file_exists($file) || (time()-filemtime($file)>=$var['poll_attributes'])) exec("smartctl -n standby -H $type /dev/$port|grep -Pom1 '^SMART.*: \K[A-Z]+'|tr -d '\n' > $file"); } function check_usage($name,$used,$text,$info) { global $notify,$disks,$saved,$unraid,$server; if ($used == -1) return; $disk = &$disks[$name]; $warning = strlen($disk['warning']) ? $disk['warning'] : $unraid['display']['warning']; $critical = strlen($disk['critical']) ? $disk['critical'] : $unraid['display']['critical']; $warn = $used>=$critical && $critical>0 ? 'alert' : ($used>=$warning && $warning>0 ? 'warning' : ''); $item = 'used'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : 0; if ($warn) { if ($used>$last) { exec("$notify -e \"unRAID $text disk utilization\" -s \"".ucfirst($warn)." [$server] - $text is ".($warn=='alert'?'low on space':'high on usage')." (${used}%)\" -d \"$info\" -i \"$warn\""); $saved[$item][$name] = $critical>0 && $used<=$critical ? $critical : $used; } } else { if ($last) { exec("$notify -e \"unRAID $text message\" -s \"Notice [$server] - $text returned to normal utilization level\" -d \"$info\""); unset($saved[$item][$name]); } } } // check once a week for smart database updates $smartDB = '/usr/share/smartmontools/drivedb.h.lastcheck'; $interval = 3600*24*7; // one week if (!file_exists($smartDB) || (time()-filemtime($smartDB)>=$interval)) exec('/usr/sbin/update-smart-drivedb 1>/dev/null 2>&1'); // check array devices foreach ($disks as $disk) { $name = $disk['name']; if ($name=='flash' || substr($disk['status'],-3)=='_NP') continue; $text = my_disk($name).($name=='cache'||$name=='parity'?' disk':''); $info = !empty($disk['id']) ? "{$disk['id']} ({$disk['device']})" : "No device identification ({$disk['device']})"; // process disk temperature notifications check_temp($name,$disk['temp'],$text,$info); // process disk SMART notifications check_smart($name,$disk['device'],$text,$info); // process disk usage notifications check_usage($name,($disk['fsSize']?100-round(100*$disk['fsFree']/$disk['fsSize']):-1),$text,$info); // process disk operation notifications $warn = strtok($disk['color'],'-'); $item = 'disk'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : ""; switch ($warn) { case 'red': if ($warn!=$last) { $status = strtolower(str_replace(['NP_','_'],['',' '],$disk['status'])); exec("$notify -e \"unRAID $text error\" -s \"Alert [$server] - $text in error state ($status)\" -d \"$info\" -i \"alert\""); $saved[$item][$name] = $warn; } break; case 'yellow': if ($warn!=$last) { $status = $name=='parity' ? "parity-sync in progress" : "drive not ready, content being reconstructed"; exec("$notify -e \"unRAID $text error\" -s \"Warning [$server] - $text, $status\" -d \"$info\" -i \"warning\""); $saved[$item][$name] = $warn; } break; default: if ($last) { exec("$notify -e \"unRAID $text message\" -s \"Notice [$server] - $text returned to normal operation\" -d \"$info\""); unset($saved[$item][$name]); } break;} // count disk errors if ($disk['numErrors']>0) $errors[] = "$text - $info (errors {$disk['numErrors']})"; } // check unassigned devices foreach ($devs as $dev) { $name = $dev['device']; if (empty($name)) continue; $smart = "/var/local/emhttp/smart/$name"; if (!file_exists($smart) || (time()-filemtime($smart)>=$var['poll_attributes'])) exec("smartctl -n standby -A /dev/$name > $smart"); $temp = exec("awk '\$1==190||\$1==194{print \$10;exit}' $smart"); $text = "device $name"; $info = !empty($dev['id']) ? "{$dev['id']} ($name)": "No device identification ($name)"; // process disk temperature notifications check_temp($name,$temp,$text,$info); // process disk SMART notifications check_smart($name,$dev['device'],$text,$info); } // report array read errors $item = 'array'; $name = 'errors'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : 0; $warn = count($errors); $info = "Array has $warn disk".($warn==1 ? "" : "s")." with read errors"; if ($warn>0) { if ($warn<>$last) { $message = implode('\n', $errors); exec("$notify -e \"unRAID array errors\" -s \"Warning [$server] - array has errors\" -d \"$info\" -m \"$message\" -i \"warning\""); $saved[$item][$name] = $warn; } } else { if ($last) { exec("$notify -e \"unRAID array errors\" -s \"Notice [$server] - array turned good\" -d \"$info\""); unset($saved[$item][$name]); } } // process parity check, parity sync and data-rebuild notifications $name = 'parity'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : ''; if ($var['mdResync']>0) { if (!$last) { if (strstr($var['mdResyncAction'],"recon")) { $last = 'Parity sync / Data rebuild'; } elseif (strstr($var['mdResyncAction'],"clear")) { $last = 'Disk clear'; } elseif ($var['mdResyncAction']=="check") { $last = 'Read check'; } elseif (strstr($var['mdResyncAction'],"check")) { $last = 'Parity check'; } $info = "Size: ".my_scale($var['mdResyncSize']*1024, $unit)." $unit"; exec("$notify -e \"unRAID $last\" -s \"Notice [$server] - $last started\" -d \"$info\" -i \"warning\""); $saved[$item][$name] = $last; } } else { if ($last) { $duration = $var['sbSynced2'] - $var['sbSynced']; $status = $var['sbSyncExit']; $speed = $status==0 ? my_scale($var['mdResyncSize']*1024/$duration,$unit,1)." $unit/s" : "Unavailable"; list($entry,$duration,$speed,$status) = explode('|', read_write_parity_log($var['sbSynced2'],$duration,$speed,$status)); $info = $status==0 ? "Duration: ".my_check($duration, $speed) : ($status==-4 ? "Canceled" : "Error code: $status"); $level = ($status==0 && $var['sbSyncErrs']==0) ? "normal" : "warning"; exec("$notify -e \"unRAID $last\" -s \"Notice [$server] - $last finished ({$var['sbSyncErrs']} errors)\" -d \"$info\" -i \"$level\""); unset($saved[$item][$name]); } } // check read-write status of USB flash drive $name = 'flash'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : ''; $warn = exec("grep -Pom1 '/boot \S+ \K\S{2}' /proc/mounts"); $info = "{$disks['flash']['id']} ({$disks['flash']['device']})"; if ($warn!="rw") { if ($warn!=$last) { exec("$notify -e \"USB flash drive failure\" -s \"Alert [$server] - USB drive is not read-write\" -d \"$info\" -i \"alert\""); $saved[$item][$name] = $warn; } } else { if ($last) { exec("$notify -e \"USB flash drive operation\" -s \"Notice [$server] - USB drive returned to normal operation\" -d \"$info\""); unset($saved[$item][$name]); } } // check docker image disk utilization system('mountpoint -q /var/lib/docker', $retval); if ($retval===0) { $item = 'system'; $name = 'docker'; $last = isset($saved[$item][$name]) ? $saved[$item][$name] : ''; if (file_exists("/boot/config/docker.cfg")) { $cfg = parse_ini_file("/boot/config/docker.cfg"); $info = "Docker utilization of image file {$cfg['DOCKER_IMAGE_FILE']}"; } else $info = "Docker image file not specified"; $warn = exec("df /var/lib/docker|awk '/^\//{print $5*1}'"); if ($warn>=$high1 && $high1>0) { if ($warn>$last) { exec("$notify -e \"Docker critical image disk utilization\" -s \"Alert [$server] - Docker image disk utilization of ${warn}%\" -d \"$info\" -i \"alert\""); $saved[$item][$name] = $warn; } } else if ($warn>=$high2 && $high2>0) { if ($warn>$last) { exec("$notify -e \"Docker high image disk utilization\" -s \"Warning [$server] - Docker image disk utilization of ${warn}%\" -d \"$info\" -i \"warning\""); $saved[$item][$name] = $warn; } } else { if ($last) { exec("$notify -e \"Docker image disk utilization\" -s \"Notice [$server] - Docker image disk utilization returned to normal level\" -d \"$info\""); unset($saved[$item][$name]); } } } // save new status if ($saved) { $text = ''; foreach ($saved as $item => $block) { if ($block) $text .= "[$item]\n"; foreach ($block as $key => $value) $text .= "$key=\"$value\"\n"; } if ($text) { if ($text != @file_get_contents($ram)) file_put_contents($ram, $text); if (!file_exists($rom) || exec("diff -q $ram $rom")) file_put_contents($rom, $text); } else { @unlink($ram); @unlink($rom); } } exit(0); ?>