Retrieve with graph bars Unix filesystem informations through SSH
Retrieve with graph bars Unix filesystem informations through SSH

This PHP script can retrieve a filesystem information through SSH using these steps :

  • launch an SSH connection to a remote server
  • execute through SSH a “df” command
  • display the results using graph bars

This code was initially designed to display graphically the filesystem from Netapp NAS appliances.

index.css file is attached at the end of this page.

Script :

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>NAS filesystem informations</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="index.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
function netapp_ssh($host,$command){
$connection = ssh2_connect($host, 22);
$auth = ssh2_auth_password($connection, 'user', 'password');
$stream = ssh2_exec($connection, $command);
while(!feof($stream)) {
$line = fgets($stream);
if ($line<>""){
$line = preg_replace('/\s\s+/', ' ', $line);
$string_data = explode(" ", $line);

$str_aggrname = $string_data[0];
$str_aggrfreesize = $string_data[3];
$str_aggrfreesize_wo_GB = str_replace("GB","",$str_aggrfreesize);
$str_aggrpercentused = $string_data[4];
$str_aggrpercentused_wo_percent = str_replace("%","",$str_aggrpercentused);

if ($line_number >= 1 && strpos($str_aggrname,".snapshot")===false && ($str_aggrname<>"" || $str_aggrname<>null) && $str_aggrfreesize<>"") {
echo "<TR><TD>$host $str_aggrname</TD>";
echo "<TD colspan=\"5\">";
if ($str_aggrpercentused_wo_percent >= 95) {
$bg_color = "#FF3333";
}
elseif ($str_aggrpercentused_wo_percent < 95 && $str_aggrpercentused_wo_percent >= 80){
$bg_color = "#FEE13D";
}
else {
$bg_color = "#8dc63f";
}
displayProgressBar(7,450,$str_aggrpercentused_wo_percent,$str_aggrpercentused,$bg_color);
echo "<TD>$str_aggrfreesize</TD>";
}
$line_number = $line_number + 1 ;
}
}
}

function displayProgressBar($h,$w,$p,$f,$c) {
$w2 = $p * $w/100;
echo "<div class='progressBarBg' style='z-index:2;height:". $h ."px; width:". $w ."px'><div class='progressBar' style='z-index:3;position:absolute;background-color:". $c.";height:". $h."px; width:". $w2 ."px;'>$f</div></div>";
}
?>

<TABLE class = "sample">
<?php
//Hide Warning messages
error_reporting(0);

echo "<TR><TH colspan=\"7\"> NetApp Aggregate size information <BR></TH>";
echo "<TR>";
echo "<TH ><font Size=1,5> NAS Hostname";
echo "<TH colspan=\"5\"><font Size=1,5> Aggregate ";
echo "<TH colspan=\"1\"><font Size=1,5> Available ";

netapp_ssh("server01","df -Ag");
netapp_ssh("server02","df -Ag");
netapp_ssh("server03","df -Ag");

?></TABLE>
<br><br>
<center>
<input name=type type=button value="Refresh" onclick="javascript:location.reload()"/>
<input type=button name=type value="Close" onclick="self.close()">
</center>

</body>

 

<>

My Powershell script categories

Retrieve with graph bars Unix filesystem informations through SSH

Leave a Reply

Your email address will not be published.