3 # Copyright (C) 2008-2011 Florian Forster
4 # Copyright (C) 2011 noris network AG
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; only version 2 of the License is applicable.
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # You should have received a copy of the GNU General Public License along with
16 # this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Florian "octo" Forster <octo at collectd.org>
25 use vars (qw($BASE_DIR));
29 if (defined $ENV{'SCRIPT_FILENAME'})
31 if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$})
34 unshift (@::INC, "$::BASE_DIR/lib");
39 use Carp (qw(confess cluck));
42 use File::Temp (':POSIX');
44 use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
45 use Collectd::Graph::TypeLoader (qw(tl_load_type));
47 use Collectd::Graph::Common (qw(sanitize_type get_selected_files
48 epoch_to_rfc1123 flush_files));
49 use Collectd::Graph::Type ();
53 if (defined $::BASE_DIR)
58 if (!defined ($ENV{'SCRIPT_FILENAME'}))
63 if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$})
74 my $base = base_dir ();
88 my $base = base_dir ();
102 my $lib_dir = lib_dir ();
103 my $sysconf_dir = sysconf_dir ();
105 if (!grep { $lib_dir eq $_ } (@::INC))
107 unshift (@::INC, $lib_dir);
110 gc_read_config ("$sysconf_dir/collection.conf");
115 my $Begin = param ('begin');
116 my $End = param ('end');
117 my $GraphWidth = param ('width');
118 my $GraphHeight = param ('height');
119 my $Index = param ('index') || 0;
120 my $OutputFormat = 'PNG';
121 my $ContentType = 'image/png';
125 if (param ('format'))
127 my $temp = param ('format') || '';
130 if ($temp =~ m/^(PNG|SVG|EPS|PDF)$/)
132 $OutputFormat = $temp;
134 if ($OutputFormat eq 'SVG') { $ContentType = 'image/svg+xml'; }
135 elsif ($OutputFormat eq 'EPS') { $ContentType = 'image/eps'; }
136 elsif ($OutputFormat eq 'PDF') { $ContentType = 'application/pdf'; }
143 Content-Type: text/plain
146 $ContentType = 'text/plain';
151 $GraphWidth =~ s/\D//g;
156 $GraphWidth = gc_get_scalar ('GraphWidth', 400);
161 $GraphHeight =~ s/\D//g;
166 $GraphHeight = gc_get_scalar ('GraphHeight', 100);
169 { # Sanitize begin and end times
178 if (!$Begin || !($Begin =~ m/^-?([1-9][0-9]*)$/))
187 $Begin = $End + $Begin;
191 $Begin = time () + $Begin;
197 $Begin = time () - 86400;
200 if (($End > 0) && ($Begin > $End))
208 my $type = param ('type') or die;
211 $obj = tl_load_type ($type);
214 confess ("tl_load_type ($type) failed");
217 $type = ucfirst (lc ($type));
218 $type =~ s/_([A-Za-z])/\U$1\E/g;
219 $type = sanitize_type ($type);
221 my $files = get_selected_files ();
224 require Data::Dumper;
225 print Data::Dumper->Dump ([$files], ['files']);
232 my $expires = time ();
234 # OR (Begin is before `now' AND End is after `now')
235 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
237 # 400 == width in pixels
242 $timespan = $expires - $Begin;
246 $timespan = $End - $Begin;
248 $expires += int ($timespan / 400.0);
250 # IF (End is not `now')
251 # AND (End is before `now')
252 # ==> Graph will never change again!
253 elsif (($End > 0) && ($End < $expires))
255 $expires += (366 * 86400);
257 elsif ($Begin > $expires)
262 # Send FLUSH command to the daemon if necessary and possible.
266 addr => gc_get_scalar ('UnixSockAddr', undef),
267 interval => gc_get_scalar ('Interval', 10));
269 print header (-Content_type => $ContentType,
270 -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
271 -Expires => epoch_to_rfc1123 ($expires));
275 print "\$expires = $expires;\n";
278 my $args = $obj->getRRDArgs (0 + $Index);
281 require Data::Dumper;
282 print Data::Dumper->Dump ([$obj], ['obj']);
283 print join (",\n", @$args) . "\n";
284 print "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
289 my $tmpfile = tmpnam ();
292 if ($End) # $Begin is always true
294 @timesel = ('-s', $Begin, '-e', $End);
298 @timesel = ('-s', $Begin); # End is implicitely `now'.
301 if (-S "/var/run/rrdcached.sock" && -w "/var/run/rrdcached.sock")
303 $ENV{"RRDCACHED_ADDRESS"} = "/var/run/rrdcached.sock";
306 RRDs::graph ($tmpfile, '-a', $OutputFormat, '--width', $GraphWidth, '--height', $GraphHeight, @timesel, @$args);
307 if (my $err = RRDs::error ())
309 print STDERR "RRDs::graph failed: $err\n";
313 $status = open (IMG, '<', $tmpfile) or die ("open ($tmpfile): $!");
316 print STDERR "graph.cgi: Unable to open temporary file \"$tmpfile\" for reading: $!\n";
321 while (my $data = <IMG>)
334 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :