Merge branch 'collectd-4.6'
[collectd.git] / contrib / collection3 / bin / graph.cgi
index 7c29bb0..7fe2087 100755 (executable)
@@ -13,13 +13,30 @@ 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 $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)
 {
@@ -134,7 +151,14 @@ 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));
 
@@ -166,7 +190,7 @@ else
   }
 
   $| = 1;
-  RRDs::graph ('-', '-a', 'PNG', '--width', $GraphWidth, @timesel, @$args);
+  RRDs::graph ('-', '-a', $OutputFormat, '--width', $GraphWidth, @timesel, @$args);
   if (my $err = RRDs::error ())
   {
     print STDERR "RRDs::graph failed: $err\n";