简体   繁体   English

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

[英]displaying disk uasge data for remote servers using php

I have a shell script that writes remote server's disk usage data to a text file.我有一个 shell 脚本,可以将远程服务器的磁盘使用数据写入文本文件。 The content of text file(diskusage.txt) is:文本文件(diskusage.txt)的内容是:

10% 10GB
20% 20GB
30% 30GB
40% 40GB
50% 50GB

I use a php function(line_functions.php) to read this file, the function is like:我使用一个 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];
}
?>

now, I display this data in html table like below code:现在,我在 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>

Now, I have no idea on how to display this data in table for server2-server4, I can use the same code for all the servers, but it will be lengthy and redundant code, can you please guide me on how loop this code for all servers and display data for all servers through some loops.现在,我不知道如何在 server2-server4 的表中显示此数据,我可以对所有服务器使用相同的代码,但这将是冗长且冗余的代码,请您指导我如何循环此代码所有服务器并通过一些循环显示所有服务器的数据。

This should work:这应该有效:

<!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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM