To parse the command output..

C

clearguy02

Hi folks,

I am looking out for some help here. I am working on an email alert
mechanism.

I need to parse the output of "df- k" command and if the capacity is
more than 50%, the output (pritn statement) of the perl script should
print out the whole line that contains the number, 50%.

Example: diskSpaceCheck.pl

Output of df -k on the unix server:
+++++++++++++++++++++
$ df -k
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/rootvol 15493995 981301 14357755 7% /
/proc 0 0 0 0%
/proc
fd 0 0 0 0%
/dev/fd
mnttab 0 0 0 0%
/etc/mnttab
/dev/vx/dsk/proddg/PRODdata01 15728640 8209296 7460600 53%
/oracle/PROD/data01
+++++++++++++++++++++++++++++++++++

Now the output should print out below two lines:
/oracle/PROD/data01 currently has 53%.
The whole line (/dev/vx/dsk/proddg/PRODdata01 15728640
8209296 7460600 53% /oracle/PROD/data01).

Thanks in advance,
CG
 
D

DJ Stunks

Hi folks,

I am looking out for some help here. I am working on an email alert
mechanism.

I need to parse the output of "df- k" command and if the capacity is
more than 50%, the output (pritn statement) of the perl script should
print out the whole line that contains the number, 50%.

Example: diskSpaceCheck.pl

Output of df -k on the unix server:
+++++++++++++++++++++
$ df -k
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/rootvol 15493995 981301 14357755 7% /
/proc 0 0 0 0%
/proc
fd 0 0 0 0%
/dev/fd
mnttab 0 0 0 0%
/etc/mnttab
/dev/vx/dsk/proddg/PRODdata01 15728640 8209296 7460600 53%
/oracle/PROD/data01
+++++++++++++++++++++++++++++++++++

Now the output should print out below two lines:
/oracle/PROD/data01 currently has 53%.
The whole line (/dev/vx/dsk/proddg/PRODdata01 15728640
8209296 7460600 53% /oracle/PROD/data01).

how did you attempt to solve this? in what way is your solution
deficient? have you read the posting guidelines for this group?

-jp
 
P

Paul Lalli

I am looking out for some help here. I am working on an email alert
mechanism.

I need to parse the output of "df- k" command and if the capacity is
more than 50%, the output (pritn statement) of the perl script should
print out the whole line that contains the number, 50%.

Example: diskSpaceCheck.pl

Output of df -k on the unix server:
+++++++++++++++++++++
$ df -k
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/rootvol 15493995 981301 14357755 7% /
/proc 0 0 0 0%
/proc
fd 0 0 0 0%
/dev/fd
mnttab 0 0 0 0%
/etc/mnttab
/dev/vx/dsk/proddg/PRODdata01 15728640 8209296 7460600 53%
/oracle/PROD/data01
+++++++++++++++++++++++++++++++++++

Now the output should print out below two lines:
/oracle/PROD/data01 currently has 53%.
The whole line (/dev/vx/dsk/proddg/PRODdata01 15728640
8209296 7460600 53% /oracle/PROD/data01).

100% untested:

#!/usr/bin/perl
use strict;
use warnings;
for my $line (`df -k`){
if ($line =~ /(\d+)%/ and $1 >= 50){
print $line;
}
}
__END__

Hope this helps,
Paul Lalli
 
J

Joe Smith

Paul said:
for my $line (`df -k`){ ... }

But a single entry can take more than one line.
This happens a lot with the Logical Volume Manager and with long NFS paths.
I use this instead:

$_ = `df -h @ARGV`;
# Look for lines with a single field (no spaces), combine it with next line
s/^(\S+)\n/$1 /mg; # Put /dev/mapper/VolGroup with the rest
foreach my $line (split /\n/,$_) { ... }

-Joe
 
D

DJ Stunks

Joe said:
But a single entry can take more than one line.
This happens a lot with the Logical Volume Manager and with long NFS paths.

just because it word wraps on your console doesn't mean it's using more
than one line.

-jp
 
J

Joe Smith

DJ said:
just because it word wraps on your console doesn't mean it's using more
than one line.

Have you verified that? I have and it is.

On RHEL-ES4, `df -k` puts "/dev/mapper/VolGroup_ID_814-LogVol1" on a
line by itself with "1K-blocks Used Available Use% Mounted on"
on the next line. This is true for cron jobs and for xterm windows
that are 203 columns wide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top