Skip to content

Commit

Permalink
Overhaul '25 part 1
Browse files Browse the repository at this point in the history
Fixes #342 #343 #344
  • Loading branch information
olehj authored Jan 7, 2025
1 parent d151d5b commit a5778d4
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 627 deletions.
340 changes: 159 additions & 181 deletions disklocation/pages/config.php

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions disklocation/pages/config_system.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright 2019-2024, Ole-Henrik Jakobsen
* Copyright 2019-2025, Ole-Henrik Jakobsen
*
* This file is part of Disk Location for Unraid.
*
Expand Down Expand Up @@ -466,24 +466,30 @@ function disklocation_system($type, $operation, $file = "") {

$print_force_scan = "
<form action=\"" . DISKLOCATION_PATH . "/pages/config_system.php\" method=\"post\">
<b>Clicking the buttons will start collecting SMART data directly. It might take a few seconds to several minutes depending on the amount of devices it need to scan.</b>
<b>Clicking the buttons will update data directly. It might take a few seconds to several minutes depending on the amount of devices it need to scan.</b>
<br />
<input type='button' value='Update Active' onclick='openBox(\"" . CRONJOB_URL . "?active_smart_scan=1\",\"Updating Active Devices\",600,800,true,\"loadlist\",\":return\")'>
<input type='button' value='Force Update All' onclick='openBox(\"" . CRONJOB_URL . "?force_smart_scan=1\",\"Force Updating All Devices\",600,800,true,\"loadlist\",\":return\")'>
<input type='button' value='SMART' onclick='openBox(\"" . CRONJOB_URL . "?active_smart_scan=1\",\"Updating SMART data on active devices\",600,800,true,\"loadlist\",\":return\")'>
<input type='button' value='Force SMART' onclick='openBox(\"" . CRONJOB_URL . "?force_smart_scan=1\",\"Wake up all devices and update SMART data\",600,800,true,\"loadlist\",\":return\")'>
<input type='button' value='Force SMART+DB' onclick='openBox(\"" . CRONJOB_URL . "?force_smartdb_scan=1\",\"Wake up all devices and update SMART data and the database\",600,800,true,\"loadlist\",\":return\")'>
<!--<br />
<input type='submit' name=\"active_smart_scan\" value=\"Update Active\">
<input type='submit' name=\"force_smart_scan\" value=\"Force Update All\">-->
<blockquote class='inline_help'>
<ul>
<li>\"Update Active\" button will update only active (spinning) drives for SMART data, It might take a while to complete depending on your configuration.</li>
<li>You can also run \"Update Active\" from the shell and get direct output which might be useful for debugging:<br />
<li>\"SMART\" button will update only active (spinning) drives for SMART data, It might take a while to complete depending on your configuration.</li>
<li>You can also run \"SMART\" from the shell and get direct output which might be useful for debugging:<br />
<code style=\"white-space: nowrap;\">php -f /usr/local/emhttp/plugins/disklocation/pages/cron_disklocation.php cronjob [silent]</code></li>
</ul>
<ul>
<li>\"Force Update All\" button will force update all drives for SMART data and move removed disks into the \"lost\" table under the \"Information\" tab. This button will and must wake up all drives into a spinning state and does so one by one. It might take a while to complete depending on your configuration.</li>
<li>You can also run \"Force Update All\" from the shell and get direct output which might be useful for debugging:<br />
<li>\"Force SMART\" button will force update all drives for SMART data. This button will and must wake up all drives into a spinning state and does so one by one. It might take a while to complete depending on your configuration.</li>
<li>You can also run \"Force SMART\" from the shell and get direct output which might be useful for debugging:<br />
<code style=\"white-space: nowrap;\">php -f /usr/local/emhttp/plugins/disklocation/pages/cron_disklocation.php force [silent]</code></li>
</ul>
<ul>
<li>\"Force SMART+DB\" button will force update all drives for SMART data and move removed disks into the \"lost\" table under the \"Information\" tab. This button will and must wake up all drives into a spinning state and does so one by one. It might take a while to complete depending on your configuration.</li>
<li>You can also run \"Force Update All\" from the shell and get direct output which might be useful for debugging:<br />
<code style=\"white-space: nowrap;\">php -f /usr/local/emhttp/plugins/disklocation/pages/cron_disklocation.php forceall [silent]</code></li>
</ul>
</blockquote>
</form>
";
Expand Down
273 changes: 86 additions & 187 deletions disklocation/pages/cron_disklocation.php

Large diffs are not rendered by default.

275 changes: 85 additions & 190 deletions disklocation/pages/devices.php

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions disklocation/pages/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
define("DISKLOCATION_URL", "/Tools/disklocation");
define("DISKLOCATIONCONF_URL", "/Tools/disklocation");
define("DISKLOCATION_PATH", "/plugins/disklocation");
define("DISKLOCATION_TMP_PATH", "/tmp/disklocation");
define("DISKLOCATION_CONF", "" . UNRAID_CONFIG_PATH . "" . DISKLOCATION_PATH . "/disklocation.conf");
define("DISKLOCATION_DB_DEFAULT", "" . UNRAID_CONFIG_PATH . "" . DISKLOCATION_PATH . "/disklocation.sqlite");
define("DISKLOCATION_LOCK_FILE", "/tmp/disklocation/db.lock");
define("DISKLOCATION_LOCK_FILE", "" . DISKLOCATION_TMP_PATH . "/db.lock");
define("DISKINFORMATION", "/var/local/emhttp/disks.ini");
define("EMHTTP_ROOT", "/usr/local/emhttp");
define("CRONJOB_URL", DISKLOCATION_PATH . "/pages/cron_disklocation.php");
Expand Down Expand Up @@ -119,6 +120,8 @@ function __construct() {
$select_db_drives_default = $select_db_drives;
$sort_db_drives_default = $sort_db_drives;

$select_db_devices_default = $select_db_devices;

$css_serial_number_highlight_default = $css_serial_number_highlight;

$bgcolor_parity_default = $bgcolor_parity;
Expand All @@ -130,12 +133,11 @@ function __construct() {
$sql_status = "";

// get Unraid disks
$get_default_smEvents = "5|187|197|198|199"; // default Unraid smEvents
$get_global_smType = ( isset($unraid_smart_all["smType"]) ? $unraid_smart_all["smType"] : null );
/* Not in use yet
$get_global_smSelect = ( isset($unraid_smart_all["smSelect"]) ? $unraid_smart_all["smSelect"] : null );
$get_global_smEvents = ( isset($unraid_smart_all["smEvents"]) ? $unraid_smart_all["smEvents"] : null );
$get_global_smEvents = ( isset($unraid_smart_all["smEvents"]) ? $unraid_smart_all["smEvents"] : $get_default_smEvents );
$get_global_smCustom = ( isset($unraid_smart_all["smCustom"]) ? $unraid_smart_all["smCustom"] : null );
*/

if(is_array($unraid_disks) && is_array($unraid_devs)) {
$unraid_devs = array_values(array_merge($unraid_disks, $unraid_devs));
Expand Down Expand Up @@ -181,9 +183,9 @@ function __construct() {
"color" => ($unraid_devs[$i]["color"] ?? null),
"fscolor" => ($unraid_devs[$i]["fsColor"] ?? null),
"smart_controller_cmd" => ($smart_controller_devs[$i] ?? null),
"smart_select" => ($unraid_devs[$i]["smSelect"] ?? null),
"smart_events" => ($unraid_devs[$i]["smEvents"] ?? null),
"smart_custom" => ($unraid_devs[$i]["smCustom"] ?? null),
"smSelect" => ($unraid_smart_one[$getdeviceid]["smSelect"] ?? null),
"smEvents" => ($unraid_smart_one[$getdeviceid]["smEvents"] ?? null),
"smCustom" => ($unraid_smart_one[$getdeviceid]["smCustom"] ?? null),
);
}
$i++;
Expand Down Expand Up @@ -252,6 +254,11 @@ function bscode2html($text) {
}
}

function keys_to_content($input, $array) {
$input_array = explode(" ", $input);
return str_replace(array_keys($array), array_values($array), $input);
}

function get_table_order($select, $sort, $return = '0', $test = '') { // $return = 0: list() = multi-arrays || 1: SQL command variables || 2(column)/3(sort): validation + $test = string of valid inputs (eg. '1,1,0,0,....0')
$select = preg_replace('/\s+/', '', $select);
$sort = preg_replace('/\s+/', '', $sort);
Expand Down Expand Up @@ -374,6 +381,10 @@ function get_table_order($select, $sort, $return = '0', $test = '') { // $return
}
}

function check_device_table($input) {
return $input;
}

// function from: https://stackoverflow.com/questions/16251625/how-to-create-and-download-a-csv-file-from-php-script
function array_to_csv_download($array, $filename = "output.tsv", $delimiter="\t") {
// open raw memory as file so no temp files needed, you might run out of memory though
Expand Down
2 changes: 1 addition & 1 deletion disklocation/pages/info.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright 2019-2024, Ole-Henrik Jakobsen
* Copyright 2019-2025, Ole-Henrik Jakobsen
*
* This file is part of Disk Location for Unraid.
*
Expand Down
36 changes: 4 additions & 32 deletions disklocation/pages/sqlite_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,12 @@
'leddiskop' => 1,
'ledsmart' => 1,
'ledtemp' => 1,
'unraidinfo' => 1,
'path' => 0,
'devicenode' => 0,
'luname' => 0,
'manufacturer' => 1,
'devicemodel' => 1,
'serialnumber' => 1,
'temperature' => 1,
'powerontime' => 1,
'loadcyclecount' => 1,
'capacity' => 1,
'cache' => 1,
'rotation' => 1,
'formfactor' => 1,
'reallocated_sector_count' => 0,
'reported_uncorrectable_errors' => 0,
'command_timeout' => 0,
'current_pending_sector_count' => 0,
'offline_uncorrectable' => 0,
'available_spare' => 1,
'percentage_used' => 1,
'units_read' => 1,
'units_written' => 1,
'manufactured' => 0,
'purchased' => 0,
'installed' => 0,
'warranty' => 0,
'comment' => 0,
'hideemptycontents' => 0,
'flashwarning' => 0,
'flashcritical' => 1
));

$select_db_info = "group,tray,manufacturer,model,serial,capacity,cache,rotation,formfactor,read,written,manufactured,purchased,installed,warranty,comment";
$select_db_info = "group,tray,manufacturer,model,serial,capacity,cache,rotation,formfactor,manufactured,purchased,installed,warranty,comment";
$sort_db_info = "asc:group,tray";

// mandatory: group,tray,locate,color
Expand All @@ -97,7 +69,7 @@
$sort_db_drives = "asc:serial";

//not used, but prepared just in case it will be added in the future:
$select_db_devices = "";
$select_db_devices = "[huge]*pool*[/huge] name node capacity rotation formfactor [color:11ff00]*[serial]*[/color]\nmanufacturer model\ncomment";
$sort_db_devices = "";

// Group settings
Expand Down Expand Up @@ -689,7 +661,7 @@
";

// Create and update database
if(!in_array("cronjob", $argv) && !$_POST["download_csv"]) { print("<h3 style=\"color: #FF0000;\">"); }
if(!in_array("cronjob", $argv) && !$_POST["download_csv"]) { print("<!--<h3 style=\"color: #FF0000;\">-->"); }
if(filesize(DISKLOCATION_DB) === 0) {
$sql = "
CREATE TABLE disks(
Expand Down Expand Up @@ -1100,7 +1072,7 @@
}
}

if(!in_array("cronjob", $argv) && !$_POST["download_csv"]) { print("</h3>"); }
if(!in_array("cronjob", $argv) && !$_POST["download_csv"]) { print(""); }
if($db_update == 1) {
print("<h3>Database successfully updated</h3>");
if(!in_array("cronjob", $argv)) {
Expand Down
32 changes: 11 additions & 21 deletions disklocation/pages/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@
if(!preg_match("/(u|m)/", $_POST["warranty_field"])) { $disklocation_error[] = "Warranty field is invalid."; }
if(!preg_match("/[0-9]{1,4}/", $_POST["dashboard_widget_pos"])) { $disklocation_error[] = "Dashboard widget position invalid."; }

if(!preg_match("/[0-9]{1,5}/", $_POST["reallocated_sector_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
if(!preg_match("/[0-9]{1,5}/", $_POST["reported_uncorr_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
if(!preg_match("/[0-9]{1,5}/", $_POST["command_timeout_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
if(!preg_match("/[0-9]{1,5}/", $_POST["pending_sector_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
if(!preg_match("/[0-9]{1,5}/", $_POST["offline_uncorr_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
//if(!preg_match("/[0-9]{1,5}/", $_POST["reallocated_sector_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
//if(!preg_match("/[0-9]{1,5}/", $_POST["reported_uncorr_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
//if(!preg_match("/[0-9]{1,5}/", $_POST["command_timeout_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
//if(!preg_match("/[0-9]{1,5}/", $_POST["pending_sector_w"])) { $disklocation_error[] = "SMART: Invalid number."; }
//if(!preg_match("/[0-9]{1,5}/", $_POST["offline_uncorr_w"])) { $disklocation_error[] = "SMART: Invalid number."; }

/*
$dashboard_widget_array = dashboard_toggle($_POST["dashboard_widget"], $_POST["dashboard_widget_pos"]);
Expand Down Expand Up @@ -193,12 +193,12 @@
$get_table_order_drives .= get_table_order($_POST["select_db_drives"], $_POST["sort_db_drives"], 3, "0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
if($get_table_order_drives) { $disklocation_error[] = "Table \"History\": " . $get_table_order_drives; }

/* // Currently not in use
// Devices
if(empty($_POST["select_db_devices"])) { $_POST["select_db_devices"] = $select_db_devices_default; }
if(empty($_POST["sort_db_devices"])) { $_POST["sort_db_devices"] = $sort_db_devices_default; }
$get_table_order_devices = get_table_order($_POST["select_db_devices"], $_POST["sort_db_devices"]);
if(!is_array($get_table_order_devices)) { $disklocation_error[] = "Table \"Devices\": " . $get_table_order_devices; }
*/
//if(empty($_POST["sort_db_devices"])) { $_POST["sort_db_devices"] = $sort_db_devices_default; }
$get_table_order_devices = check_device_table($_POST["select_db_devices"]);
if(!$get_table_order_devices) { $disklocation_error[] = "Table \"Devices\": " . $get_table_order_devices; }

if(empty($disklocation_error)) {
$sql .= "
REPLACE INTO
Expand All @@ -216,11 +216,6 @@
warranty_field,
dashboard_widget,
dashboard_widget_pos,
reallocated_sector_w,
reported_uncorr_w,
command_timeout_w,
pending_sector_w,
offline_uncorr_w,
css_serial_number_highlight,
displayinfo,
select_db_info,
Expand All @@ -246,11 +241,6 @@
'" . $_POST["warranty_field"] . "',
'" . SQLite3::escapeString($_POST["dashboard_widget"] ?? null) . "',
'" . $_POST["dashboard_widget_pos"] . "',
'" . $_POST["reallocated_sector_w"] . "',
'" . $_POST["reported_uncorr_w"] . "',
'" . $_POST["command_timeout_w"] . "',
'" . $_POST["pending_sector_w"] . "',
'" . $_POST["offline_uncorr_w"] . "',
'" . SQLite3::escapeString($_POST["css_serial_number_highlight"] ?? $css_serial_number_highlight_default) . "',
'" . $post_info . "',
'" . SQLite3::escapeString($_POST["select_db_info"] ?? $select_db_info_default) . "',
Expand All @@ -259,7 +249,7 @@
'" . SQLite3::escapeString($_POST["sort_db_trayalloc"] ?? $sort_db_trayalloc_default) . "',
'" . SQLite3::escapeString($_POST["select_db_drives"] ?? $select_db_drives_default) . "',
'" . SQLite3::escapeString($_POST["sort_db_drives"] ?? $sort_db_drives_default) . "',
'" . SQLite3::escapeString($_POST["select_db_devices"] ?? $select_db_devices_default) . "',
'" . SQLite3::escapeString($get_table_order_devices ?? $select_db_devices_default) . "',
'" . SQLite3::escapeString($_POST["sort_db_devices"] ?? $sort_db_devices_default) . "'
)
;
Expand Down

0 comments on commit a5778d4

Please sign in to comment.