#!/usr/local/bin/perl # # This is a unix disk space report generator. It takes df -k's and spits out # one report that gives each host's name, the total drive space, total 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 can login # NIS and just create some of the files. I can log in as root on some # 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 sftp. I put # all the files in one directory and call the script ./disk.pl hosts/* # Calling the hosts by name from the command line also allows me to 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 fields 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 sometimes. 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 = ) { 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, "space"; my @numbers = qw(0 0); #numbers from the data file line my @space = qw(0 0); #space derived from numbers while($line = ) { 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 percentage 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"; } }