0

I have a Perl script that executes another Perl script inside it:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case bundling_override);
no warnings 'uninitialized';
#print '<<<check_jenkins_jobs>>>';
my ($port, $host) ;
# Parse command line arguments
GetOptions ('host=s'   => \$host, 'port=s' => \$port);
if( (!defined($host)) || (!defined($port))) {
        print "USAGE:perl $0 -host mbiradar2d -port 8080";
    exit 1;
}
use constant {
    OK => 0,
    WARNING => 1,
    CRITICAL => 2,
    UNKNOWN => 3,
};
my @output = `perl "H:\\Mangesh\\check_jenkins_jobs.pl" -host $host -port $port`;
my $line;
my $status;
my $statustxt;
foreach $line (@output) {
    #print $line;
    my @values = split('~', $line);
    if($values[0] =~ /^CRITICAL/) {
        $status = 2;
        $statustxt = 'CRITICAL';
        print "$values[0] : $values[1] has health score $values[2]";
        exit CRITICAL;
    }
    elsif($values[0] =~ /^WARNING/) {
        $status = 1;
        $statustxt = 'WARNING';
        print "$values[0] : $values[1] has health score $values[2]";
        exit WARNING;
    }
    else {
        $status = 0;
        $statustxt = 'OK';
        print "$values[0] : $values[1] has health score $values[2]";
        exit OK;
    }
}

Here is the output of inner Perl script which I am parsing in above script for further use:

CRITICAL ~ First_run ~ Build stability: 3 out of the last 4 builds failed. ~ 25
CRITICAL ~ Mangesh_Testing ~ Build stability: All recent builds failed. ~ 0
CRITICAL ~ MKS_Integrity ~ Build stability: All recent builds failed. ~ 0
OK ~ MKS_TEST ~ Build stability: No recent builds failed. ~ 100
CRITICAL ~ Rest_api_testing ~  ~ no score
CRITICAL ~ Second_job ~  ~ no score
OK ~ Team_test ~ Build stability: No recent builds failed. ~ 100
OK ~ test ~ Build stability: No recent builds failed. ~ 100
CRITICAL ~ TEST_1 ~ Build stability: 2 out of the last 3 builds failed. ~ 33
OK ~ Update_Outlook ~ Build stability: No recent builds failed. ~ 100

But the problem is this script is exiting after printing the first line of inner Perl scripts output. I want to exit this script with each output line printed. What are other ways that I can achieve this? That would be better if this script exits according to the Nagios compatible output.For eg.0 for OK, 1 for WARNING, 2 for CRITICAL and 3 for UNKNOWN

Keith
  • 4,637
  • 15
  • 25
MangeshBiradar
  • 1,820
  • 4
  • 16
  • 17

1 Answers1

4

You are telling the script to exit after the first line yourself, which each exit statement in the if blocks.

If you want the logic of your script to stay the same but you want to print everything, let it print the output in a separate loop before processing and exiting.

foreach $line (@output)  {
   print $line;
}
Sven
  • 98,649
  • 14
  • 180
  • 226
  • Thanks SvenW. I tried that and the script is printing(on console) what I expecting. But I want this output(each line) to be printed on Nagios UI, how could I get over it? – MangeshBiradar Jan 18 '13 at 06:49
  • 1
    @Maverick [This is what you're after.](http://nagios.sourceforge.net/docs/3_0/pluginapi.html) Please mark SvenW's answer as correct, you are changing the scope of the question from being about Perl to being about Nagios monitors. – Andrew B Jan 18 '13 at 08:51
  • @AndrewB: Thanks, I just was going to look that one up ... – Sven Jan 18 '13 at 09:11