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 ();
61 #gc_read_config ("$RealBin/../etc/collection.conf");
62 gc_read_config ("$BASE_DIR/etc/collection.conf");
69 my $Begin = param ('begin');
70 my $End = param ('end');
71 my $GraphWidth = param ('width');
72 my $GraphHeight = param ('height');
73 my $Index = param ('index') || 0;
74 my $OutputFormat = 'PNG';
75 my $ContentType = 'image/png';
79 my $temp = param ('format') || '';
82 if ($temp =~ m/^(PNG|SVG|EPS|PDF)$/)
84 $OutputFormat = $temp;
86 if ($OutputFormat eq 'SVG') { $ContentType = 'image/svg+xml'; }
87 elsif ($OutputFormat eq 'EPS') { $ContentType = 'image/eps'; }
88 elsif ($OutputFormat eq 'PDF') { $ContentType = 'application/pdf'; }
95 Content-Type: text/plain
98 $ContentType = 'text/plain';
105 $GraphWidth =~ s/\D//g;
110 $GraphWidth = gc_get_scalar ('GraphWidth', 400);
115 $GraphHeight =~ s/\D//g;
120 $GraphHeight = gc_get_scalar ('GraphHeight', 100);
123 { # Sanitize begin and end times
132 if (!$Begin || !($Begin =~ m/^-?([1-9][0-9]*)$/))
141 $Begin = $End + $Begin;
145 $Begin = time () + $Begin;
151 $Begin = time () - 86400;
154 if (($End > 0) && ($Begin > $End))
162 my $type = param ('type') or die;
165 $obj = tl_load_type ($type);
168 confess ("tl_load_type ($type) failed");
171 $type = ucfirst (lc ($type));
172 $type =~ s/_([A-Za-z])/\U$1\E/g;
173 $type = sanitize_type ($type);
175 my $files = get_selected_files ();
178 require Data::Dumper;
179 print Data::Dumper->Dump ([$files], ['files']);
186 my $expires = time ();
188 # OR (Begin is before `now' AND End is after `now')
189 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
191 # 400 == width in pixels
196 $timespan = $expires - $Begin;
200 $timespan = $End - $Begin;
202 $expires += int ($timespan / 400.0);
204 # IF (End is not `now')
205 # AND (End is before `now')
206 # ==> Graph will never change again!
207 elsif (($End > 0) && ($End < $expires))
209 $expires += (366 * 86400);
211 elsif ($Begin > $expires)
216 # Send FLUSH command to the daemon if necessary and possible.
220 addr => gc_get_scalar ('UnixSockAddr', undef),
221 interval => gc_get_scalar ('Interval', 10));
223 print header (-Content_type => $ContentType,
224 -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
225 -Expires => epoch_to_rfc1123 ($expires));
229 print "\$expires = $expires;\n";
232 my $args = $obj->getRRDArgs (0 + $Index);
235 require Data::Dumper;
236 print Data::Dumper->Dump ([$obj], ['obj']);
237 print join (",\n", @$args) . "\n";
238 print "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
243 my $tmpfile = tmpnam ();
246 if ($End) # $Begin is always true
248 @timesel = ('-s', $Begin, '-e', $End);
252 @timesel = ('-s', $Begin); # End is implicitely `now'.
255 if (-S "/var/run/rrdcached.sock" && -w "/var/run/rrdcached.sock")
257 $ENV{"RRDCACHED_ADDRESS"} = "/var/run/rrdcached.sock";
260 RRDs::graph ($tmpfile, '-a', $OutputFormat, '--width', $GraphWidth, '--height', $GraphHeight, @timesel, @$args);
261 if (my $err = RRDs::error ())
263 print STDERR "RRDs::graph failed: $err\n";
267 $status = open (IMG, '<', $tmpfile) or die ("open ($tmpfile): $!");
270 print STDERR "graph.cgi: Unable to open temporary file \"$tmpfile\" for reading: $!\n";
275 while (my $data = <IMG>)
288 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :