[英]displaying disk uasge data for remote servers using php
我有一個 shell 腳本,可以將遠程服務器的磁盤使用數據寫入文本文件。 文本文件(diskusage.txt)的內容是:
10% 10GB
20% 20GB
30% 30GB
40% 40GB
50% 50GB
我使用一個 php 函數(line_functions.php)來讀取這個文件,函數是這樣的:
<?php
function getColLines1($col, $lines)
{
$lin1 = explode(' ', $lines[0]);
return $lin1[$col];
}
function getColLines2($col, $lines)
{
$lin2 = explode(' ', $lines[1]);
return $lin2[$col];
}
function getColLines3($col, $lines)
{
$lin3 = explode(' ', $lines[2]);
return $lin3[$col];
}
function getColLines4($col, $lines)
{
$lin4 = explode(' ', $lines[3]);
return $lin4[$col];
}
function getColLines5($col, $lines)
{
$lin5 = explode(' ', $lines[4]);
return $lin5[$col];
}
?>
現在,我在 html 表中顯示此數據,如下代碼所示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php
include 'line_functions.php';
$lines = file('/diskusage.txt');
$server1_root_pc=getColLines1(0, $lines).PHP_EOL; $server1_app_usedvstotal=getColLines1(1, $lines).PHP_EOL;
#displaying progress bar for disk.
if (trim($server1_root_pc)<="70%"){
echo '<div class="progress"><div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="'.$server1_root_pc.'"
aria-valuemin="0" aria-valuemax="100" style="width: '.$server1_root_pc.'" >
'.$server1_root_pc.' '.$server1_app_usedvstotal.' Used
</div></div>';}
elseif(trim($server1_root_pc) >"70%" && trim($server1_root_pc)<="80%"){
echo '<div class="progress"><div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="'.$server1_root_pc.'"
aria-valuemin="0" aria-valuemax="100" style="width: '.$server1_root_pc.'" >
'.$server1_root_pc.' '.$server1_app_usedvstotal.' Used
</div></div>';}
else {echo '<div class="progress"><div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="'.$server1_root_pc.'"
aria-valuemin="0" aria-valuemax="100" style="width: '.$server1_root_pc.'" >
'.$server1_root_pc.' '.$server1_app_usedvstotal.' Used
</div></div>';}
?>
</div>
現在,我不知道如何在 server2-server4 的表中顯示此數據,我可以對所有服務器使用相同的代碼,但這將是冗長且冗余的代碼,請您指導我如何循環此代碼所有服務器並通過一些循環顯示所有服務器的數據。
這應該有效:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php
$lines = file('/diskusage.txt');
foreach($lines as $line) {
$cols = explode(' ', $line);
$server_root_pc = $cols[0];
$server_app_usedvstotal = $cols[1];
$percentage = (int) str_replace("%", "", $server_root_pc);
$status = "success";
if ($percentage > 80) {
$status = "danger";
} elseif ($percentage > 70) {
$status = "warning";
}
echo '<div class="progress"><div class="progress-bar progress-bar-'.$status.'" role="progressbar" aria-valuenow="'.$server_root_pc.'"
aria-valuemin="0" aria-valuemax="100" style="width: '.$server_root_pc.'" >
'.$server_root_pc.' '.$server_app_usedvstotal.' Used
</div></div>';
}
?>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.