Quantcast
Channel: Admins Goodies » jenkins
Viewing all articles
Browse latest Browse all 10

Perl script which returns multiple exit codes

0
0

Question

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

Asked by Maverick143

Answer

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;
}
Answered by SvenW

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images