CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl Programming help required

 



gravindra1977
Novice

Jan 31, 2008, 1:26 PM

Post #1 of 11 (706 views)
Perl Programming help required Can't Post

I have written the following code to obtain the diskspace of unix filesystem and all the /uXX filesystems installed on the server. I am gettting an error while executing the same.

Code is as follows:

#!/usr/bin/perl
use strict;
use warnings;
my %env_to_fs =
(DEV => 'FSDEV',
QA => 'FSQA',
PROD => 'FSPRD', );
#Get argument,
set $fsmy $fs_arg = shift;
die "Usage: $0 <Environment>\n" unless $fs_arg;
die "Invalid environment, it must be one of: " . join(", ", keys %env_to_fs) . "\n" unless exists($env_to_fs{$fs_arg});my $fs = $env_to_fs{$fs_arg};

my $exitcode=0;

open DF, "df|" or die "Can not run df $!\n";<DF>;
#Get header line#Get all other lines

while (<DF>) {
my ($dev,$total,$used,$free,$percent,$fs) = split /\s+/;
if($dev =~ m!($fs|/u\d+)!) {
print "The filesystem is : $dev\n";
print " Total: $total\n";
print " Used : $used\n";
print " Free : $free\n";
if($free/$total < .1) {
$exitcode=1;
}
}
}
close(DF);
exit($exitcode);

The output of this program is coming as

The filesystem is : /u01
Total: (/dev/vx/dsk/oradg/u01):21596644
Used : blocks
Free : 3254247 Argument "(/dev/vx/dsk/oradg/u01):21596644" isn't numeric in division.Illegal division by zero at df.pl line 33, <DF> line 10.

I typed df in terminal server and I get the following output. I also uploaded the output that I got when I type in df in the terminal server as an attachment.

/ (/dev/vx/dsk/bootdg/rootvol): 3533744 blocks 417512 files
/proc (/proc ): 0 blocks 29421 files
/etc/mnttab (mnttab ): 0 blocks 0 files
/dev/fd (fd ): 0 blocks 0 files
/var (/dev/vx/dsk/bootdg/var): 3843060 blocks 489235 files
/var/run (swap ):39591600 blocks 3219307 files
/dev/vx/dmp (dmpfs ):39591600 blocks 3219307 files
/dev/vx/rdmp (dmpfs ):39591600 blocks 3219307 files
/tmp (swap ): 960928 blocks 3219307 files
/redo002 (/dev/vx/dsk/orardodg02/redo002):104745016 blocks 7188431 fs
/u01 (/dev/vx/dsk/oradg/u01):21100702 blocks 3254641 files
/stage (/dev/vx/dsk/oradg/stage):24403074 blocks 1793502 files
/redo001 (/dev/vx/dsk/orardodg01/redo001):104510852 blocks 7188416 fs
/u008 (/dev/vx/dsk/oradatdg02/u008):177416744 blocks 14376930 files
/arch001 (/dev/vx/dsk/oraarcdg01/arch001):224134440 blocks 28746140 fs
/uhcdba (/dev/vx/dsk/oradg/uhcdba):57641054 blocks 7181513 files
/u006 (/dev/vx/dsk/oradatdg01/u006):168425668 blocks 14376890 files
/u007 (/dev/vx/dsk/oradatdg02/u007):16914724 blocks 14376720 files
/u003 (/dev/vx/dsk/oradatdg01/u003):91315850 blocks 14376878 files
/u005 (/dev/vx/dsk/oradatdg01/u005):182653128 blocks 14376909 files
/u004 (/dev/vx/dsk/oradatdg01/u004):115505694 blocks 14376872 files
/u009 (/dev/vx/dsk/oradatdg03/u009):53605372 blocks 14376936 files
/u010 (/dev/vx/dsk/oradatdg03/u010):87544968 blocks 14376923 files
/FSDEV (nasgw010pn:/vol/eagan15/fsdev):53315504 blocks 31712083 fis
/u011 (/dev/vx/dsk/orau011dg/u011):40284380 blocks 14376919 files
/u012 (/dev/vx/dsk/orau012dg/u012):12841628 blocks 14376924 files
/u013 (/dev/vx/dsk/orau013dg/u013):189903986 blocks 14376917 files
/u014 (/dev/vx/dsk/orau014dg/u014):202216442 blocks 14376934 files
/u015 (/dev/vx/dsk/orau015dg/u015):202640584 blocks 14376934 files
/INTERFACES_SIMS/SIMSDEV(nasgw010pn:/vol/eagan17/simsdev): 2097152 blocks 31320s
/uhtrels (/dev/vx/dsk/rootdg/uhtrels): 4306766 blocks 362072 files
/prdctl (/dev/vx/dsk/rootdg/prdctl): 1151956 blocks 291716 files
/home (/dev/vx/dsk/bootdg/home): 1231912 blocks 517025 files


