Merge branch 'collectd-4.5' into collectd-4.6
[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 use JSON ('objToJson');
28
29 use Data::Dumper;
30
31 use Collectd::Graph::Config (qw(gc_read_config));
32 use Collectd::Graph::TypeLoader (qw(tl_load_type));
33 use Collectd::Graph::Common (qw(get_all_hosts get_files_for_host type_to_module_name));
34 use Collectd::Graph::Type ();
35
36 our $Debug = param ('debug') ? 1 : 0;
37 our $ServerName = 'collect.noris.net';
38
39 gc_read_config ("$RealBin/../etc/collection.conf");
40
41 if ($Debug)
42 {
43   print "Content-Type: text/plain; charset=utf-8\n\n";
44 }
45 else
46 {
47   print "Content-Type: application/json; charset=utf-8\n\n";
48 }
49
50 my $obj = {};
51 my @hosts = get_all_hosts ();
52 for (my $i = 0; $i < @hosts; $i++)
53 {
54   my $host_obj = {};
55   my $host = $hosts[$i];
56   my $files = get_files_for_host ($host);
57   my %graphs = ();
58   my @graphs = ();
59
60   # Group files by graphs
61   for (@$files)
62   {
63     my $file = $_;
64     my $type = $file->{'type'};
65
66     # Create a new graph object if this is the first of this type.
67     if (!defined ($graphs{$type}))
68     {
69       $graphs{$type} = tl_load_type ($file->{'type'});
70       if (!$graphs{$type})
71       {
72         cluck ("tl_load_type (" . $file->{'type'} . ") failed");
73         next;
74       }
75     }
76
77     $graphs{$type}->addFiles ($file);
78   } # for (@$files)
79
80   #print qq(  ") . objToJson ({ foo => 123 }) . qq(":\n  {\n);
81
82   @graphs = keys %graphs;
83   for (my $j = 0; $j < @graphs; $j++)
84   {
85     my $type = $graphs[$j];
86     my $graphs_num = $graphs{$type}->getGraphsNum ();
87
88     if (!defined ($host_obj->{$type}))
89     {
90       $host_obj->{$type} = [];
91     }
92
93     for (my $k = 0; $k < $graphs_num; $k++)
94     {
95       my $args = $graphs{$type}->getGraphArgs ($k);
96       my $url = "http://$ServerName/cgi-bin/collection3/bin/graph.cgi?" . $args;
97       push (@{$host_obj->{$type}}, $url);
98     }
99   } # for (keys %graphs)
100
101   $obj->{$host} = $host_obj;
102 } # for (my $i = 0; $i < @hosts; $i++)
103
104 print STDOUT objToJson ($obj, { pretty => 1, indent => 2 });
105
106 exit (0);
107
108 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :