8 use FindBin ('$RealBin');
9 use Carp (qw(confess cluck));
13 use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
14 use Collectd::Graph::TypeLoader (qw(tl_load_type));
16 use Collectd::Graph::Common (qw(sanitize_type get_selected_files
17 epoch_to_rfc1123 flush_files));
18 use Collectd::Graph::Type ();
20 our $Debug = param ('debug');
21 our $Begin = param ('begin');
22 our $End = param ('end');
23 our $GraphWidth = param ('width');
24 our $GraphHeight = param ('height');
25 our $Index = param ('index') || 0;
26 our $OutputFormat = 'PNG';
27 our $ContentType = 'image/png';
31 my $temp = param ('format') || '';
34 if ($temp =~ m/^(PNG|SVG|EPS|PDF)$/)
36 $OutputFormat = $temp;
38 if ($OutputFormat eq 'SVG') { $ContentType = 'image/svg+xml'; }
39 elsif ($OutputFormat eq 'EPS') { $ContentType = 'image/eps'; }
40 elsif ($OutputFormat eq 'PDF') { $ContentType = 'application/pdf'; }
47 Content-Type: text/plain
52 gc_read_config ("$RealBin/../etc/collection.conf");
56 $GraphWidth =~ s/\D//g;
61 $GraphWidth = gc_get_scalar ('GraphWidth', 400);
66 $GraphHeight =~ s/\D//g;
71 $GraphHeight = gc_get_scalar ('GraphHeight', 100);
74 { # Sanitize begin and end times
83 if (!$Begin || !($Begin =~ m/^-?([1-9][0-9]*)$/))
92 $Begin = $End + $Begin;
96 $Begin = time () + $Begin;
102 $Begin = time () - 86400;
105 if (($End > 0) && ($Begin > $End))
113 my $type = param ('type') or die;
116 $obj = tl_load_type ($type);
119 confess ("tl_load_type ($type) failed");
122 $type = ucfirst (lc ($type));
123 $type =~ s/_([A-Za-z])/\U$1\E/g;
124 $type = sanitize_type ($type);
126 my $files = get_selected_files ();
129 require Data::Dumper;
130 print STDOUT Data::Dumper->Dump ([$files], ['files']);
137 my $expires = time ();
139 # OR (Begin is before `now' AND End is after `now')
140 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
142 # 400 == width in pixels
147 $timespan = $expires - $Begin;
151 $timespan = $End - $Begin;
153 $expires += int ($timespan / 400.0);
155 # IF (End is not `now')
156 # AND (End is before `now')
157 # ==> Graph will never change again!
158 elsif (($End > 0) && ($End < $expires))
160 $expires += (366 * 86400);
162 elsif ($Begin > $expires)
167 # Send FLUSH command to the daemon if necessary and possible.
171 addr => gc_get_scalar ('UnixSockAddr', undef),
172 interval => gc_get_scalar ('Interval', 10));
174 print STDOUT header (-Content_type => $ContentType,
175 -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
176 -Expires => epoch_to_rfc1123 ($expires));
180 print "\$expires = $expires;\n";
183 my $args = $obj->getRRDArgs (0 + $Index);
187 require Data::Dumper;
188 print STDOUT Data::Dumper->Dump ([$obj], ['obj']);
189 print STDOUT join (",\n", @$args) . "\n";
190 print STDOUT "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
196 if ($End) # $Begin is always true
198 @timesel = ('-s', $Begin, '-e', $End);
202 @timesel = ('-s', $Begin); # End is implicitely `now'.
206 RRDs::graph ('-', '-a', $OutputFormat, '--width', $GraphWidth, '--height', $GraphHeight, @timesel, @$args);
207 if (my $err = RRDs::error ())
209 print STDERR "RRDs::graph failed: $err\n";
216 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :