Skip to content

Commit

Permalink
Part of #346 and #344
Browse files Browse the repository at this point in the history
  • Loading branch information
olehj authored Jan 16, 2025
1 parent f84902f commit db116e6
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 355 deletions.
75 changes: 13 additions & 62 deletions disklocation/pages/cron_disklocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
require_once("functions.php");

$sql_loop = "";
$smart_log = "";

$time_start = hrtime(true);
Expand Down Expand Up @@ -230,55 +229,26 @@
if($force_scan_db) {
$filename_device_data = UNRAID_CONFIG_PATH . "/" . DISKLOCATION_PATH . "/devices.json";
$devices_current = ( file_exists($filename_device_data) ? json_decode(file_get_contents($filename_device_data), true) : null );
print_r($devices_current);

$smart_model_family = ( $smart_array["scsi_product"] ? $smart_array["scsi_product"] : ( $smart_array["product"] ?: $smart_array["model_family"] ) );
$smart_cache = get_smart_cache("" . $unraid_array[$lsscsi_devicenode[$i]]["smart_controller_cmd"] . " " . ( !preg_match("/dev/", "foo-" . $unraid_array[$lsscsi_devicenode[$i]]["smart_controller_cmd"] . "") ? $lsscsi_devicenodesg[$i] : "" ) . "");

debug_print($debugging_active, __LINE__, "HASH", "#:" . $i . ":" . $deviceid[$i] . "");

find_and_unset_reinserted_devices_status($db, $deviceid[$i]); // tags old existing devices with 'null', delete device from location just in case it for whatever reason it already exists.
find_and_unset_reinserted_devices_status($devices_current, $deviceid[$i]); // tags old existing devices with 'null', delete device from location just in case it for whatever reason it already exists.

if(isset($smart_array["serial_number"]) && $smart_model_name) {
$devices[$deviceid[$i]] = array(
"device" => ($lsscsi_device[$i] ?? null),
"devicenode" => ($lsscsi_devicenode[$i] ?? null),
"model_name" => ( !file_exists($filename_device_data) ? $smart_model_name : $devices_current[$deviceid[$i]]["model_name"]),
"smart_serialnumber" => ( !file_exists($filename_device_data) ? $smart_array["serial_number"] : $devices_current[$deviceid[$i]]["smart_serialnumber"] ),
"smart_cache" => ($smart_cache ?? null),
"status" => ( !file_exists($filename_device_data) ? 'h' : $devices_current[$deviceid[$i]]["status"] )
);
}

$sql_loop .= "
INSERT INTO
disks(
device,
devicenode,
model_name,
smart_serialnumber,
smart_cache,
status,
hash
)
VALUES(
'" . ($lsscsi_device[$i] ?? null) . "',
'" . ($lsscsi_devicenode[$i] ?? null) . "',
'" . ($smart_model_name ?? null) . "',
'" . ($smart_array["serial_number"] ?? null) . "',
'" . ($smart_cache ?? null) . "',
'h',
'" . ($deviceid[$i] ?? null) . "'
)
ON CONFLICT(hash) DO UPDATE SET
device='" . ($lsscsi_device[$i] ?? null) . "',
devicenode='" . ($lsscsi_devicenode[$i] ?? null) . "',
smart_cache='" . ($smart_cache ?? null) . "'
WHERE hash='" . $deviceid[$i] . "'
;
";
"device" => ($lsscsi_device[$i] ?? null),
"devicenode" => ($lsscsi_devicenode[$i] ?? null),
"model_name" => ( !file_exists($filename_device_data) ? $smart_model_name : $devices_current[$deviceid[$i]]["model_name"]),
"smart_serialnumber" => ( !file_exists($filename_device_data) ? $smart_array["serial_number"] : $devices_current[$deviceid[$i]]["smart_serialnumber"] ),
"smart_cache" => ($smart_cache ?? null),
"status" => ( !file_exists($filename_device_data) ? 'h' : $devices_current[$deviceid[$i]]["status"] )
);
}
else {
debug_print($debugging_active, __LINE__, "SQL", "#:" . $i . ":<pre>Invalid SMART information, skipping...</pre>");
debug_print($debugging_active, __LINE__, "DB", "#:" . $i . ":<pre>Invalid SMART information, skipping...</pre>");
}
}
}
Expand Down Expand Up @@ -307,9 +277,8 @@
$i++;
}

