Removed /usr/local from CDPATH
[clearscm.git] / clearadm / cleartasks.pl
index 66c964b..7e36b82 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 =pod
 
@@ -47,6 +47,9 @@ $Date: 2013/06/02 18:47:26 $
 
 Examine the Clearadm schedule and perform the tasks required.
 
+Note that sending the Cleartasks.pl process a sigusr1 will cause it to toggle
+verbose mode.
+
 =cut
 
 use strict;
@@ -54,6 +57,7 @@ use warnings;
 
 use FindBin;
 use Getopt::Long;
+use Sys::Hostname;
 
 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
 
@@ -67,7 +71,10 @@ use Utils;
 my $VERSION  = '$Revision: 1.25 $';
   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
 
-my $logfile = "$Clearadm::CLEAROPTS{CLEARADM_LOGDIR}/$FindBin::Script.log";           
+my $logfile =  "$Clearadm::CLEAROPTS{CLEARADM_LOGDIR}/$FindBin::Script";
+   $logfile =~ s/\.pl$//;
+   $logfile .= '.' . hostname() . '.log';
+
 my $pidfile = "$Clearadm::CLEAROPTS{CLEARADM_RUNDIR}/$FindBin::Script.pid";
 my $daemon  = 1;
 
@@ -76,7 +83,19 @@ $ENV{PATH} .= ":$Clearadm::CLEAROPTS{CLEARADM_BASE}";
 
 my ($clearadm, $clearexec);
 
