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: Beginner:
Filtering Perl-Output

 



Wuz
New User

Sep 6, 2013, 12:33 AM

Post #1 of 4 (738 views)
Filtering Perl-Output Can't Post

Hi 2 all,

I'm a very beginner in programming perl. I've installed nagios on my Server and now I want to create a selfmade plugin in perl.
My intention is to get the free RAM space of my linux server. I know that such a plugin is programmed before, but I want this for learning.
I know, I'll get the free space with "free -m". So i can put this into a variable with

Code
my $free_ram = `free -m`

But now I have to "filter" the output, so that in $free_ram is only the available RAM, which can be used.
So my question: How can I filter the output of "free -m"?


Laurent_R
Veteran / Moderator

Sep 6, 2013, 9:57 AM

Post #2 of 4 (731 views)
Re: [Wuz] Filtering Perl-Output [In reply to] Can't Post

It should be pretty easy. Please provide a sample of the raw output of your command (not all implementations deliver exactly the same output) and of what you want the filtered output to look like.


Wuz
New User

Sep 9, 2013, 4:30 AM

Post #3 of 4 (702 views)
Re: [Wuz] Filtering Perl-Output [In reply to] Can't Post

Hi, thanks for your answer.
I have fixed my problem by taking the uptime and not the free RAM. But now I have a new problem.
Now I have a selfmade plugin for nagios and I want to give this plugin some parameters. But how can I do that? I can't use the Nagios plugin for perl because of some demand.
My code is:


Code
#!/usr/bin/perl -w 
use strict;
use warnings;

my $test = `uptime`;
my @gesplittet = split(/ /,$test);
my $counter = 0;
my $ausgabe = 0;

foreach(@gesplittet)
{
if($counter == 3)
{
$ausgabe = $_;
}
$counter = $counter + 1;
}

if($ausgabe < 300)
{
print "OK - Uptime: $ausgabe Tage";
exit(0);
}
elsif($ausgabe == 300)
{
print "WARNING - Uptime: $ausgabe Tage";
exit(1);
}
elsif($ausgabe > 300)
{
print "CRITICAL - Uptime: $ausgabe Tage";
exit(2);
}
else
{
print "UNKNOWN - Uptime: $ausgabe Tage";
exit(3);
}


Now if I call the plugin, I want to say, that the threshold is at 200 oder 300, so dynamic. But for this I have to give the parameters to the perl script and I don't know how.

Could anybody help?


FishMonger
Veteran / Moderator

Sep 9, 2013, 7:30 AM

Post #4 of 4 (697 views)
Re: [Wuz] Filtering Perl-Output [In reply to] Can't Post

When you pass parameters to a perl script, the are placed in the @ARGV array.

Example:

uptime.pl 300


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $number = $ARGV[0];

....
....

if($ausgabe < $number)
{
print "OK - Uptime: $ausgabe Tage";
exit(0);
}


Why are you doing this:

Code
foreach(@gesplittet)  
{
if($counter == 3)
{
$ausgabe = $_;
}
$counter = $counter + 1;
}


That should be written as

Code
my $ausgabe = $gesplittet[3];


 
 


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

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