Files
server_status/device.php
T

105 lines
2.2 KiB
PHP

<?php
require "components/nvme.php";
require "components/sat.php";
if ($_GET["name"] == null) {
header("Location: /");
exit();
}
function get_data()
{
$cmd = [
"sudo",
"-n",
"smartctl",
"--json=oc",
"-a",
"/dev/" . $_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);
return json_decode($data);
}
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>
<?php if ($data->device->type == "nvme") {
nvme_data($data);
} elseif ($data->device->type == "sat") {
sat_data($data);
} ?>
<details>
<summary>Original Output</summary>
<pre><?= implode("\n", $data->smartctl->output) ?></pre>
</details>
<?php
} ?>
</body>
</html>