I believe split is not working properly. How to correct this? Is there any other easy way of getting the total, allocated, free, capacity etc attributes of /FSDEV and /uXX filesystems. If so , how. Could you please let me know where I am going wrong and correct this accordingly.

Thanks
Attachments: df.txt (2.53 KB)


KevinR
Veteran


Jan 31, 2008, 1:40 PM

Post #2 of 11 (703 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

is that the actual code? It looks like it should not even compile. This line is bad:


Code
set $fsmy $fs_arg = shift;



there is no "set" function in perl that I am aware of and the scalars have not been declared with "my".

What does a raw line from the "df|" look like before you split it?
-------------------------------------------------


gravindra1977
Novice

Jan 31, 2008, 1:47 PM

Post #3 of 11 (701 views)
Re: [KevinR] Perl Programming help required [In reply to] Can't Post

Sorry, Typo.. copy paste error it should be as below. "#Get argument, set $fs" is a comment.

#!/usr/bin/perl
use strict;
use warnings;
my %env_to_fs =
(DEV => 'FSDEV',
QA => 'FSQA',
PROD => 'FSPRD', );


#Get argument, set $fs



my $fs_arg = shift;


KevinR
Veteran


Jan 31, 2008, 2:22 PM

Post #4 of 11 (696 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

OK, how about my other request:

What does a raw line from the "df|" look like before you split it?
-------------------------------------------------


gravindra1977
Novice

Jan 31, 2008, 2:28 PM

Post #5 of 11 (695 views)
Re: [KevinR] Perl Programming help required [In reply to] Can't Post

How can I get the raw line before splitting? I am relative new to perl and hence it might be a silly question, but can you tell me how to get the raw line.


KevinR
Veteran


Jan 31, 2008, 3:04 PM

Post #6 of 11 (688 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

