7 use FindBin ('$RealBin');
8 use Carp (qw(confess cluck));
12 use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
13 use Collectd::Graph::TypeLoader (qw(tl_load_type));
15 use Collectd::Graph::Common (qw(sanitize_type get_selected_files
16 epoch_to_rfc1123 flush_files));
17 use Collectd::Graph::Type ();
19 our $Debug = param ('debug');
20 our $Begin = param ('begin');
21 our $End = param ('end');
22 our $GraphWidth = param ('width');
23 our $OutputFormat = 'PNG';
24 our $ContentType = 'image/png';
28 my $temp = param ('format') || '';
31 if ($temp =~ m/^(PNG|SVG|EPS|PDF)$/)
33 $OutputFormat = $temp;
35 if ($OutputFormat eq 'SVG') { $ContentType = 'image/svg+xml'; }
36 elsif ($OutputFormat eq 'EPS') { $ContentType = 'image/eps'; }
37 elsif ($OutputFormat eq 'PDF') { $ContentType = 'application/pdf'; }
44 Content-Type: text/plain
49 gc_read_config ("$RealBin/../etc/collection.conf");
53 $GraphWidth =~ s/\D//g;
58 $GraphWidth = gc_get_scalar ('GraphWidth', 400);
61 { # Sanitize begin and end times
70 if (!$Begin || !($Begin =~ m/^-?([1-9][0-9]*)$/))
79 $Begin = $End + $Begin;
83 $Begin = time () + $Begin;
89 $Begin = time () - 86400;
92 if (($End > 0) && ($Begin > $End))
100 my $type = param ('type') or die;
103 $obj = tl_load_type ($type);
106 confess ("tl_load_type ($type) failed");
109 $type = ucfirst (lc ($type));
110 $type =~ s/_([A-Za-z])/\U$1\E/g;
111 $type = sanitize_type ($type);
113 my $files = get_selected_files ();
116 require Data::Dumper;
117 print STDOUT Data::Dumper->Dump ([$files], ['files']);
124 my $expires = time ();
126 # OR (Begin is before `now' AND End is after `now')
127 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
129 # 400 == width in pixels
134 $timespan = $expires - $Begin;
138 $timespan = $End - $Begin;
140 $expires += int ($timespan / 400.0);
142 # IF (End is not `now')
143 # AND (End is before `now')
144 # ==> Graph will never change again!
145 elsif (($End > 0) && ($End < $expires))
147 $expires += (366 * 86400);
149 elsif ($Begin > $expires)
154 # Send FLUSH command to the daemon if necessary and possible.
158 addr => gc_get_scalar ('UnixSockAddr', undef),
159 interval => gc_get_scalar ('Interval', 10));
161 print STDOUT header (-Content_type => $ContentType,
162 -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
163 -Expires => epoch_to_rfc1123 ($expires));
167 print "\$expires = $expires;\n";
170 my $args = $obj->getRRDArgs (0);
174 require Data::Dumper;
175 print STDOUT Data::Dumper->Dump ([$obj], ['obj']);
176 print STDOUT join (",\n", @$args) . "\n";
177 print STDOUT "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
183 if ($End) # $Begin is always true
185 @timesel = ('-s', $Begin, '-e', $End);
189 @timesel = ('-s', $Begin); # End is implicitely `now'.
193 RRDs::graph ('-', '-a', $OutputFormat, '--width', $GraphWidth, @timesel, @$args);
194 if (my $err = RRDs::error ())
196 print STDERR "RRDs::graph failed: $err\n";
203 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :