Convert df -h output to html table

rajeshkumar created the topic: convert df -h output to html table

Code shared on permonks by choroba.

#!/usr/bin/perl
use warnings;
use strict;

use Text::Table;
use constant CAPACITY => 3;

my @lines = `df -hP`;
my $header = shift @lines;
# Do not create two columns for "Mounted" and "On"
$header =~ s/Mounted /Mounted_/;
my $table = Text::Table->new(split ' ', $header);
{
no warnings 'numeric'; # Ignore % signs
@lines = sort { $a->[CAPACITY] <=> $b->[CAPACITY] }
map [split ' ', $_], @lines;
}
$table->load(@lines);
print $table;

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re: convert df -h output to html table

There is one i made it working as below;

#############################################
#!/usr/local/bin/perl

# Use either -h or -k flag or leave it blank for default (-k)
# -h for human-readable size output
# -k for 1k-block output
$flag = "-h";
@df = `df $flag`;

print "Content-type: text/htmln\n";
print "

\n";
print "

\n";

print "

\n";

if ($flag eq "-h") {
print "

\n";
}
else {
print "

\n";
}

print "

\n";
print "

\n";
print "

\n";
print "

\n";
print "

\n";

foreach $line (@df) {
next if ($line =~ /Filesystem/);

($fsystem,$blocks,$used,$avail,$capacity,$mounted) = split(/\s+/,$line);

print "fsystem is $fsystem\n";
print "blocks is $blocks\n";
print "used is $used\n";
print "avail is $avail\n";
print "capacity is $capacity\n";
print "mounted is $mounted\n";

($number,$sign) = split(/%/,$capacity);
if ($number < 60) { print "

\n";
}
elsif (($number >= 60) && ($number < 90)) { print " \n";
}
else {
print "

\n";
}
#
print "

\n";
print "

\n";
print "

\n";
print "

\n";
print "

\n";
print "

\n";
print "

\n";
}

print "

Filesystem Size 1k-blocks Used Avail Capacity Mounted on
$fsystem $blocks $used $avail $capacity $mounted

\n";

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Df command usage

rajeshkumar created the topic: df command usage
Filesystems using more than 90% capacity

df -hP | awk '{x=$5;sub ("%","",x)}x>75'
df -h | awk '{if(NF==1){x=$0;getline;if(int($4)>90)print x,$0}else if(int($5)>90) print}'

ssh -q rajesh 'df -hP' | awk '{x=$5;sub ("%","",x)}x> 30'
ssh -q rajesh 'df -h' | awk '{if(NF==1){x=$0;getline;if(int($4)>60)print x,$0}else if(int($5)>60) print}'

Error:
df: unknown option: P
Usage: df [-F FSType] [-abeghklntVvZ] [-o FSType-specific_options] [directory | block_device | resource]

Solution:

May be your solaris version is not supporting P options. Check it out without using -P.

df output filter based on Mounted on and Capacity

df -kh | awk '/\/export/ && int($5) >= 90'

Here Capacity – 90%
Mounted on – \export
df -kh | \ # Pipe the output of df to awk
awk ‘ # Start the awk script
/\/mnt/ \ # Grep for pattern(first condition)
&& \ # add a second condition (and)
int($5) >= 85 \ # Check if integer of $5 is greater that 85(second condition)
‘ # End awk script

Df Output to one line

ssh -q $server 'df -hP | cat' 2>&1

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :