3 # Copyright (C) 2008 Florian octo Forster <octo at verplant.org>
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation; only version 2 of the License is applicable.
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 use FindBin ('$RealBin');
25 use CGI::Carp ('fatalsToBrowser');
26 use HTML::Entities ('encode_entities');
30 use Collectd::Graph::Config (qw(gc_read_config gc_get_scalar));
31 use Collectd::Graph::TypeLoader (qw(tl_load_type));
32 use Collectd::Graph::Common (qw(get_files_from_directory get_all_hosts
33 get_timespan_selection get_selected_files get_host_selection
34 get_plugin_selection flush_files));
35 use Collectd::Graph::Type ();
37 our $Debug = param ('debug') ? 1 : 0;
48 my $action = param ('action') || 'list_hosts';
51 list_hosts => \&action_list_hosts,
52 show_selection => \&action_show_selection
55 if (!exists ($Actions{$action}))
57 print STDERR "No such action: $action\n";
61 gc_read_config ("$RealBin/../etc/collection.conf");
63 $Actions{$action}->();
70 if (!defined $ENV{'HTTP_ACCEPT'})
75 for (split (',', $ENV{'HTTP_ACCEPT'}))
80 if ($type =~ m#^([^;]+);q=([0-9\.]+)$#)
88 if (!defined ($types{'application/xhtml+xml'}))
92 elsif (!defined ($types{'text/html'}))
96 elsif ($types{'application/xhtml+xml'} < $types{'text/html'})
109 return if ($html_started);
116 $timespan = get_timespan_selection ();
117 $begin = $end - $timespan;
119 if (can_handle_xhtml ())
122 Content-Type: application/xhtml+xml; charset=UTF-8
124 <?xml version="1.0" encoding="UTF-8"?>
125 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
126 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
127 <html xmlns="http://www.w3.org/1999/xhtml"
128 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
129 xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
136 Content-Type: text/html; charset=UTF-8
138 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
139 "http://www.w3.org/TR/html4/strict.dtd">
145 <title>collection.cgi, Version 3</title>
146 <link rel="icon" href="../share/shortcut-icon.png" type="image/png" />
147 <link rel="stylesheet" href="../share/style.css" type="text/css" />
148 <script type="text/javascript" src="../share/navigate.js" />
150 <body onload="nav_init ($begin, $end);">
165 my $timespan_selection = get_timespan_selection ();
166 my $host_selection = get_host_selection ();
167 my $plugin_selection = get_plugin_selection ();
170 <form action="${\script_name ()}" method="get">
172 <legend>Data selection</legend>
173 <select name="hostname" multiple="multiple" size="15">
175 for (sort (keys %$host_selection))
177 my $host = encode_entities ($_);
178 my $selected = $host_selection->{$_}
179 ? ' selected="selected"'
181 print qq# <option value="$host"$selected>$host</option>\n#;
185 <select name="plugin" multiple="multiple" size="15">
187 for (sort (keys %$plugin_selection))
189 my $plugin = encode_entities ($_);
190 my $selected = $plugin_selection->{$_}
191 ? ' selected="selected"'
193 print qq# <option value="$plugin"$selected>$plugin</option>\n#;
197 <select name="timespan">
199 for (sort { $TimeSpans->{$a} <=> $TimeSpans->{$b} } (keys (%$TimeSpans)))
201 my $name = encode_entities ($_);
202 my $value = $TimeSpans->{$_};
203 my $selected = ($value == $timespan_selection)
204 ? ' selected="selected"'
206 print qq# <option value="$value"$selected>$name</option>\n#;
210 <input type="hidden" name="action" value="show_selection" />
211 <input type="submit" name="ok_button" value="OK" />
217 sub action_list_hosts
222 my @hosts = get_all_hosts ();
226 my $url = encode_entities (script_name () . "?action=show_selection;hostname=$_");
227 my $name = encode_entities ($_);
228 print qq# <li><a href="$url">$name</a></li>\n#;
233 } # action_list_hosts
235 =head1 MODULE LOADING
237 This script makes use of the various B<Collectd::Graph::Type::*> modules. If a
238 file like C<foo.rrd> is encountered it tries to load the
239 B<Collectd::Graph::Type::Foo> module and, if that fails, falls back to the
240 B<Collectd::Graph::Type> base class.
242 If you want to create a specialized graph for a certain type, you have to
243 create a new module which inherits from the B<Collectd::Graph::Type> base
244 class. A description of provided (and used) methods can be found in the inline
245 documentation of the B<Collectd::Graph::Type> module.
247 There are other, more specialized, "abstract" classes that possibly better fit
248 your need. Unfortunately they are not yet documented.
252 =item B<Collectd::Graph::Type::GenericStacked>
254 Specialized class that groups files by their plugin instance and stacks them on
255 top of each other. Example types that inherit from this class are
256 B<Collectd::Graph::Type::Cpu> and B<Collectd::Graph::Type::Memory>.
258 =item B<Collectd::Graph::Type::GenericIO>
260 Specialized class for input/output graphs. This class can only handle files
261 with exactly two data sources, input and output. Example types that inherit
262 from this class are B<Collectd::Graph::Type::DiskOctets> and
263 B<Collectd::Graph::Type::IfOctets>.
269 sub action_show_selection
281 $all_files = get_selected_files ();
282 $timespan = get_timespan_selection ();
286 print "<pre>", Data::Dumper->Dump ([$all_files], ['all_files']), "</pre>\n";
289 # Send FLUSH command to the daemon if necessary and possible.
290 flush_files ($all_files,
291 begin => time () - $timespan,
293 addr => gc_get_scalar ('UnixSockAddr', undef),
294 interval => gc_get_scalar ('Interval', 10));
299 my $type = ucfirst (lc ($file->{'type'}));
301 $type =~ s/[^A-Za-z_]//g;
302 $type =~ s/_([A-Za-z])/\U$1\E/g;
304 if (!defined ($types->{$type}))
306 $types->{$type} = tl_load_type ($file->{'type'});
307 if (!$types->{$type})
309 cluck ("tl_load_type (" . $file->{'type'} . ") failed");
314 $types->{$type}->addFiles ($file);
316 #print STDOUT Data::Dumper->Dump ([$types], ['types']);
318 print qq# <table>\n#;
319 for (sort (keys %$types))
322 my $graphs_num = $types->{$type}->getGraphsNum ();
324 for (my $i = 0; $i < $graphs_num; $i++)
326 my $args = $types->{$type}->getGraphArgs ($i);
327 my $url = encode_entities ("graph.cgi?$args;begin=-$timespan");
328 my $id = sprintf ("graph%04i", $id_counter++);
331 print " <td rowspan=\"$graphs_num\">$type</td>\n" if ($i == 0);
334 <div class="graph_canvas">
335 <div class="graph_float">
336 <img id="${id}" class="graph_image"
339 <div class="controls zoom">
341 onclick="nav_move_earlier ('${id}');">←</div>
342 <div title="Zoom out"
343 onclick="nav_zoom_out ('${id}');">-</div>
345 onclick="nav_zoom_in ('${id}');">+</div>
347 onclick="nav_move_later ('${id}');">→</div>
349 <div class="controls preset">
350 <div title="Show current hour"
351 onclick="nav_time_reset ('${id}', 3600);">H</div>
352 <div title="Show current day"
353 onclick="nav_time_reset ('${id}', 86400);">D</div>
354 <div title="Show current week"
355 onclick="nav_time_reset ('${id}', 7 * 86400);">W</div>
356 <div title="Show current month"
357 onclick="nav_time_reset ('${id}', 31 * 86400);">M</div>
358 <div title="Show current year"
359 onclick="nav_time_reset ('${id}', 366 * 86400);">Y</div>
360 <div title="Set all images to this timespan"
361 onclick="nav_set_reference ('${id}');">!</div>
367 # print qq# <td><img src="$url" /></td>\n#;
378 L<Collectd::Graph::Type>
380 =head1 AUTHOR AND LICENSE
382 Copyright (c) 2008 by Florian Forster
383 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
384 General Public License, VersionE<nbsp>2 (GPLv2).
388 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :