Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unix disk space reports

by halxd2 (Monk)
on Dec 15, 2000 at 02:15 UTC ( [id://46730]=CUFP: print w/replies, xml ) Need Help??

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"; } }

Replies are listed 'Best First'.
Re: Unix disk space reports
by salvadors (Pilgrim) on Dec 31, 2000 at 02:56 UTC
    This seems much too complicated for my liking.

    I really don't like the way it needs to create a temporary file, and that it writes it output out to a fixed file, rather than STDOUT, thus letting me redirect it, or play with it in some other way. I'd go for something more along the lines of:

    use strict; my %space; while (<>) { next unless m#^/#; next if m#^/proc#; next unless m#^[^ ]* *([^ ]*) *[^ ]* *([^ ]*) *[^ ]*#; $space{$ARGV}{total} += $1; $space{$ARGV}{free} += $2; } foreach my $host (sort keys %space) { printf "%s: %dk free, out of %dk, (%.2f%%)\n", $host, $space{$host}{free}, $space{$host}{total}, $space{$host}{free} / $space{$host}{total} * 100; }
    Then I'd call it as: perl ~/dfk hosts/* > report

    Playing with the format of the output is then trivial.

    If you really want to be able to view the intermediate 'space' file, then you could add a command line parameter for it (including the name of the output file?).

    Tony

      I agree my script does limit general usage; however, I was working to get a report that had the format I wanted for my boss. As for keeping the temp files I usualy keep them until I'm very sure I'm getting everything I what out of the script. I guess I just live in debugland.
        I want the same kind of report which has the capablity of showing the diskpace status for the past month. Can some help?
    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://46730]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-19 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found