Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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\\" -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

share|improve this question
up vote 4 down vote accepted

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;
share|improve this answer
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? – meMangesh Jan 18 '13 at 6:49
@Maverick This is what you're after. 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 8:51
Yep, Thanks SvenW, Andrew B. – meMangesh Jan 18 '13 at 9:02
@AndrewB: Thanks, I just was going to look that one up ... – Sven Jan 18 '13 at 9:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.