-sub HandleSystemNotCheckingIn (%) {
+sub ToggleVerbose() {
+  if (get_verbose) {
+    display 'Turning verbose off';
+    set_verbose 0;
+  } else {
+    display 'Turning verbose on';
+    set_verbose 1;
+  } # if
+} # ToggleVerbose
+
+$SIG{USR1} = \&ToggleVerbose;
+
+sub HandleSystemNotCheckingIn(%) {
   my (%system) = @_;
    
   my $startTime = time;
@@ -91,7 +110,7 @@ sub HandleSystemNotCheckingIn (%) {
     system   => $system{name},
   );
 
-  my ($err, $msg, $lastid) = $clearadm->AddRunlog (%runlog);
+  my ($err, $msg, $lastid) = $clearadm->AddRunlog(%runlog);
   
   $clearadm->Error ("Unable to add to runlog (Status: $err)\n$msg") if $err;
    
@@ -104,8 +123,8 @@ sub HandleSystemNotCheckingIn (%) {
      $systemLink     .= "/systemdetails.cgi?system=$system{name}";
   my $runlogLink      = $Clearadm::CLEAROPTS{CLEARADM_WEBBASE};
      $runlogLink     .= "/runlog.cgi?id=$lastid";
-   my $subject         = "System is not responding (Is clearagent running?)";
-     $message = <<"END";      
+  my $subject         = "System is not responding (Is clearagent running?)";
+     $message         = <<"END";      
 <center>
 <h1><font color="red">Alert</font> System not responding!</h1>
 </center>
@@ -115,7 +134,7 @@ href="$runlogLink">not responding</a> to clearagent requests. This can happen if
 clearagent is not setup and running on the system.</p> 
 END
      
-  $clearadm->Notify (
+  $clearadm->Notify(
     $notification{name},
     $subject,
     $message,
@@ -130,8 +149,8 @@ END
   return;
 } # HandleSystemNotCheckingIn
 
-sub SystemsCheckin () {
-  foreach ($clearadm->FindSystem) {
+sub SystemsCheckin() {
+  for ($clearadm->FindSystem) {
     my %system = %$_;
     
     next if $system{active} eq 'false';
@@ -140,10 +159,7 @@ sub SystemsCheckin () {
     
     my $startTime = time;
     
-    my $status = $clearexec->connectToServer (
-      $system{name},
-      $system{port}
-    );
+    my $status = $clearexec->connectToServer($system{name}, $system{port});
     
     unless ($status) {
       HandleSystemNotCheckingIn %system;
@@ -156,19 +172,16 @@ sub SystemsCheckin () {
           . "$system{name}:$system{port}";
     
     display __FILE__ . " DEBUG: System undefined 1" unless $system{name};
-    $clearadm->UpdateSystem (
-      $system{name},
-      (lastheardfrom => Today2SQLDatetime)
-    );
+    $clearadm->UpdateSystem($system{name}, (lastheardfrom => Today2SQLDatetime));
   
     $clearadm->ClearNotifications ($system{name})
       if $system{notification} and $system{notification} eq 'Heartbeat';
-  } # foreach
+  } # for
   
   return;
 } # SystemsCheckin
 
-sub UpdateRunlog ($$$$) {
+sub UpdateRunlog($$$$) {
   my ($status, $startTime, $task, $output) = @_;
   
   my %runlog = (
@@ -196,28 +209,28 @@ sub UpdateRunlog ($$$$) {
     } # if
   } # if
     
-  my ($err, $msg, $lastid) = $clearadm->AddRunlog (%runlog);
+  my ($err, $msg, $lastid) = $clearadm->AddRunlog(%runlog);
     
-  $clearadm->Error ($msg, $err) if $err;
+  $clearadm->Error($msg, $err) if $err;
 
   return $lastid;
 } # UpdateRunlog
 
-sub MakeSystemLink ($) {
+sub MakeSystemLink($) {
   my ($system) = @_;
   
   return "$Clearadm::CLEAROPTS{CLEARADM_WEBBASE}/systemdetails.cgi?system="
        . $system;
 } # MakeSystemLink
 
-sub MakeLoadavgLink ($) {
+sub MakeLoadavgLink($) {
   my ($system) = @_;
 
   return "$Clearadm::CLEAROPTS{CLEARADM_WEBBASE}/plot.cgi?type=loadavg&system="
        . "$system&scaling=Hour&points=24";
 } # MakeLoadavgLink
 
-sub ProcessLoadavgErrors ($$$$@) {
+sub ProcessLoadavgErrors($$$$@) {
   # TODO: Also need to handle the case where the error was something other
   # than "Load average over threshold". Perhaps by having different return
   # status. Also, runlog entry #22169 never reported!
@@ -225,7 +238,7 @@ sub ProcessLoadavgErrors ($$$$@) {
   
   my $when = Today2SQLDatetime;
   
-  foreach (@output) {
+  for (@output) {
     # We need to log this output. Write it to STDOUT
     display $_;
 
@@ -271,12 +284,12 @@ END
 END
       $message .= join "\n", @output;
       $message .= "</pre>";
-      $clearadm->Error ($message, -1);
+      $clearadm->Error($message, -1);
       
       last;
     } # if
 
-    $clearadm->Notify (
+    $clearadm->Notify(
       $notification,
       $subject,
       $message,
@@ -285,12 +298,12 @@ END
       undef,
       $lastid,
     );
-  } # foreach
+  } # for
   
   return;
 } # ProcessLoadAvgErrors
 
-sub ProcessFilesystemErrors ($$$$@) {
+sub ProcessFilesystemErrors($$$$@) {
   # TODO: Also need to handle the case where the error was something other
   # than "Filesystem over threshold". Perhaps by having different return
   # status.
@@ -300,7 +313,7 @@ sub ProcessFilesystemErrors ($$$$@) {
 
   my %system;
   
-  foreach (@output) {
+  for (@output) {
     # We need to log this output. Write it to STDOUT
     display $_;
     
@@ -317,9 +330,9 @@ sub ProcessFilesystemErrors ($$$$@) {
         $system{$1} = \%fsinfo;
       } # if
     } # if
-  } # foreach
+  } # for
    
-  foreach my $systemName (keys %system) {
+  for my $systemName (keys %system) {
     my @fsinfo;
     
     if (ref $system{$systemName} eq 'HASH') {
@@ -328,7 +341,7 @@ sub ProcessFilesystemErrors ($$$$@) {
        push @fsinfo, @{$system{$systemName}};
     } # if
 
-    my $systemLink = MakeSystemLink ($systemName);
+    my $systemLink = MakeSystemLink($systemName);
     my $subject    = 'Filesystem has exceeded threshold';
     my $message = <<"END";      
 <center>
@@ -340,7 +353,7 @@ were over their threshold.</p>
 
 <ul>
 END
-    foreach (@fsinfo) {
+    for (@fsinfo) {
       my %fsinfo = %{$_};
       my $filesystemLink  = $Clearadm::CLEAROPTS{CLEARADM_WEBBASE};
          $filesystemLink .= "/plot.cgi?type=filesystem&system=$systemName";
@@ -349,11 +362,11 @@ END
       $message .= "<li>Filesystem <a href=\"$filesystemLink\">";
       $message .= "$fsinfo{filesystem}</a> is $fsinfo{usedPct}% full. Threshold is ";
       $message .= "$fsinfo{threshold}%</li>";
-    } # foreach
+    } # for
       
     $message .= "</ul>";
     
-    $clearadm->Notify (
+    $clearadm->Notify(
       $notification,
       $subject,
       $message,
@@ -362,12 +375,12 @@ END
       undef,
       $lastid,
     );
-  } # foreach
+  } # for
   
   return;
 } # ProcessFilesystemErrors
 
-sub NonZeroReturn ($$$$$$) {
+sub NonZeroReturn($$$$$$) {
   my ($system, $notification, $status, $lastid, $output, $task) = @_;
 
   my @output = @{$output};
@@ -405,7 +418,7 @@ END
 
   $message .= "</pre></blockquote>";
   
-  $clearadm->Notify (
+  $clearadm->Notify(
     $notification,
     $subject,
     $message,
@@ -418,7 +431,7 @@ END
   return;   
 } # NonZeroReturn
 
-sub ExecuteTask ($%) {
+sub ExecuteTask($%) {
   my ($sleep, %task) = @_;
   
   my ($status, @output, %system, $subject, $message);
@@ -447,7 +460,7 @@ sub ExecuteTask ($%) {
                  . "execute $task{command}";
       $status = -1;
     } else {
-      ($status, @output) = $clearexec->execute ($task{command});
+      ($status, @output) = $clearexec->execute($task{command});
       
       $output[0] = "Unable to exec $task{command} on $system{name}"
         if $status == -1;
@@ -461,7 +474,7 @@ sub ExecuteTask ($%) {
   if ($status != 0) {
     if ($notification{cond}
       and $notification{cond} =~ /non zero return/i) {
-      NonZeroReturn (
+      NonZeroReturn(
         $system{name},
         $notification{name},
         $status,
@@ -470,20 +483,20 @@ sub ExecuteTask ($%) {
         \%task
       );
     } elsif ($notification{cond} =~ /loadavg over threshold/i) {
-      ProcessLoadavgErrors ($notification{name}, $task{name}, $system{name}, $lastid, @output);
+      ProcessLoadavgErrors($notification{name}, $task{name}, $system{name}, $lastid, @output);
     } elsif ($notification{cond} =~ /filesystem over threshold/i) {
-      ProcessFilesystemErrors ($notification{name}, $task{name}, $system{name}, $lastid, @output);
+      ProcessFilesystemErrors($notification{name}, $task{name}, $system{name}, $lastid, @output);
     } # if
   } else {
-    $clearadm->ClearNotifications ($task{system});
+    $clearadm->ClearNotifications($task{system});
   } # if
         
-  my ($err, $msg) = $clearadm->UpdateSchedule (
+  my ($err, $msg) = $clearadm->UpdateSchedule(
     $task{schedulename},
     ( 'lastrunid' => $lastid ),
   );
     
-  $clearadm->Error ($msg, $err) if $err;  
+  $clearadm->Error($msg, $err) if $err;
   
   $sleep -= time - $startTime;
   
@@ -491,7 +504,7 @@ sub ExecuteTask ($%) {
 } # ExecuteTask
 
 # Main
-GetOptions (
+GetOptions(
   'usage'     => sub { Usage },
   'verbose'   => sub { set_verbose },
   'debug'     => sub { set_debug },
@@ -499,11 +512,9 @@ GetOptions (
   'pidfile=s' => \$pidfile,
 ) or Usage "Invalid parameter";
 
-Usage 'Extraneous options: ' . join ' ', @ARGV
-  if @ARGV;
+Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
 
-EnterDaemonMode $logfile, $logfile, $pidfile
-  if $daemon;
+EnterDaemonMode $logfile, $logfile, $pidfile if $daemon;
 
 display "$FindBin::Script V$VERSION started at " . localtime;
 
@@ -518,26 +529,28 @@ while () {
   
   my ($sleep, @workItems) = $clearadm->GetWork;
   
-  foreach (@workItems) {
+  for (@workItems) {
     my %scheduledTask = %{$_};
     
     $scheduledTask{system} ||= 'All systems';
     
     if ($scheduledTask{system} =~ /all systems/i) {
-      foreach my $system ($clearadm->FindSystem) {
+      for my $system ($clearadm->FindSystem) {
+        next if $$system{active} eq 'false';
+
         $scheduledTask{system} = $$system{name};
         $sleep = ExecuteTask $sleep, %scheduledTask;
-      } # foreach
+      } # for
     } else {
       $sleep = ExecuteTask $sleep, %scheduledTask;
     } # if
-  } # foreach  
+  } # for  
   
   if ($sleep > 0) {
     verbose "Sleeping for $sleep seconds";
     sleep $sleep;
   } # if  
-} # foreach
+} # for
 
 =pod