in reply to Re: Exit values
in thread Exit values

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;
}

Replies are listed 'Best First'.
Re^3: Exit values
by ikegami (Patriarch) on Nov 28, 2008 at 07:03 UTC

    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.

Re^3: Exit values
by ysth (Canon) on Nov 28, 2008 at 05:44 UTC
    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.