+ elsif ($Begin > $expires)
+ {
+ $expires = $Begin;
+ }
+
+# 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 header (-Content_type => $ContentType,
+ -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
+ -Expires => epoch_to_rfc1123 ($expires));
+
+ if (param ('debug'))
+ {
+ print "\$expires = $expires;\n";
+ }
+
+ my $args = $obj->getRRDArgs (0 + $Index);
+ if (param ('debug'))
+ {
+ require Data::Dumper;
+ print Data::Dumper->Dump ([$obj], ['obj']);
+ print join (",\n", @$args) . "\n";
+ print "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
+ }
+ else
+ {
+ my @timesel = ();
+ my $tmpfile = tmpnam ();
+ my $status;
+
+ if ($End) # $Begin is always true
+ {
+ @timesel = ('-s', $Begin, '-e', $End);
+ }
+ else
+ {
+ @timesel = ('-s', $Begin); # End is implicitely `now'.
+ }
+
+ if (-S "/var/run/rrdcached.sock" && -w "/var/run/rrdcached.sock")
+ {
+ $ENV{"RRDCACHED_ADDRESS"} = "/var/run/rrdcached.sock";
+ }
+ unlink ($tmpfile);
+ RRDs::graph ($tmpfile, '-a', $OutputFormat, '--width', $GraphWidth, '--height', $GraphHeight, @timesel, @$args);
+ if (my $err = RRDs::error ())
+ {
+ print STDERR "RRDs::graph failed: $err\n";
+ exit (1);
+ }
+
+ $status = open (IMG, '<', $tmpfile) or die ("open ($tmpfile): $!");
+ if (!$status)
+ {
+ print STDERR "graph.cgi: Unable to open temporary file \"$tmpfile\" for reading: $!\n";
+ }
+ else
+ {
+ local $/ = undef;
+ while (my $data = <IMG>)
+ {
+ print STDOUT $data;
+ }
+
+ close (IMG);
+ unlink ($tmpfile);
+ }
+ }
+} # sub main