Removed /usr/local from CDPATH
[clearscm.git] / clearadm / updatela.pl
index 6995175..250e06b 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 =pod
 
@@ -72,19 +72,18 @@ my $clearexec = Clearexec->new;
 my $host;
 
 # Given a host, formulate a loadavg record
-sub snapshotLoad ($) {
+sub snapshotLoad($) {
   my ($systemRef) = @_;
 
   my %system = %{$systemRef};
   
   my ($status, @output);
 
-  $status = $clearexec->connectToServer (
+  $status = $clearexec->connectToServer(
     $system{name}, $system{port}
   );
   
-  error "Unable to connect to system $system{name}:$system{port}", 1
-    unless $status;
+  error "Unable to connect to system $system{name}:$system{port}", 1 unless $status;
   
   verbose "Snapshotting load on $system{name}";
   
@@ -94,10 +93,9 @@ sub snapshotLoad ($) {
 
   my $cmd = 'uptime';
   
-  ($status, @output) = $clearexec->execute ($cmd);
+  ($status, @output) = $clearexec->execute($cmd);
 
-  return
-    if $status;
+  return if $status;
 
   # Parsing uptime is odd. Sometimes we get output like
   #
@@ -129,12 +127,11 @@ sub snapshotLoad ($) {
     my $loadvbs = 'c:/cygwin/opt/clearscm/clearadm/load.vbs';
     $cmd = "cscript /nologo $loadvbs";
        
-    ($status, @output) = $clearexec->execute ($cmd);
+    ($status, @output) = $clearexec->execute($cmd);
        
     chop @output if $output[0] =~ /\r/;
        
-    return
-      if $status;
+    return if $status;
          
     $load{loadavg} = $output[0] / 100;
   } # if
@@ -145,49 +142,70 @@ sub snapshotLoad ($) {
 } # snapshotLoad
 
 # Main
-GetOptions (
+GetOptions(
   'usage'   => sub { Usage },
   'verbose' => sub { set_verbose },
   'debug'   => sub { set_debug },
   'host=s'  => \$host,
 ) or Usage "Invalid parameter";
 
-Usage 'Extraneous options: ' . join ' ', @ARGV
-  if @ARGV;
+Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
 
 # Announce ourselves
 verbose "$FindBin::Script V$VERSION";
 
 my $exit = 0;
 
-foreach my $system ($clearadm->FindSystem ($host)) {
-  next if $$system{active} eq 'false';
+for my $system ($clearadm->FindSystem($host)) {
+  next if $system->{active} eq 'false';
   
   my %load = snapshotLoad $system;
   
   if (%load) {
-    my ($err, $msg) = $clearadm->AddLoadavg (%load);
+    my ($err, $msg) = $clearadm->AddLoadavg(%load);
   
     error $msg, $err if $err;
   } else {
-    error "Unable to get loadavg for system $$system{name}", 1;
+    error "Unable to get loadavg for system $system->{name}", 1;
   } # if
   
   # Check if over threshold
-  my %notification = $clearadm->GetNotification ('Loadavg');
+  my %notification = $clearadm->GetNotification('Loadavg');
 
-  next
-    unless %notification;
+  next unless %notification;
   
-  if ($load{loadavg} >= $$system{loadavgThreshold}) {
+  if ($load{loadavg} >= $system->{loadavgThreshold}) {
     $exit = 2;
-    error YMDHMS . " System: $$system{name} "
+    error YMDHMS . " System: $system->{name} "
         . "Loadavg $load{loadavg} "
-        . "Threshold $$system{loadavgThreshold}";
+        . "Threshold $system->{loadavgThreshold}";
   } else {
-    $clearadm->ClearNotifications ($$system{name});
+    $clearadm->ClearNotifications ($system->{name});
   } # if
-} # foreach
+
+  # Add graphs to system record
+  my ($loadavgsmall, $loadavg);
+
+  my $cmd = "plotloadavg.cgi generate=1 system=$system->{name} scaling=Hour points=24";
+
+  verbose "Generating loadavgsmall for $system->{name}";
+  my ($error, @output) = Execute("$cmd tiny=1 2>&1");
+
+  error 'Unable to generate loadavgsmall' . join("\n", @output), $error if $error;
+
+  $system->{loadavgsmall} = join '', @output;
+
+  verbose "Generating loadavg for $system->{name}";
+  ($error, @output) = Execute("$cmd 2>&1");
+  
+  error 'Unable to generate loadavg' . join("\n", @output), $error if $error;
+
+  $system->{loadavg} = join '', @output;
+
+  my ($err, $msg) = $clearadm->UpdateSystem($system->{name}, %$system);
+
+  error "Unable to udpate system record $msg", $err if $err;
+} # for
 
 exit $exit;
 
@@ -226,11 +244,11 @@ L<Net::Domain|Net::Domain>
 =begin html
 
 <blockquote>
-<a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/Utils.pm">Utils</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
 </blockquote>
 
 =end html
@@ -245,4 +263,4 @@ Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
 
 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
 
-=cut
\ No newline at end of file
+=cut