Files
server_status/device.php
T
2026-10-15 17:17:16 +07:00

124 lines
3.1 KiB
PHP

<?php
if ($_GET["name"] == null) {
header("Location: /");
exit();
}
function get_data()
{
$cmd = ["sudo", "-n", "/usr/local/sbin/smartctl-proxy.sh", $_GET["name"]];
$proc = proc_open($cmd, [1 => ["pipe", "w"]], $pipes);
if (!is_resource($proc)) {
return;
}
$data = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$retval = proc_close($proc);
if ($retval == 0) {
return json_decode($data);
}
return $data . "\nProcess exited with $retval";
}
const NVME_DATA_UNIT = 1000 * 512;
const GIGABYTE = 10 ** 9;
const TERABYTE = 10 ** 12;
function data_unit($data)
{
$num = $data * NVME_DATA_UNIT;
if ($num < TERABYTE) {
return strval(round($num / GIGABYTE, 2)) . " GB";
}
return strval(round($num / TERABYTE, 2)) . " TB";
}
$data = get_data();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="sakura.css">
<title>Block Device Status</title>
</head>
<body>
<h1><?= $_GET["name"] ?></h1>
<?php if (is_string($data)) {
echo "<pre>";
echo $data;
echo "</pre>";
} else {
?>
<table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>smartctl <?= implode(
".",
$data->smartctl->version,
) ?></td>
<td></td>
</tr>
<tr>
<td>Model Name</td>
<td><?= $data->model_name ?></td>
</tr>
<tr>
<td>Temperature</td>
<td><?= $data->temperature->current ?> Celcius</td>
</tr>
</tbody>
</table>
<h2>SMART data</h2>
<table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<?php if ($data->device->type == "nvme") {
$health = $data->nvme_smart_health_information_log; ?>
<tr>
<td>Data Units Read</td>
<td><?= number_format($health->data_units_read) .
" (" .
data_unit($health->data_units_read) .
")" ?></td>
</tr>
<tr>
<td>Data Units Written</td>
<td><?= number_format($health->data_units_written) .
" (" .
data_unit($health->data_units_written) .
")" ?></td>
</tr>
<?php
} ?>
<tr></tr>
</tbody>
</table>
<details>
<summary>Original Output</summary>
<pre><?= implode("\n", $data->smartctl->output) ?></pre>
</details>
<?php
} ?>
</body>
</html>