My supervisor needed to know how much space was on each of our servers.
I knew what I needed was the df-k from each server, but that can be
a lot of stuff. Welcome to Perl. So here's the quicky df-k grinder
#!/usr/local/bin/perl
#
# This is a unix disk space report generator. It takes df -k's and sp
+its out
# one report that gives each host's name, the total drive space, t
+otal free
# space available, and the percentage of free space on the system.
+ This is
# total space, not separated by drive. Swap space not counted, nor
+ are NFS
# mounted file systems. It supports Solaris and Linux, but not AIX
+. To use
# each host must have a separate file named for the host (df -k >
+HOSTNAME)
# as that is the way it will be called from the command line. I ca
+n login
# NIS and just create some of the files. I can log in as root on s
+ome
# non-NIS machines and use mail (dk -k > HOSTNAME; cat HOSTNAME |
+mail hck)
# have several systems that do not have mail access, but I can sft
+p. I put
# all the files in one directory and call the script ./disk.pl hos
+ts/*
# Calling the hosts by name from the command line also allows me t
+o get a
# report of just one host, or a selection of hosts.
#
#A typical df -k looks like this:
#############################################################
#Filesystem 1k-blocks Used Available Use% Mounted on
#/dev/dsk/c0t0d0s0 8061757 6306497 1674643 79% /
#/dev/dsk/c0t0d0s1 380815 121164 221570 35% /var
#swap 534400 216 534184 0% /tmp
#nfs.home.edu:/mounts/pluto4/vol4/u04
# 43505106 39604270 3465785 92% /u04
#nfs.home.edu:/export/sun4/SunOS5.7/usr/x11
# 69607865 54714403 14197384 79% /usr/x11
#nfs.home.edu:/export/sun4/SunOS5.7/usr/misc
# 69607865 54714403 14197384 79% /usr/misc
#nfs.home.edu:/mounts/pluto4/vol4/u04/hck
# 43505106 39604270 3465785 92% /home/hck
#/vol/dev/dsk/c0t2d0/sol_8_sparc_2
# 115408 115408 0 100% /cdrom/sol_8_
+sparc_2
#mail.home.edu:/var/mail
# 51998176 34547199 16930996 67% /var/mail
#nfs.home.edu:/export/sun4/SunOS5.7/usr/perl-5.6
# 41562853 38366501 2780724 93% /usr/perl-5.6
#nfs.home.edu:/export/sun4/SunOS5.7/usr/gnu
# 69607865 54714403 14197384 79% /usr/gnu
#############################################################
#So we know some things about it fast. The only lines of interest are
+the ones
# that start with "/" CDROM drives stick out, but could be edited out
+ if
# needed. NFS drives match the != /\// so they're gone. Headers from
+mail get
# wild, but they match != /\// also. Blank lines are the same. The fi
+elds we
# care about are the second and fourth. So we grab them and dump the
+rest!
#
use strict;
use diagnostics;
my $host;
my $line;
#make a tmp file to read from
open WRITEIT, ">report";
#All relevant hosts have been put on the command line (maybe hosts/*)
#This will generate our data file. I like to see the raw data sometime
+s.
while($host = pop @ARGV)
{
chomp $host;
open (READIT, "<$host") || die "\n can\'t open the host file: $!\n"
+;
print WRITEIT "start of $host\n";
while($line = <READIT>)
{
if($line !~ /^\/.*/){next;}
if($line =~ /^\/proc.*/){next;}#Proc is not of interest
$line =~ s/^([^ ]*) *([^ ]*) *[^ ]* *([^ ]*) *[^ ]* (.*)/$2 $3/;
print WRITEIT "$line";
}
print WRITEIT "end of $host\n";
close READIT;
}
close WRITEIT;
#Now read the data and spit out a report.
open READIT, "<report";
open WRITEIT, ">space";
my @numbers = qw(0 0); #numbers from the data file line
my @space = qw(0 0); #space derived from numbers
while($line = <READIT>)
{
chomp $line;
#If this is the beginning of a host report, get the host name
# and make the first entry. Then clear the space variables.
if($line =~ /^start of.*/)
{
$line =~ s/start of (.*)/$1/;
$host = $line;
$host =~ s/hosts\/(.*)/$1/;
print WRITEIT "Space report for $host\n";
@numbers = qw(0 0);
@space = qw(0 0);
next;
}
#if this is disk line grab the numbers and add them to the totals
if($line =~ /^[0-9].*/)
{
@numbers = split " ", $line;
$space[0] = $space[0] + $numbers[0];
$space[1] = $space[1] + $numbers[1];
next;
}
#if this the end of a host report, write the totals with a percenta
+ge
if($line =~ /^end of.*/)
{
my $percentage = $space[1] / $space[0];
#OK so this is a cheap way to format a number, but I didn't want
+ to
# change print statements for one line.
$percentage =~ s/..(..).*/$1%/;
print WRITEIT "Total space on $host = $space[0]\n";
print WRITEIT "Total space available $host = $space[1]\n";
print WRITEIT "Percentage of free space is $percentage\n";
print WRITEIT "\n\n";
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.