if($force_scan_db && $sql_loop) {
debug_print($debugging_active, __LINE__, "SQL", "#:<pre>" . $sql_loop . "</pre>");
//print("#:" . $i . ":<pre>" . $sql_loop . "</pre>");
if($force_scan_db) {
debug_print($debugging_active, __LINE__, "DB", "#:<pre>" . print_r($devices) . "</pre>");

if(!isset($argv) || !in_array("silent", $argv)) {
$smart_output = "\nWriting to the database... ";
Expand All @@ -321,27 +290,11 @@
if(!file_put_contents($filename_device_data, json_encode($devices, JSON_PRETTY_PRINT))) {
print("Could not save file " . $filename_device_data . "");
}

$ret = $db->exec($sql_loop);
if(!$ret) {
echo $db->lastErrorMsg();
}
else {
// check the existence of devices, must be run during force smart scan.
if($force_scan) {
find_and_set_removed_devices_status($db, $deviceid); // tags removed devices 'r', delete device from location
}
if(!isset($argv) || !in_array("silent", $argv)) {
$smart_output = "done in " . round((hrtime(true)-$time_start)/1e+9, 1) . " seconds.\n";
print($smart_output);
$smart_log .= $smart_output;
flush();
}
find_and_set_removed_devices_status($db, $deviceid);
}
}

$db->close();

if(isset($_GET["force_smartdb_scan"]) || $_GET["force_smart_scan"] || isset($_GET["active_smart_scan"])) {
if(!isset($argv) || !in_array("silent", $argv)) {
print("</pre>
Expand All @@ -358,8 +311,6 @@

}

cronjob_runfile_updater();

if($smart_log) {
file_put_contents(DISKLOCATION_TMP_PATH."/cron_smart.log", $smart_log);
}
Expand Down
8 changes: 7 additions & 1 deletion disklocation/pages/devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@
}

// DB $data:
$devices[$hash]["raw"]["tray"] = $data["tray"] ?? 0;
$devices[$hash]["formatted"]["tray"] = $devices[$hash]["raw"]["tray"];
$devices[$hash]["raw"]["groupid"] = $groupid;
$devices[$hash]["formatted"]["groupid"] = $devices[$hash]["raw"]["groupid"];
$devices[$hash]["raw"]["group_name"] = $array_groups[$groupid]["group_name"];
$devices[$hash]["formatted"]["group_name"] = $devices[$hash]["raw"]["group_name"];
$devices[$hash]["raw"]["device"] = $device;
$devices[$hash]["formatted"]["device"] = $devices[$hash]["raw"]["device"];
$devices[$hash]["raw"]["node"] = $devicenode;
Expand All @@ -278,7 +284,7 @@
$devices[$hash]["raw"]["model"] = $data["model_name"];
$devices[$hash]["formatted"]["model"] = $devices[$hash]["raw"]["model"];
$devices[$hash]["raw"]["cache"] = $data["smart_cache"];
$devices[$hash]["formatted"]["cache"] = "" . $devices[$hash]["raw"]["cache"] . "MB";
$devices[$hash]["formatted"]["cache"] = "" . ( $devices[$hash]["raw"]["cache"] ? $devices[$hash]["raw"]["cache"] . "MB" : null );
$devices[$hash]["raw"]["installed"] = $data["installed"];
$devices[$hash]["formatted"]["installed"] = $devices[$hash]["raw"]["installed"];
$devices[$hash]["raw"]["removed"] = $data["removed"];
Expand Down
Loading

0 comments on commit db116e6

Please sign in to comment.