Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: 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"; } }

In reply to Unix disk space reports by halxd2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2019-12-13 03:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?