contrib/collection3: Add an basic, extensible, modular graphing front-end.
[collectd.git] / contrib / collection3 / bin / index.cgi
1 #!/usr/bin/perl
2
3 # Copyright (C) 2008  Florian octo Forster <octo at verplant.org>
4 #
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.
8 #
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
12 # details.
13 #
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.
17
18 use strict;
19 use warnings;
20 use lib ('../lib');
21 use utf8;
22
23 use FindBin ('$RealBin');
24 use CGI (':cgi');
25 use CGI::Carp ('fatalsToBrowser');
26 use HTML::Entities ('encode_entities');
27
28 use Data::Dumper;
29
30 use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type));
31 use Collectd::Graph::Common (qw(get_files_from_directory get_all_hosts
32       get_timespan_selection get_selected_files get_host_selection
33       get_plugin_selection));
34 use Collectd::Graph::Type ();
35
36 our $Debug = param ('debug') ? 1 : 0;
37
38 our $TimeSpans =
39 {
40   Hour  =>        3600,
41   Day   =>       86400,
42   Week  =>   7 * 86400,
43   Month =>  31 * 86400,
44   Year  => 366 * 86400
45 };
46
47 my $action = param ('action') || 'list_hosts';
48 our %Actions =
49 (
50   list_hosts => \&action_list_hosts,
51   show_selection => \&action_show_selection
52 );
53
54 if (!exists ($Actions{$action}))
55 {
56   print STDERR "No such action: $action\n";
57   exit 1;
58 }
59
60 tl_read_config ("$RealBin/../etc/collection3.conf");
61
62 $Actions{$action}->();
63 exit (0);
64
65 sub can_handle_xhtml
66 {
67   my %types = ();
68
69   if (!defined $ENV{'HTTP_ACCEPT'})
70   {
71     return;
72   }
73
74   for (split (',', $ENV{'HTTP_ACCEPT'}))
75   {
76     my $type = lc ($_);
77     my $q = 1.0;
78
79     if ($type =~ m#^([^;]+);q=([0-9\.]+)$#)
80     {
81       $type = $1;
82       $q = 0.0 + $2;
83     }
84     $types{$type} = $q;
85   }
86
87   if (!defined ($types{'application/xhtml+xml'}))
88   {
89     return;
90   }
91   elsif (!defined ($types{'text/html'}))
92   {
93     return (1);
94   }
95   elsif ($types{'application/xhtml+xml'} < $types{'text/html'})
96   {
97     return;
98   }
99   else
100   {
101     return (1);
102   }
103 } # can_handle_xhtml
104
105 {my $html_started;
106 sub start_html
107 {
108   return if ($html_started);
109
110   if (can_handle_xhtml ())
111   {
112     print <<HTML;
113 Content-Type: application/xhtml+xml; charset=UTF-8
114
115 <?xml version="1.0" encoding="UTF-8"?>
116 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
117     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
118 <html xmlns="http://www.w3.org/1999/xhtml"
119     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
120     xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
121     xml:lang="en">
122 HTML
123   }
124   else
125   {
126     print <<HTML;
127 Content-Type: text/html; charset=UTF-8
128
129 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
130     "http://www.w3.org/TR/html4/strict.dtd">
131 <html>
132 HTML
133   }
134   print <<HTML;
135   <head>
136     <title>collection.cgi, Version 3</title>
137     <link rel="icon" href="../share/shortcut-icon.png" type="image/png" />
138     <link rel="stylesheet" href="../share/style.css" type="text/css" />
139   </head>
140   <body>
141 HTML
142   $html_started = 1;
143 }}
144
145 sub end_html
146 {
147   print <<HTML;
148   </body>
149 </html>
150 HTML
151 }
152
153 sub show_selector
154 {
155   my $timespan_selection = get_timespan_selection ();
156   my $host_selection = get_host_selection ();
157   my $plugin_selection = get_plugin_selection ();
158
159   print <<HTML;
160     <form action="${\script_name ()}" method="get">
161       <fieldset>
162         <legend>Data selection</legend>
163         <select name="hostname" multiple="multiple" size="15">
164 HTML
165   for (sort (keys %$host_selection))
166   {
167     my $host = encode_entities ($_);
168     my $selected = $host_selection->{$_}
169     ? ' selected="selected"'
170     : '';
171     print qq#          <option value="$host"$selected>$host</option>\n#;
172   }
173   print <<HTML;
174         </select>
175         <select name="plugin" multiple="multiple" size="15">
176 HTML
177   for (sort (keys %$plugin_selection))
178   {
179     my $plugin = encode_entities ($_);
180     my $selected = $plugin_selection->{$_}
181     ? ' selected="selected"'
182     : '';
183     print qq#          <option value="$plugin"$selected>$plugin</option>\n#;
184   }
185   print <<HTML;
186         </select>
187         <select name="timespan">
188 HTML
189   for (sort { $TimeSpans->{$a} <=> $TimeSpans->{$b} } (keys (%$TimeSpans)))
190   {
191     my $name = encode_entities ($_);
192     my $value = $TimeSpans->{$_};
193     my $selected = ($value == $timespan_selection)
194     ? ' selected="selected"'
195     : '';
196     print qq#          <option value="$value"$selected>$name</option>\n#;
197   }
198   print <<HTML;
199         </select>
200         <input type="hidden" name="action" value="show_selection" />
201         <input type="submit" name="ok_button" value="OK" />
202       </fieldset>
203     </form>
204 HTML
205 } # show_selector
206
207 sub action_list_hosts
208 {
209   start_html ();
210   show_selector ();
211
212   my @hosts = get_all_hosts ();
213   print "    <ul>\n";
214   for (sort @hosts)
215   {
216     my $url = encode_entities (script_name () . "?action=show_selection;hostname=$_");
217     my $name = encode_entities ($_);
218     print qq#      <li><a href="$url">$name</a></li>\n#;
219   }
220   print "    </ul>\n";
221
222   end_html ();
223 } # action_list_hosts
224
225 =head1 MODULE LOADING
226
227 This script makes use of the various B<Collectd::Graph::Type::*> modules. If a
228 file like C<foo.rrd> is encountered it tries to load the
229 B<Collectd::Graph::Type::Foo> module and, if that fails, falls back to the
230 B<Collectd::Graph::Type> base class.
231
232 If you want to create a specialized graph for a certain type, you have to
233 create a new module which inherits from the B<Collectd::Graph::Type> base
234 class. A description of provided (and used) methods can be found in the inline
235 documentation of the B<Collectd::Graph::Type> module.
236
237 There are other, more specialized, "abstract" classes that possibly better fit
238 your need. Unfortunately they are not yet documented.
239
240 =over 4
241
242 =item B<Collectd::Graph::Type::GenericStacked>
243
244 Specialized class that groups files by their plugin instance and stacks them on
245 top of each other. Example types that inherit from this class are
246 B<Collectd::Graph::Type::Cpu> and B<Collectd::Graph::Type::Memory>.
247
248 =item B<Collectd::Graph::Type::GenericIO>
249
250 Specialized class for input/output graphs. This class can only handle files
251 with exactly two data sources, input and output. Example types that inherit
252 from this class are B<Collectd::Graph::Type::DiskOctets> and
253 B<Collectd::Graph::Type::IfOctets>.
254
255 =back
256
257 =cut
258
259 sub action_show_selection
260 {
261   start_html ();
262   show_selector ();
263
264   my $ident = {};
265
266   my $all_files;
267   my $types = {};
268
269   $all_files = get_selected_files ();
270
271   if ($Debug)
272   {
273     print "<pre>", Data::Dumper->Dump ([$all_files], ['all_files']), "</pre>\n";
274   }
275
276   for (@$all_files)
277   {
278     my $file = $_;
279     my $type = ucfirst (lc ($file->{'type'}));
280
281     $type =~ s/[^A-Za-z_]//g;
282     $type =~ s/_([A-Za-z])/\U$1\E/g;
283
284     if (!defined ($types->{$type}))
285     {
286       $types->{$type} = tl_load_type ($file->{'type'});
287       if (!$types->{$type})
288       {
289         cluck ("tl_load_type (" . $file->{'type'} . ") failed");
290         next;
291       }
292     }
293
294     $types->{$type}->addFiles ($file);
295   }
296 #print STDOUT Data::Dumper->Dump ([$types], ['types']);
297
298   print qq#    <table>\n#;
299   for (sort (keys %$types))
300   {
301     my $type = $_;
302     my $graphs_num = $types->{$type}->getGraphsNum ();
303
304     my $timespan = get_timespan_selection ();
305
306     for (my $i = 0; $i < $graphs_num; $i++)
307     {
308       my $args = $types->{$type}->getGraphArgs ($i);
309       my $url = encode_entities ("graph.cgi?$args;begin=-$timespan");
310
311       print "      <tr>\n";
312       print "        <td rowspan=\"$graphs_num\">$type</td>\n" if ($i == 0);
313
314       print qq#        <td><img src="$url" /></td>\n#;
315       print "      </tr>\n";
316     }
317   }
318
319   print "    </table>\n";
320   end_html ();
321 }
322
323 =head1 SEE ALSO
324
325 L<Collectd::Graph::Type>
326
327 =head1 AUTHOR AND LICENSE
328
329 Copyright (c) 2008 by Florian Forster
330 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>. Licensed under the terms of the GNU
331 General Public License, VersionE<nbsp>2 (GPLv2).
332
333 =cut
334
335 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :