Perl RegEx correction needed

Hello,

I'm trying to develop a Perl script on Linux platform which upon execution, will print all the file-systems that are using the disk space more than 85%.  This script basically takes the 'df -h' output as input and checks the Use% column.  Could you please correct my RegEx which I used in this ?   I guess rest of the script is okay.
Please let me know if you have any questions. Thanks.

#!/usr/bin/perl

open INFILE, "df -h |" or die $!;
while( <INFILE> ){
     if( $_ =~ m/^\/dev\/\S+?\s+(\d+(M|G|T))\s+(\d+(M|G|T))\s+(\d+(M|G|T))\s+(\d+)%\s+(\S+)$/ ) {
                ($size,$used,$avail,$free,$mntpoint)=($1,$2,$3,$4,$5);
                if ( $free > 85 ) {
                        push(@myarray, "$mntpoint");
                }
        }
}
$result = join("\n",@myarray);
print "List of file-systems having Disk space alerts : $result\n";

Open in new window


A Sample 'df -h' output:
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgroot-lvroot
                      4.0G  3.4G  466M  88% /
/dev/mapper/vgroot-lvhome
                      124M   57M   62M  49% /home
/dev/sda1              99M   32M   63M  34% /boot
tmpfs                  16G     0   16G   0% /dev/shm
/dev/mapper/vgapps-lvoracle
                       20.5G  8.8G   11G  45% /oracle
/dev/mapper/vgapps-lvoradata
                      2.4T  2.0T  206G  91% /oracle/oradata
/dev/mapper/vgapps-lvoraarch
                      301G  125G  161G  44% /oracle/archivelogs
sc-nas:/ifs/PROD/Oracle/oracle_expimp_prd_d1_d2
                      500G  424G  77G  86% /oracle/expimp
sc-nas:/ifs/PROD/Oracle/oracle_expimp2
                      500G   84G  417G  17% /oracle_expimp2

Open in new window


Expected Ouput
List of file-systems having Disk space alerts :  /
                                                                          /oracle/oradata
                                                                          /oracle/expimp
ashsysadAsked:
Who is Participating?
 
namethisCommented:
Your script doesn't work because:
\d won't match dots in size numbers
you have 8 captures, not 5, so the mountpoint becomes $8
your df output is split, so you need to match against the whole output

Here's how it can be fixed:

#!/usr/bin/perl

open INFILE, "df -h |" or die $!;
$/=undef;
$_=<INFILE>;
while( m#^/dev/\S+?\s+([.\d]+[MGT]?)\s+([.\d]+[MGT]?)\s+([.\d]+[MGT]?)\s+(\d+)\%\s+(\S+)$#mg ) {
                ($size,$used,$avail,$free,$mntpoint)=($1,$2,$3,$4,$5);
                if ( $free > 85 ) {
                        push(@myarray, "$mntpoint");
                }
}
$result = join("\n",@myarray);
print "List of file-systems having Disk space alerts : $result\n";

Open in new window

0
 
savoneCommented:
There is already a script written for this:
http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html

At least you can get started!
0
 
farzanjCommented:
Try:
m{^/dev/\S+?\s+(\d+(?:\.\d+)?)[MGT])\s+(\d+(?:\.\d+)?)[MGT])\s+(\d+(?:\.\d+)?)[MGT])\s+(\d+(?:\.\d+)?)%\s+(\S+)$} 

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
tel2Commented:
Hi ashsysad,

I won't try to correct your Regex now, but here are some comments on your script:

1. The "Use%" value seems to go into $4, and then into a variable called $free.  Since free space and used space are oposites, this could be a bit confusing to someone reading your script, which says "if ( $free > 85 )".  Since you want to "print all the file-systems that are using the disk space more than 85%", I suggest you change $free to something like $usepc.

2. Due to the length of the filesystem names, the logical lines of the "df -u" output are usually split into 2 physical lines in your situation.  So, you can't just read them 1 by 1 and treat them as you are.  If you don't know how to resolve that, feel free to ask and someone may help you.
0
 
tel2Commented:
The Regex that namethis has provided won't report /oracle/expimp even though it is 86% full.  That's because not all filesystems start with /dev.  Here's one way to fix that.  Replace namethis's "while..." line with this:

while( m#^\S+?\s+([.\d]+[MGT]?)\s+([.\d]+[MGT]?)\s+([.\d]+[MGT]?)\s+(\d+)%\s+(\S+)$#mg ) {

Note that I have also not escaped the "%" as I don't see why that is necessary.

Other than that, nice work, namethis!
0
 
ashsysadAuthor Commented:
Thankyou all for your help !  I just tested and works like a charm. There are lot of stuff to learn from this one-line RegEx that you provided.

Thanks again !!
0
 
tel2Commented:
Thanks for the points, ashsysad,

I think the "?" at the end of the:
    m#^\S+?
part is also not necessary.  Seems to work fine without it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.