farhan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Writing Nagios plugin to check the status of Solaris 10 zones and having exit values issue, must return exit 2 if any zone is not running, in a loop when I call "exit 2 " program exits but doesn't display all the items in loop, how can I fix this, any help would be really appreciated


#!/usr/bin/perl
@zone = qx {zoneadm list -ip};
foreach (@zone) {
($id,$name,$state) = split(/:/,$_);
if($state ne "running"){
print "$name\n";
exit 2;\\here it prints only 1 item and exit how I can print all the items and exit with value 2 }
}


root@dory# zoneadm list -ip
0:global:running:/::native:shared
-:drvldap-dev:installed:/export/zones/drvldap-dev:44a6b308-7cd5-669f-93f6-90447fcd22ca:native:shared
-:drvsps-test:installed:/export/zones/drvsps-test:f786c808-f9b8-6bae-970d-b9fb9969ec80:native:shared


want to display following lines with exit value 2, but when I call exit 2 , it display 1 line and then exit which is the expected behavior. How can I fix this


root@dory# perl check_zones.pl
drvldap-dev
drvsps-test

Replies are listed 'Best First'.
Re: Exit values
by ysth (Canon) on Nov 28, 2008 at 05:19 UTC
      Thanks ysth
      your advise did the trick, now my code looks like

      #!/usr/bin/perl
      use strict;
      my $status;
      my @zones = qx {zoneadm list -ip};
      foreach (@zones) {
      my($id,$name,$state) = split(/:/,$_);
      if($state eq "running") {
      print "OK: zone $name is in running state";
      $status=0;
      }
      if($state ne "running"){
      print "CRITICAL: Zone $name is in $state "; ,br> $status = 2;
      }
      }
      if($status == 2) {
      exit 2;
      }
      if($status == 0) {
      exit 0;
      }

        By the way, putting your code in <c>...</c> tags will simplify things for both you and us.

        Applying ysth's advice, we get:

        #!/usr/bin/perl use strict; use warnings; my $non_running = 0; my @zones = qx{zoneadm list -ip}; foreach (@zones) { my ($id, $name, $state) = split(/:/, $_); if($state eq "running") { print "OK: zone $name is in running state\n"; } else { print "CRITICAL: Zone $name is in $state\n"; $non_running = 1; } } if ($non_running) { exit 2; } else { exit 0; }

        Hopefully, you've learned about else in the process.

        That's not going to work. If you get a non-running zone, it will set it to 2, but if there's a running zone after that, it will be set back to 0. Move the $status=0 to before the loop and it should work. That way, $status will be 0 if all the zones are running, but if any of them are not it will be 2.