使用php显示远程服务器的磁盘uasge数据

问题描述 投票:0回答:1

我有一个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的表中显示此数据,我可以对所有服务器使用相同的代码,但这将是冗长和冗余的代码,请您指导我如何循环此代码所有服务器的代码,并通过一些循环显示所有服务器的数据。

php html
1个回答
0
投票

这应该起作用:

<!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>
© www.soinside.com 2019 - 2024. All rights reserved.