FAQ
Hello!

I am new to the list, so forgive me if this is not the correct forum. I am
writing (attempting to anyway) a script that will look at the size of the
filesystem, and warn me if it is getting too full, say 90% percent or so.

Initially I though that combining the df -k output of the system with awk
would work:

#!/usr/bin/perl

$percent = `df -k | awk '{print $5}'`;

chomp($percent);
chop($percent); # To get rid of the %

if($percent > "90") {

do something
}

I though that this would work, but I didn't get just the % column from the
awk statment. I am really trying
to do this all in perl instead of mixing it with awk.

Any ideas?

-James

Search Discussions

  • Andrea Holstein at Nov 26, 2001 at 11:34 pm

    James Kelty wrote:

    Hello!

    I am new to the list, so forgive me if this is not the correct forum. I am
    writing (attempting to anyway) a script that will look at the size of the
    filesystem, and warn me if it is getting too full, say 90% percent or so.

    Initially I though that combining the df -k output of the system with awk
    would work:

    #!/usr/bin/perl

    $percent = `df -k | awk '{print $5}'`;

    chomp($percent);
    chop($percent); # To get rid of the %

    if($percent > "90") {

    do something
    }

    I though that this would work, but I didn't get just the % column from the
    awk statment. I am really trying
    to do this all in perl instead of mixing it with awk.
    It's better to know what you got, instead to know what you didn't got.

    However, before awk can work, perl substitues $5 with it's value (which
    is surely undef).
    So you should quote it:
    $percent = `df -k | awk '{print \$5}'`;

    or use the non-interpolation form
    $percent = qx'df -k | awk \'{print $5}\'';

    You would have recogniced it when running under use warnings,
    as you should always do :-)

    Best Wishes,
    Andrea
  • Jonathan E. Paton at Nov 26, 2001 at 11:35 pm
    Hi,

    I've just about solved the problem for you... I am
    obviously a student with too much time on his hands ;-)
    However, it is fairly complicated for a beginner example,
    but should be quite interesting for the more experienced
    "beginner" (like me :)

    The line with 'split' is what you are looking for, a simple
    variation would be:

    my $capacity = (split /\s+/)[4];

    I hope you can follow what is happening, at least in a
    primative sense. I didn't bother to sort the devices,
    since this is a hard thing to do correctly.

    If anything, learn that you can create concise and
    readable(?) scripts. Take care,

    Jonathan Paton

    --- PERL SCRIPT STARTS HERE ---
    #!/usr/bin/perl -w

    use strict;

    # Call df function for filesystem information
    my %df = %{ df() };

    # Check capacity
    while (my ($device, $data) = each (%df)) {
    my $capacity = $data->{capacity};
    do_something($device, $capacity) if (90 < $capacity);
    }

    # Do something about being full
    sub do_something {
    my ($device, $capacity) = @_;
    print "Warning: Device $device has just $capacity\%
    left!\n";
    }

    # Extract information from df
    # and return a hash reference to data
    sub df {

    # Create hash for all the data retrieved from df
    my %df;

    # Call and process output of df
    foreach( qx(df -k) ) {

    # Skip non-data lines
    next if /^File/;

    # Extract data from this row
    my ($device, $blocks, $used, $available, $capacity,
    $mountpoint) = split /\s+/;

    # Remove newline
    chomp($mountpoint);

    # Remove percent symbol
    $capacity =~ s/\%.*//;

    # Stuff an anonymous hash containing row data into %df
    $df{$device} = { "blocks" => $blocks,
    "used" => $used,
    "available" => $available,
    "capacity" => $capacity,
    "mountpoint" => $mountpoint
    };
    }
    return \%df;
    }
    --- PERL SCRIPT ENDS HERE ---


    __________________________________________________
    Do You Yahoo!?
    Everything you'll ever need on one web page from News and Sport to Email and Music Charts
    http://uk.my.yahoo.com
  • Mark Symonds at Nov 27, 2001 at 5:49 am
    ----- Original Message -----
    From: "James Kelty" <jamesk@ashlandagency.com>
    To: <beginners@perl.org>
    Sent: Monday, November 26, 2001 2:17 PM
    Subject: File System Sizes....

    Hello!

    I am new to the list, so forgive me if this is not the correct forum. I am
    writing (attempting to anyway) a script that will look at the size of the
    filesystem, and warn me if it is getting too full, say 90% percent or so.
    [...]

    Perhaps this is one of those rare times when a plain
    old shell script would better suit your purposes?

    df |awk '{print $5}' |tail +2 |tr -d %

    .... you get the list from there, and test each partition
    in order.

    Perhaps there are still certain problems that lend
    their solutions to a simple shell script.

    Perl appears to be better for the more complex stuff
    (which seems to happen every single day here!)

    --
    Mark
  • Kipp, James at Nov 27, 2001 at 1:48 pm

    $percent = `df -k | awk '{print $5}'`;
    if($percent > "90") {

    do something
    }

    I though that this would work, but I didn't get just the %
    column from the
    awk statment. I am really trying
    to do this all in perl instead of mixing it with awk.
    in that case all you need to do is capture the df -k output then regex it.

    $percent = `df -k`;
    $percent =~/regex_to_capture_number/;

    --
    don't have time to look at the output right now and regex but should be
    easy.

    >
  • Robert Graham at Nov 27, 2001 at 2:06 pm
    You can try the following

    ($percent) = `df -k` =~ /(\d+)%/;

    Regards
    Robert Graham

    -----Original Message-----
    From: Kipp, James
    Sent: 27 November 2001 15:47
    To: 'James Kelty'; beginners@perl.org
    Subject: RE: File System Sizes....

    $percent = `df -k | awk '{print $5}'`;
    if($percent > "90") {

    do something
    }

    I though that this would work, but I didn't get just the %
    column from the
    awk statment. I am really trying
    to do this all in perl instead of mixing it with awk.
    in that case all you need to do is capture the df -k output then regex it.

    $percent = `df -k`;
    $percent =~/regex_to_capture_number/;

    --
    don't have time to look at the output right now and regex but should be
    easy.

    >


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Kipp, James at Nov 27, 2001 at 3:22 pm

    You can try the following

    ($percent) = `df -k` =~ /(\d+)%/;

    Regards
    Robert Graham

    There you go James !!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 26, '01 at 10:17p
activeNov 27, '01 at 3:22p
posts7
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase