execute and analyze unix command with perl

execute and analyze unix command with perl

am 27.05.2010 18:27:46 von Robert Morales

--0016e6d9a027e74ea4048795de80
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I want my code to execute the unix free command in order to analyze
the memory state, and issue a warning if the cached memory increases.
I don`t know what I did wrong, but this is what I got for now:

#! /usr/bin/perl
use warnings;
use strict;

# return codes
$ok = 0;
$warning = 1;
$critical = 2;

open(my $memory, "free") or die "Error: $!"; # running unix command "free"

$regex = "^((?!total).)*$";

while (my $line = $vgs){

if ($line =~ m/$regex/){
chomp $line;
my @array = split /\s+/, $line;
if ($array[6] >= "867580"){
print "$warning\n";
}elsif ($array[6] >= "689967"){
print "$critical\n";
}else{
print "$ok\n";
}
}
}

--0016e6d9a027e74ea4048795de80--

Re: execute and analyze unix command with perl

am 27.05.2010 18:36:45 von Shlomi Fish

On Thursday 27 May 2010 19:27:46 Robert Morales wrote:
> Hi,
>
> I want my code to execute the unix free command in order to analyze
> the memory state, and issue a warning if the cached memory increases.
> I don`t know what I did wrong, but this is what I got for now:
>
> #! /usr/bin/perl
> use warnings;
> use strict;
>
> # return codes
> $ok = 0;
> $warning = 1;
> $critical = 2;
>
> open(my $memory, "free") or die "Error: $!"; # running unix command "free"
>

This command opens the file "free" for reading, not the output of the UNIX
command "free". Use three args open and do:

open (my $memory, "-|", "free") or ....

Regards,

Shlomi Fish

> $regex = "^((?!total).)*$";
>
> while (my $line = $vgs){

What does that line do?

>
> if ($line =~ m/$regex/){
> chomp $line;
> my @array = split /\s+/, $line;
> if ($array[6] >= "867580"){
> print "$warning\n";
> }elsif ($array[6] >= "689967"){
> print "$critical\n";
> }else{
> print "$ok\n";
> }
> }
> }

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: execute and analyze unix command with perl

am 27.05.2010 18:37:22 von Jim Gibson

On 5/27/10 Thu May 27, 2010 9:27 AM, "Robert Morales"
scribbled:

