[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Perl RegEx correction needed

Posted on 2012-09-02
7
Medium Priority
?
415 Views
Last Modified: 2012-09-04
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
0
Comment
Question by:ashsysad
7 Comments
 
LVL 23

Expert Comment

by:savone
ID: 38359748
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
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 500 total points
ID: 38359788
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
 
LVL 12

Expert Comment

by:tel2
ID: 38359814
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Accepted Solution

by:
namethis earned 1000 total points
ID: 38360289
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
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 500 total points
ID: 38361981
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
 

Author Closing Comment

by:ashsysad
ID: 38362318
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
 
LVL 12

Expert Comment

by:tel2
ID: 38365675
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question