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(cluck confess));
41 use CGI::Carp ('fatalsToBrowser');
42 use HTML::Entities ('encode_entities');
46 use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
47 use Collectd::Graph::TypeLoader (qw(tl_load_type));
48 use Collectd::Graph::Common (qw(get_files_from_directory get_all_hosts
49 get_timespan_selection get_selected_files get_host_selection
50 get_plugin_selection flush_files));
51 use Collectd::Graph::Type ();
64 list_hosts => \&action_list_hosts,
65 show_selection => \&action_show_selection
70 if (defined $::BASE_DIR)
75 if (!defined ($ENV{'SCRIPT_FILENAME'}))
80 if ($ENV{'SCRIPT_FILENAME'} =~ m{^(/.+)/bin/[^/]+$})
91 my $base = base_dir ();
105 my $base = base_dir ();
119 my $lib_dir = lib_dir ();
120 my $sysconf_dir = sysconf_dir ();
122 if (!grep { $lib_dir eq $_ } (@::INC))
124 unshift (@::INC, $lib_dir);
127 gc_read_config ("$sysconf_dir/collection.conf");
132 my $Debug = param ('debug') ? 1 : 0;
133 my $action = param ('action') || 'list_hosts';
135 if (!exists ($Actions{$action}))
137 print STDERR "No such action: $action\n";
143 $Actions{$action}->();
151 if (!defined $ENV{'HTTP_ACCEPT'})
156 for (split (',', $ENV{'HTTP_ACCEPT'}))
161 if ($type =~ m#^([^;]+);q=([0-9\.]+)$#)
169 if (!defined ($types{'application/xhtml+xml'}))
173 elsif (!defined ($types{'text/html'}))
177 elsif ($types{'application/xhtml+xml'} < $types{'text/html'})
190 return if ($html_started);
197 $timespan = get_timespan_selection ();
198 $begin = $end - $timespan;
200 if (can_handle_xhtml ())
202 print header (-Content_Type => 'application/xhtml+xml; charset=UTF-8');
204 <?xml version="1.0" encoding="UTF-8"?>
205 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
206 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
207 <html xmlns="http://www.w3.org/1999/xhtml"
208 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
209 xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
215 print header (-Content_Type => 'text/html; charset=UTF-8');
217 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
218 "http://www.w3.org/TR/html4/strict.dtd">
224 <title>collection.cgi, Version 3</title>
225 <link rel="icon" href="../share/shortcut-icon.png" type="image/png" />
226 <link rel="stylesheet" href="../share/style.css" type="text/css" />
227 <script type="text/javascript" src="../share/navigate.js"></script>
229 <body onload="nav_init ($begin, $end);">
243 sub contains_invalid_chars
247 for (split (m//, $str))
251 # Whitespace is allowed.
252 if (($n >= 9) && ($n <= 13))
267 my $timespan_selection = get_timespan_selection ();
268 my $host_selection = get_host_selection ();
269 my $plugin_selection = get_plugin_selection ();
272 <form action="${\script_name ()}" method="get">
274 <legend>Data selection</legend>
275 <select name="hostname" multiple="multiple" size="15">
277 for (sort (keys %$host_selection))
279 next if contains_invalid_chars ($_);
280 my $host = encode_entities ($_);
281 my $selected = $host_selection->{$_}
282 ? ' selected="selected"'
284 print qq# <option value="$host"$selected>$host</option>\n#;
288 <select name="plugin" multiple="multiple" size="15">
290 for (sort (keys %$plugin_selection))
292 next if contains_invalid_chars ($_);
293 my $plugin = encode_entities ($_);
294 my $selected = $plugin_selection->{$_}
295 ? ' selected="selected"'
297 print qq# <option value="$plugin"$selected>$plugin</option>\n#;
301 <select name="timespan">
303 for (sort { $TimeSpans->{$a} <=> $TimeSpans->{$b} } (keys (%$TimeSpans)))
305 next if contains_invalid_chars ($_);
306 my $name = encode_entities ($_);
307 my $value = $TimeSpans->{$_};
308 my $selected = ($value == $timespan_selection)
309 ? ' selected="selected"'
311 print qq# <option value="$value"$selected>$name</option>\n#;
315 <input type="hidden" name="action" value="show_selection" />
316 <input type="submit" name="ok_button" value="OK" />
322 sub action_list_hosts
327 my @hosts = get_all_hosts ();
331 my $url = encode_entities (script_name () . "?action=show_selection;hostname=$_");
332 next if contains_invalid_chars ($_);
333 my $name = encode_entities ($_);
334 print qq# <li><a href="$url">$name</a></li>\n#;
339 } # action_list_hosts
341 =head1 MODULE LOADING
343 This script makes use of the various B<Collectd::Graph::Type::*> modules. If a
344 file like C<foo.rrd> is encountered it tries to load the
345 B<Collectd::Graph::Type::Foo> module and, if that fails, falls back to the
346 B<Collectd::Graph::Type> base class.
348 If you want to create a specialized graph for a certain type, you have to
349 create a new module which inherits from the B<Collectd::Graph::Type> base
350 class. A description of provided (and used) methods can be found in the inline
351 documentation of the B<Collectd::Graph::Type> module.
353 There are other, more specialized, "abstract" classes that possibly better fit
354 your need. Unfortunately they are not yet documented.
358 =item B<Collectd::Graph::Type::GenericStacked>
360 Specialized class that groups files by their plugin instance and stacks them on
361 top of each other. Example types that inherit from this class are
362 B<Collectd::Graph::Type::Cpu> and B<Collectd::Graph::Type::Memory>.
364 =item B<Collectd::Graph::Type::GenericIO>
366 Specialized class for input/output graphs. This class can only handle files
367 with exactly two data sources, input and output. Example types that inherit
368 from this class are B<Collectd::Graph::Type::DiskOctets> and
369 B<Collectd::Graph::Type::IfOctets>.
375 sub action_show_selection
387 $all_files = get_selected_files ();
388 $timespan = get_timespan_selection ();
392 print "<pre>", Data::Dumper->Dump ([$all_files], ['all_files']), "</pre>\n";
395 # Send FLUSH command to the daemon if necessary and possible.
396 flush_files ($all_files,
397 begin => time () - $timespan,
399 addr => gc_get_scalar ('UnixSockAddr', undef),
400 interval => gc_get_scalar ('Interval', 10));
405 my $type = ucfirst (lc ($file->{'type'}));
407 $type =~ s/[^A-Za-z0-9_]//g;
408 $type =~ s/_([A-Za-z0-9])/\U$1\E/g;
410 if (!defined ($types->{$type}))
412 $types->{$type} = tl_load_type ($file->{'type'});
413 if (!$types->{$type})
415 warn ("tl_load_type (" . $file->{'type'} . ") failed");
420 $types->{$type}->addFiles ($file);
422 #print STDOUT Data::Dumper->Dump ([$types], ['types']);
424 print qq# <table>\n#;
425 for (sort (keys %$types))
429 if (!defined ($types->{$type}))
434 my $graphs_num = $types->{$type}->getGraphsNum ();
436 for (my $i = 0; $i < $graphs_num; $i++)
438 my $args = $types->{$type}->getGraphArgs ($i);
439 my $url = encode_entities ("graph.cgi?$args;begin=-$timespan");
440 my $id = sprintf ("graph%04i", $id_counter++);
443 print " <td rowspan=\"$graphs_num\">$type</td>\n" if ($i == 0);
446 <div class="graph_canvas">
447 <div class="graph_float">
448 <img id="${id}" class="graph_image"
451 <div class="controls zoom">
453 onclick="nav_move_earlier ('${id}');">←</div>
454 <div title="Zoom out"
455 onclick="nav_zoom_out ('${id}');">-</div>
457 onclick="nav_zoom_in ('${id}');">+</div>
459 onclick="nav_move_later ('${id}');">→</div>
461 <div class="controls preset">
462 <div title="Show current hour"
463 onclick="nav_time_reset ('${id}', 3600);">H</div>
464 <div title="Show current day"
465 onclick="nav_time_reset ('${id}', 86400);">D</div>
466 <div title="Show current week"
467 onclick="nav_time_reset ('${id}', 7 * 86400);">W</div>
468 <div title="Show current month"
469 onclick="nav_time_reset ('${id}', 31 * 86400);">M</div>
470 <div title="Show current year"
471 onclick="nav_time_reset ('${id}', 366 * 86400);">Y</div>
472 <div title="Set all images to this timespan"
473 onclick="nav_set_reference ('${id}');">!</div>
479 # print qq# <td><img src="$url" /></td>\n#;
492 L<Collectd::Graph::Type>
494 =head1 AUTHOR AND LICENSE
496 Copyright (c) 2008 by Florian Forster
497 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
498 General Public License, VersionE<nbsp>2 (GPLv2).
502 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :