contrib/collection3: Add an basic, extensible, modular graphing front-end.
[collectd.git] / contrib / collection3 / bin / json.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 URI::Escape ('uri_escape');
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_all_hosts get_files_for_host type_to_module_name));
32 use Collectd::Graph::Type ();
33
34 our $Debug = param ('debug') ? 1 : 0;
35
36 tl_read_config ("$RealBin/../etc/collection3.conf");
37
38 if ($Debug)
39 {
40   print "Content-Type: text/plain; charset=utf-8\n\n";
41 }
42 else
43 {
44   print "Content-Type: application/json; charset=utf-8\n\n";
45 }
46
47 print "{\n";
48
49 my @hosts = get_all_hosts ();
50 for (my $i = 0; $i < @hosts; $i++)
51 {
52   my $host = $hosts[$i];
53   my $files = get_files_for_host ($host);
54   my %graphs = ();
55   my @graphs = ();
56
57   # Group files by graphs
58   for (@$files)
59   {
60     my $file = $_;
61     my $type = $file->{'type'};
62
63     # Create a new graph object if this is the first of this type.
64     if (!defined ($graphs{$type}))
65     {
66       $graphs{$type} = tl_load_type ($file->{'type'});
67       if (!$graphs{$type})
68       {
69         cluck ("tl_load_type (" . $file->{'type'} . ") failed");
70         next;
71       }
72     }
73
74     $graphs{$type}->addFiles ($file);
75   } # for (@$files)
76
77   print qq(  "$host":\n  {\n);
78
79   @graphs = keys %graphs;
80   for (my $j = 0; $j < @graphs; $j++)
81   {
82     my $type = $graphs[$j];
83     my $graphs_num = $graphs{$type}->getGraphsNum ();
84     my @args = ();
85
86     for (my $k = 0; $k < $graphs_num; $k++)
87     {
88       my $args = $graphs{$type}->getGraphArgs ($k);
89       my $url = 'http://' . $ENV{'SERVER_NAME'} . "/cgi-bin/graph.cgi?" . $args;
90       push (@args, $url);
91     }
92
93     print qq(    "$type": [ )
94     . join (', ', map { qq("$_") } (@args));
95
96     if ($j == (@graphs - 1))
97     {
98       print qq( ]\n);
99     }
100     else
101     {
102       print qq( ],\n);
103     }
104   } # for (keys %graphs)
105
106   if ($i == (@hosts - 1))
107   {
108     print qq(  }\n);
109   }
110   else
111   {
112     print qq(  },\n\n);
113   }
114 } # for (my $i = 0; $i < @hosts; $i++)
115
116 print "}\n";
117
118 exit (0);
119
120 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :