Merge branch 'master' of git+ssh://github.com/adefaria/clearscm
[clearscm.git] / clearadm / plotloadavg.cgi
index d33afd9..8d51a21 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/local/bin/perl
 
 =pod
 
@@ -59,6 +59,7 @@ use strict;
 use warnings;
 
 use FindBin;
+use Convert::Base64;
 
 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
 
@@ -85,12 +86,6 @@ if ($opts{tiny}) {
   $opts{scaling} = 'Hour';
 } # if
 
-sub labelY ($) {
-  my ($value) = @_;
-
-  return $opts{tiny} ? '' : $value;  
-} # labelY
-
 my $clearadm = Clearadm->new;
 
 my $graph = GD::Graph::area->new ($opts{width}, $opts{height});
@@ -106,15 +101,15 @@ my @loads = $clearadm->GetLoadavg (
   $opts{start},
   $opts{end},
   $opts{points},
-  $opts{scaling}
+  $opts{scaling},
 );
 
-graphError "No loadavg data found for system $opts{system}"
+graphError "No loadavg data"
   unless @loads;
 
 my (@x, @y);
 
-foreach (@loads) {
+for (@loads) {
   my %load = %{$_};
   
   if ($opts{tiny}) {
@@ -122,9 +117,9 @@ foreach (@loads) {
   } else {
     push @x, $load{timestamp};
   } # if
-  
+
   push @y, $load{loadavg};
-} # foreach
+} # for
 
 my @data = ([@x], [@y]);
 
@@ -137,6 +132,7 @@ my $x_label_skip = @x > 1000 ? 200
 my $x_label = $opts{tiny} ? '' : 'Time';
 my $y_label = $opts{tiny} ? '' : 'Load';
 my $title   = $opts{tiny} ? '' : "Load Average for $opts{system}";
+my $labelY  = $opts{tiny} ? '' : '%.2f';
 
 $graph->set (
   x_label           => $x_label,
@@ -144,7 +140,7 @@ $graph->set (
   x_label_skip      => $x_label_skip,
   x_label_position  => .5,
   y_label           => $y_label,
-  y_number_format   => &labelY,
+  y_number_format   => $labelY,
   title             => $title,
   dclrs             => [$opts{color}],
   bgclr             => 'white',
@@ -159,8 +155,12 @@ $graph->set (
 my $image = $graph->plot(\@data)
   or croak $graph->error;
 
-print "Content-type: image/png\n\n";
-print $image->png;
+unless ($opts{generate}) {
+  print "Content-type: image/png\n\n";
+  print $image->png;
+} else {
+  print encode_base64 $image->png;
+} # unless
 
 =pod
 
@@ -195,9 +195,9 @@ L<GD::Graph::area|GD::Graph::area>
 =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/ClearadmWeb.pm">ClearadmWeb</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</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/ClearadmWeb.pm">ClearadmWeb</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
 </blockquote>
 
 =end html
@@ -212,4 +212,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