我有一个Perl脚本,在其中执行另一个Perl脚本:
#!/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; } }
下面是我在上面的脚本中parsing的内部Perl脚本的输出以供进一步使用:
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
但问题是这个脚本在打印内部Perl脚本输出的第一行之后正在退出。 我想要打印每个输出行来退出这个脚本。 我能做到的其他方法是什么? 如果此脚本根据Nagios兼容输出退出,则会更好。对于例如OK.0,WARNING为1,CRITICAL为2,UNKNOWN为3
你正在告诉脚本在你自己的第一行之后exit ,其中每个exit语句在if块中。
如果您希望脚本的逻辑保持不变,但您想要打印所有内容,请在处理和退出之前在单独的循环中打印输出。
foreach $line (@output) { print $line; }