Merge branch 'master' of defaria.com:/opt/git/clearscm
[clearscm.git] / lib / Utils.pm
index 74fe13c..9666d81 100644 (file)
@@ -74,6 +74,7 @@ our @EXPORT = qw (
   GetChildren
   GetPassword
   InArray
+  LoadAvg
   PageOutput
   PipeOutput
   PipeOutputArray
@@ -346,6 +347,7 @@ Returns:
 
 =cut  
 
+  
   $prompt ||= 'Password';
   
   my $password;
@@ -429,6 +431,60 @@ Returns:
   return $FALSE;
 } # InArray
 
+sub LoadAvg () {
+
+=pod
+
+=head2 LoadAvg ()
+
+Return an array of the 1, 5, and 15 minute load averages.
+
+Parameters:
+
+=for html <blockquote>
+
+=over
+
+=item none
+
+=back
+
+=for html </blockquote>
+
+Returns:
+
+=for html <blockquote>
+
+=over
+
+=item An array of the 1, 5, and 15 minute load averages in a list context.
+In a scalar context just the 1 minute load average.
+
+=back
+
+=for html </blockquote>
+
+=cut  
+
+  # TODO: Make it work on Windows...
+  return if $^O =~ /win/i;
+  
+  open my $loadAvg, '/proc/loadavg'
+    or croak "Unable to open /proc/loadavg\n";
+    
+  my $load = <$loadAvg>;
+  
+  close $loadAvg;
+  
+  my @loadAvgs = split /\s/, $load;
+  
+  if (wantarray) {
+    return @loadAvgs;
+  } else {
+    return $loadAvgs[0]; # This is the 1 minute average
+  }
+} # LoadAvg
+
 our $pipe;
 
 sub StartPipe ($;$) {
@@ -826,7 +882,8 @@ and the values of the hash will be the counters.
 
 =item $log
 
-Logger object to log stats to (if specified)
+Logger object to log stats to (if specified). Note: if the Logger object has 
+errors or warnings then they will be automatically included in the output.
 
 =back
 
@@ -848,7 +905,12 @@ Returns:
 
   my $msg = "$FindBin::Script Run Statistics:";
   
-  if (scalar keys %$total) {
+  if ($log and ref $log eq 'Logger') {
+    $total->{errors}   = $log->{errors};
+    $total->{warnings} = $log->{warnings};
+  } # if
+  
+  if (keys %$total) {
     # Display statistics (if any)
     if ($log) {
       $log->msg ($msg);
@@ -857,10 +919,10 @@ Returns:
     } # if
 
     foreach (sort keys %$total) {
-      $msg = $$total{$_} . "\t $_";
+      $msg = $total->{$_} . "\t $_";
       
       if ($log) {
-        $log->msg ($$total{$_} . "\t $_");
+        $log->msg ($total->{$_} . "\t $_");
       } else {
         display $msg;
       } # if
@@ -935,8 +997,6 @@ L<FindBin>
 
 L<POSIX>
 
-L<Term::ReadKey>
-
 =head2 ClearSCM Perl Modules
 
 =for html <p><a href="/php/scm_man.php?file=lib/Display.pm">Display</a></p>