時間を費やしている関数はC言語で記述されているため、カスタムシグナルハンドラを安全に使用することはできません。
あなたのプログラムを強制終了する心配はありませんので、実行に時間がかかりすぎるとプログラムを強制的に終了させるためにシグナルハンドラなしでalarm
を使用し、Nagiosに正しい応答を提供するためにラッパーを使用することをお勧めします。
変更
/path/to/program some args
/path/to/timeout_wrapper 30 /path/to/program some args
次へtimeout_wrapper
です:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(WNOHANG);
use Time::HiRes qw(sleep time);
sub wait_for_child_to_complete {
my ($pid, $timeout) = @_;
my $wait_until = time + $timeout;
while (time < $wait_until) {
waitpid($pid, WNOHANG)
and return $?;
sleep(0.5);
}
return undef;
}
{
my $timeout = shift(@ARGV);
defined(my $pid = fork())
or exit(3);
if (!$pid) {
alarm($timeout); # Optional. The parent will handle this anyway.
exec(@ARGV)
or exit(3);
}
my $timed_out = 0;
my $rv = wait_for_child_to_complete($pid, $timeout);
if (!defined($rv)) {
$timed_out = 1;
if (kill(ALRM => $pid)) {
$rv = wait_for_child_to_complete($pid, 5);
if (!defined($rv)) {
kill(KILL => $pid)
}
}
}
exit(2) if $timed_out;
exit(3) if $rv & 0x7F; # Killed by some signal.
exit($rv >> 8); # Expect the exit code to comply with the spec.
}
はNagios Plugin Return Codesを使用します。タイムアウトは実際には2
を返します。
'Temporizador'は、その子の1つがARLMシグナルによって殺されたときにシェルによって出力されます。 '$?'はここで子供の終了コードではありません。それは子供を殺した信号の番号です(14)ORと128。 – ikegami