]> jspc29.x-matter.uni-frankfurt.de Git - hadesicinga.git/commitdiff
signal handling included. Sergey.
authorhadaq <hadaq>
Wed, 28 Jan 2009 16:09:54 +0000 (16:09 +0000)
committerhadaq <hadaq>
Wed, 28 Jan 2009 16:09:54 +0000 (16:09 +0000)
plugins/check_lustre.pl

index cf49ce2c047a8cf4a4c3aca2d810e375321a95a5..6a64f62435759f7a83011047fb92df64f21eebd1 100755 (executable)
@@ -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';