> Hi,
>
> I want my code to execute the unix free command in order to analyze
> the memory state, and issue a warning if the cached memory increases.
> I don`t know what I did wrong, but this is what I got for now:
>
> #! /usr/bin/perl
> use warnings;
> use strict;
>
> # return codes
> $ok = 0;
> $warning = 1;
> $critical = 2;
>
> open(my $memory, "free") or die "Error: $!"; # running unix command "free"

The above line will attempt to open a file "free" in the current directory.
If you want to pipe output from a command into your program, the name should
end in '|':
open( my $memory, "free|") ...

or, better, use the 3-argument version of open:

open( my $memory, '-|', 'free' ) ...

See 'perldoc -f open' for details.

>
> $regex = "^((?!total).)*$";
>
> while (my $line = $vgs){

The above should be:

while( my $line = <$memory> ) {

>
> if ($line =~ m/$regex/){
> chomp $line;
> my @array = split /\s+/, $line;
> if ($array[6] >= "867580"){
> print "$warning\n";
> }elsif ($array[6] >= "689967"){
> print "$critical\n";
> }else{
> print "$ok\n";
> }
> }
> }



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: execute and analyze unix command with perl

am 27.05.2010 18:48:52 von Robert Morales

--0016e657b1da5ba2a90487962a9d
Content-Type: text/plain; charset=ISO-8859-1

Tnx Guys!

the $vgs variable is of course and error and should be $memory.
After adding your suggestions, I ran the script over again and got this
error:

root@user# ./script.pl
Final $ should be \$ or $name at ./script.pl line 11, within string
syntax error at ./script.pl line 11, near "= "^((?!total).)*$""
Execution of ./script.pl aborted due to compilation errors.

It might be something with the regex, but I can't see exactly what??

Current code:

#! /usr/bin/perl
use warnings;
use strict;

# Nagios return codes
$ok = 0;
$warning = 1;
$critical = 2;
$unknown = 3;

open(my $memory,"-|","free") or die "Error: $!"; # running unix command
"free"

$regex = "^((?!total).)*$";

while (my $line =
<$memory>){


if ($line =~ m/$regex/){
chomp $line;
my @array = split /\s+/, $line;
if ($array[6] >= "867580"){
print "$warning\n";
}elsif ($array[6] >=
"689967"){
print "$critical\n";
}else{
print "$ok\n";
}
}
}

--0016e657b1da5ba2a90487962a9d--

Re: execute and analyze unix command with perl

am 27.05.2010 19:34:21 von Brandon McCaig

On Thu, May 27, 2010 at 12:48 PM, Robert Morales
wrote:
> Final $ should be \$ or $name at ./script.pl line 11, within string
> syntax error at ./script.pl line 11, near "= "^((?!total).)*$""
> Execution of ./script.pl aborted due to compilation errors.
>
> It might be something with the regex, but I can't see exactly what??
** snip **
> $regex = "^((?!total).)*$";

The problem is that Perl is interpreting the $ to mean something
special (i.e., an embedded variable). You can escape it with \$, as
suggested by the error message.

perl -e '
use strict;
use warnings;

#my $regex = "^((?!total).)*$";
my $regex = "^((?!total).)*\$";
'

--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: execute and analyze unix command with perl

am 27.05.2010 20:09:50 von Robert Morales

--00504502c303e993370487974b72
Content-Type: text/plain; charset=ISO-8859-1

Ok, now I added the:

my $regex = "^((?!total).)*\$";

I also removed the " " from the numbers in the if test:

if ($array[6] >= 867580){

The error msg I get this time, is like this:

root@user# ./script.pl
0
Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
21, <$memory> line 3.
Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
23, <$memory> line 3.
0
Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
21, <$memory> line 4.
Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
23, <$memory> line 4.
0

I appreciate the help :)

--00504502c303e993370487974b72--

Re: execute and analyze unix command with perl

am 27.05.2010 20:43:57 von Brandon McCaig

On Thu, May 27, 2010 at 2:09 PM, Robert Morales
wrote:
> The error msg I get this time, is like this:
>
> root@user# ./script.pl
> 0
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 21, <$memory> line 3.
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 23, <$memory> line 3.
> 0
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 21, <$memory> line 4.
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 23, <$memory> line 4.
> 0

It appears that $array[6] is uninitialized (it's equal to undef). It's
warning you because you're using it as a number, which might indicate
an error on your part so it lets you know. You can get rid of them by
wrapping the entire if...elsif...else ladder in another if...

if(defined $array[6])
{
...
}

So that it only prints a status message if that element exists in the array.

Of course, the fact that it isn't defined at all suggests that the
data you're processing isn't what you think it is. You might want to
analyze the arrays you're getting back to make sure they're what you
expect. You can use Data::Dumper to see what the array(s) look like:

use Data::Dumper;

....

print Dumper(\@array);

--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: execute and analyze unix command with perl

am 27.05.2010 21:32:22 von jwkrahn

Robert Morales wrote:
> Ok, now I added the:
>
> my $regex = "^((?!total).)*\$";
>
> I also removed the " " from the numbers in the if test:
>
> if ($array[6] >= 867580){
>
> The error msg I get this time, is like this:
>
> root@user# ./script.pl
> 0
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 21, <$memory> line 3.
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 23, <$memory> line 3.
> 0
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 21, <$memory> line 4.
> Use of uninitialized value $array[6] in numeric ge (>=) at ./script.pl line
> 23, <$memory> line 4.
> 0
>
> I appreciate the help :)

If 'free' on your system is the same as 'free' on my system then there
is only one line with seven fields so the other lines will try to
compare an undefined value with a number and produce that warning
message. This should probably work better:


#!/usr/bin/perl
use warnings;
use strict;

# Nagios return codes
my $ok = 0;
my $warning = 1;
my $critical = 2;
my $unknown = 3;

# running unix command "free"
open my $memory, '-|', 'free' or die "Cannot open pipe from 'free' $!";

while ( <$memory> ) {
my @fields = split;
next unless @fields == 7;

if ( $array[ 6 ] >= 867_580 ) {
print "$warning\n";
}
elsif ( $array[ 6 ] >= 689_967 ) {
print "$critical\n";
}
else {
print "$ok\n";
}
}

close $memory or warn $! ? "Error closing 'free' pipe: $!"
: "Exit status $? from 'free'";


__END__



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: execute and analyze unix command with perl

am 28.05.2010 01:03:07 von derykus

On May 27, 11:09=A0am, robertmorales...@gmail.com (Robert Morales)
wrote:
> Ok, now I added the:
>
> =A0 =A0 =A0 =A0my $regex =3D "^((?!total).)*\$";
>

Using the 'qr' operator is a more efficient option
(see "Regexp Quote-Like Operators " in perlop)
and will take care of the $:

my $regex =3D qr/^((?!total).)*$/;
....
if ( $line =3D~ /$regex/ )
> I also removed the " " from the numbers in the if test:
>
> =A0 =A0 =A0 =A0 if ($array[6] >=3D 867580){
>
> The error msg I get this time, is like this:
>
> root@user# ./script.pl
> 0
> Use of uninitialized value $array[6] in numeric ge (>=3D) at ./script.pl =
line
./script.pl line

Looks like $array[6] is undefined. Are you sure the split
always generates that many fields...? Do you need to
refactor the code ..?


--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/