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