while (<DF>) {
print; #<--here it will print the raw output
my ($dev,$total,$used,$free,$percent,$fs) = split /\s+/;
-------------------------------------------------


gravindra1977
Novice

Jan 31, 2008, 3:36 PM

Post #7 of 11 (687 views)
Re: [KevinR] Perl Programming help required [In reply to] Can't Post

Below is the output. Also attached the txt file.

/devices (/devices ): 0 blocks 0 files
/system/contract (ctfs ): 0 blocks 2147483601 files
/proc (proc ): 0 blocks 29712 files
/etc/mnttab (mnttab ): 0 blocks 0 files
/etc/svc/volatile (swap ):112179664 blocks 4779653 files
/system/object (objfs ): 0 blocks 2147483439 files
/platform/sun4u-us3/lib/libc_psr.so.1(/platform/sun4u-us3/lib/libc_psr/libc_psr_hwcap1.so.1): 6846164 blocks 564462 files
The filesystem is : /platform/sun4u-us3/lib/libc_psr.so.1(/platform/sun4u-us3/lib/libc_psr/libc_psr_hwcap1.so.1):
Total: 6846164
Used : blocks
Free : 564462
/platform/sun4u-us3/lib/sparcv9/libc_psr.so.1(/platform/sun4u-us3/lib/sparcv9/libc_psr/libc_psr_hwcap1.so.1): 6846164 blocks 564462 files
The filesystem is : /platform/sun4u-us3/lib/sparcv9/libc_psr.so.1(/platform/sun4u-us3/lib/sparcv9/libc_psr/libc_psr_hwcap1.so.1):
Total: 6846164
Used : blocks
Free : 564462
/dev/fd (fd ): 0 blocks 0 files
/var (/dev/vx/dsk/bootdg/var): 8456890 blocks 616559 files
/tmp (swap ): 8317840 blocks 4779653 files
/var/run (swap ):112179664 blocks 4779653 files
/dev/vx/dmp (swap ):112179664 blocks 4779653 files
/dev/vx/rdmp (swap ):112179664 blocks 4779653 files
/opt (/dev/vx/dsk/bootdg/opt):27395896 blocks 2064415 files
The filesystem is : /opt
Total: (/dev/vx/dsk/bootdg/opt):27395896
Used : blocks
Free : 2064415
Argument "(/dev/vx/dsk/bootdg/opt):27395896" isn't numeric in division (/) at df.pl line 34, <DF> line 16.
Illegal division by zero at df.pl line 34, <DF> line 16.
Attachments: rawdata.txt (1.64 KB)


KevinR
Veteran


Jan 31, 2008, 4:12 PM

Post #8 of 11 (678 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

OK, well it's clearly obvious you can't split those lines on the spaces and expect the data fields you want:


Code
my ($dev,$total,$used,$free,$percent,$fs) = split /\s+/;


Lets take a look at a couple of the raw lines:


Code
/devices (/devices ): 0 blocks 0 files 
/system/contract (ctfs ): 0 blocks 2147483601 files


If you split them on the spaces you get this (I use '--' to show the where each field begins and ends):


Code
/devices--(/devices--):--0--blocks--0--files 
/system/contract--(ctfs--):--0--blocks--2147483601--files


You can see there are 7 fields in most of the lines. Now lets line them up with your list of scalars using the first line above:


Code
$dev = /devices 
$total = (/devices
$used = ):
$free = 0
$percent = blocks
$fs = 0
??? = files (this is not assigned to anything)


That is obviously no good. But there is another problem. This line:


Code
/opt (/dev/vx/dsk/bootdg/opt):27395896 blocks 2064415 files


has no space after "opt):" and before "27395896" so it is not getting split correctly and that is causing the error you see:


Quote
Argument "(/dev/vx/dsk/bootdg/opt):27395896" isn't numeric in division (/) at df.pl line 34, <DF> line 16.
Illegal division by zero at df.pl line 34, <DF> line 16.


You are going to have to use a regular expression (or maybe more than one) to get the data out of the lines instead of using split().

But there might also be a module that already does all the dirty work for you. A quick search of CPAN for "df" finds a few, like this one:

http://search.cpan.org/~iguthrie/Filesys-Df-0.92/Df.pm

of course it will more than likely need to be installed for you to use it, which opens a new can of worms. Is this a class/school/course assignment or why are you trying to write this script?
-------------------------------------------------


gravindra1977
Novice

Jan 31, 2008, 4:23 PM

Post #9 of 11 (677 views)
Re: [KevinR] Perl Programming help required [In reply to] Can't Post

I am writing this as an utility for our organization.

Basically the idea is to have a perl script which will monitor diskspace of unix filesystems.

The following is the logic that I was thinking about:

Logic:

Check the environment passed as an argument
If environment passed is DEV
set unix-file-system to FSDEV
else if environment passed is QA
then unix-file-system to FSQA
else if environment passed is PROD
then unix-file-system is FSPRD
end if

Set threshold to 10%

Get the total space, total used and total unused disk space on the unix-file-system set above.
Calculate the % of unused space.
If it falls below threshold then
send a mail /exit with non-zero
end if

Loop thru all the filesystems
Check the unused space for all the filesystems which are of uXX i.e., /u01, /u02, /u03 [ Basically trying to get space of the filesystems where oracle is installed]
Log the unused space of these filesystems in a log file
If any of these falls below threshold,
send a mail / exit with non-zero
end if


Is there any better way of doing this. Basically need the allocated,free, used disk space of the unix filesystem, FSDEV as mentioned above and all the /uXX filesystems installed on the unix server.

Please post the code if there is a better way of doing this thing.


KevinR
Veteran


Jan 31, 2008, 6:03 PM

Post #10 of 11 (663 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

I think your approach will work OK. You just need to figure out how to parse the lines so you get the appropriate fields plugged into your calculations. You can do all that just using the Unix df function too:

df / FSDEV (displays for that filesystem only)

I'm going to recommend you ask on www.unix.com in the Shell Programming and Scripting forum. There are many knowledgeable shell scripting people that can recommend all sorts of ways to go about this besides perl. You probably have awk and sh and ksh available to use also. If you're really stuck on using perl let me know though and I will help you parse the lines of the output from the unix df function.
-------------------------------------------------


KevinR
Veteran


Jan 31, 2008, 10:29 PM

Post #11 of 11 (655 views)
Re: [gravindra1977] Perl Programming help required [In reply to] Can't Post

Here is a nearly complete script, I can't test it. it needs the email and logging routines added.

*** code removed. If you want to see the code I have written so far, post back in this thread ***

I went by the output from the df command you PM'ed me.
-------------------------------------------------


(This post was edited by KevinR on Feb 2, 2008, 12:40 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives