collection3/etc/collection.conf: Reencoded in UTF-8.
[collectd.git] / contrib / collection3 / bin / graph.cgi
index c84199d..8a5bf85 100755 (executable)
@@ -3,21 +3,43 @@
 use strict;
 use warnings;
 use lib ('../lib');
+use utf8;
 
 use FindBin ('$RealBin');
 use Carp (qw(confess cluck));
 use CGI (':cgi');
 use RRDs ();
 
-use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type));
+use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
+use Collectd::Graph::TypeLoader (qw(tl_load_type));
 
 use Collectd::Graph::Common (qw(sanitize_type get_selected_files
-      epoch_to_rfc1123));
+      epoch_to_rfc1123 flush_files));
 use Collectd::Graph::Type ();
 
 our $Debug = param ('debug');
 our $Begin = param ('begin');
 our $End = param ('end');
+our $GraphWidth = param ('width');
+our $GraphHeight = param ('height');
+our $Index = param ('index') || 0;
+our $OutputFormat = 'PNG';
+our $ContentType = 'image/png';
+
+if (param ('format'))
+{
+  my $temp = param ('format') || '';
+  $temp = uc ($temp);
+
+  if ($temp =~ m/^(PNG|SVG|EPS|PDF)$/)
+  {
+    $OutputFormat = $temp;
+
+    if ($OutputFormat eq 'SVG') { $ContentType = 'image/svg+xml'; }
+    elsif ($OutputFormat eq 'EPS') { $ContentType = 'image/eps'; }
+    elsif ($OutputFormat eq 'PDF') { $ContentType = 'application/pdf'; }
+  }
+}
 
 if ($Debug)
 {
@@ -27,7 +49,27 @@ Content-Type: text/plain
 HTTP
 }
 
-tl_read_config ("$RealBin/../etc/collection3.conf");
+gc_read_config ("$RealBin/../etc/collection.conf");
+
+if ($GraphWidth)
+{
+  $GraphWidth =~ s/\D//g;
+}
+
+if (!$GraphWidth)
+{
+  $GraphWidth = gc_get_scalar ('GraphWidth', 400);
+}
+
+if ($GraphHeight)
+{
+  $GraphHeight =~ s/\D//g;
+}
+
+if (!$GraphHeight)
+{
+  $GraphHeight = gc_get_scalar ('GraphHeight', 100);
+}
 
 { # Sanitize begin and end times
   $End ||= 0;
@@ -93,26 +135,52 @@ for (@$files)
 }
 
 my $expires = time ();
+# IF (End is `now')
+#    OR (Begin is before `now' AND End is after `now')
 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
 {
   # 400 == width in pixels
-  my $timespan = $expires - $Begin;
-  $expires += int ($timespan / 400);
+  my $timespan;
+
+  if ($End == 0)
+  {
+    $timespan = $expires - $Begin;
+  }
+  else
+  {
+    $timespan = $End - $Begin;
+  }
+  $expires += int ($timespan / 400.0);
 }
+# IF (End is not `now')
+#    AND (End is before `now')
+# ==> Graph will never change again!
 elsif (($End > 0) && ($End < $expires))
 {
-  $expires += 366 * 86400;
+  $expires += (366 * 86400);
 }
 elsif ($Begin > $expires)
 {
   $expires = $Begin;
 }
 
-print STDOUT header (-Content_type => 'image/png',
+# Send FLUSH command to the daemon if necessary and possible.
+flush_files ($files,
+    begin => $Begin,
+    end => $End,
+    addr => gc_get_scalar ('UnixSockAddr', undef),
+    interval => gc_get_scalar ('Interval', 10));
+
+print STDOUT header (-Content_type => $ContentType,
   -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
   -Expires => epoch_to_rfc1123 ($expires));
 
-my $args = $obj->getRRDArgs (0);
+if ($Debug)
+{
+  print "\$expires = $expires;\n";
+}
+
+my $args = $obj->getRRDArgs (0 + $Index);
 
 if ($Debug)
 {
@@ -135,7 +203,7 @@ else
   }
 
   $| = 1;
-  RRDs::graph ('-', '-a', 'PNG', @timesel, @$args);
+  RRDs::graph ('-', '-a', $OutputFormat, '--width', $GraphWidth, '--height', $GraphHeight, @timesel, @$args);
   if (my $err = RRDs::error ())
   {
     print STDERR "RRDs::graph failed: $err\n";