简体   繁体   中英

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. The content of text file(diskusage.txt) is:

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
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:

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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