From e97886e9a16f67ff944ebc6263d50c84a35c3f02 Mon Sep 17 00:00:00 2001 From: hadaq Date: Wed, 28 Jan 2009 16:09:54 +0000 Subject: [PATCH] signal handling included. Sergey. --- plugins/check_lustre.pl | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/plugins/check_lustre.pl b/plugins/check_lustre.pl index cf49ce2..6a64f62 100755 --- a/plugins/check_lustre.pl +++ b/plugins/check_lustre.pl @@ -16,6 +16,7 @@ use IO::Handle; use Time::Local; use Time::localtime; use Getopt::Std; +#use POSIX qw(sys_wait_h signal_h); # Perl interface to IEEE Std 1003.1 # the following is for the status server # to communicate with Nagios plugin @@ -32,13 +33,20 @@ if($opt_h){ exit(0); } -my $status : shared = "OK"; +my $status : shared = "OK"; # status reported by Nagios my $time_ls : shared = &getTime(); my $sleep_time : shared = 600; # 10 minutes +my $ExitCode : shared = -1; # used to stop threads our $server_port = '50502'; our $protocol = 'tcp'; +#--- POSIX signal handlers: see signal(7) or kill(1) for available signals +foreach my $signal (qw(HUP INT QUIT ILL ABRT FPE SEGV TERM USR1 USR2)) +{ + $SIG{$signal} = sub { &exitProgram( $signal ); }; +} + #--- Daemonize if($opt_d){ open( STDIN, '/dev/null' ); @@ -105,6 +113,7 @@ sub statusServer{ Listen => SOMAXCONN ) )) { print "ERROR: Cannot start status server!\n"; + &exitProgram(2); } $selector = new IO::Select( $server_socket ); @@ -122,6 +131,7 @@ sub statusServer{ unless (defined( $client_socket = $server_socket->accept() )) { print "ERROR: Cannot open socket to send status!\n"; + &exitProgram(2); } #--- report the status @@ -175,7 +185,7 @@ sub getTotalSize return $totsize; } -sub checkLustreSize +sub checkLustreSize() { my $lustre_size = shift; @@ -187,6 +197,25 @@ sub checkLustreSize } } +sub exitProgram() +{ + #--- don't allow nested signal handling + return if ($ExitCode ne "-1"); + + #--- this will stop the treads, too + $ExitCode = shift; + + #--- wait until all threads ended + foreach my $thread (threads->list()) + { + $thread->join() + if ($thread->tid() && !threads::equal( $thread, threads->self() )); + } + + #--- exit with code 0 in case a signal was caught + exit( $ExitCode !~ /^\d+$/ ? 0 : $ExitCode ); +} + sub showHelp { print << 'EOF'; -- 2.43.0