X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=contrib%2Fcollection3%2Fbin%2Fjson.cgi;h=99e27034193c3fc1aeda2830324e7f275967c4a5;hb=ff9e11ade5cfabf32c63fb19fc76cbbc4186bc5b;hp=0dceb62870583f3d6f2afc4fc2dc999cc07cdb63;hpb=41f141639046a261063cf346fe7f89cd4cb9cc59;p=collectd.git diff --git a/contrib/collection3/bin/json.cgi b/contrib/collection3/bin/json.cgi index 0dceb628..99e27034 100755 --- a/contrib/collection3/bin/json.cgi +++ b/contrib/collection3/bin/json.cgi @@ -24,16 +24,19 @@ use FindBin ('$RealBin'); use CGI (':cgi'); use CGI::Carp ('fatalsToBrowser'); use URI::Escape ('uri_escape'); +use JSON ('objToJson'); use Data::Dumper; -use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type)); +use Collectd::Graph::Config (qw(gc_read_config)); +use Collectd::Graph::TypeLoader (qw(tl_load_type)); use Collectd::Graph::Common (qw(get_all_hosts get_files_for_host type_to_module_name)); use Collectd::Graph::Type (); our $Debug = param ('debug') ? 1 : 0; +our $ServerName = 'collect.noris.net'; -tl_read_config ("$RealBin/../etc/collection3.conf"); +gc_read_config ("$RealBin/../etc/collection.conf"); if ($Debug) { @@ -44,11 +47,11 @@ else print "Content-Type: application/json; charset=utf-8\n\n"; } -print "{\n"; - +my $obj = {}; my @hosts = get_all_hosts (); for (my $i = 0; $i < @hosts; $i++) { + my $host_obj = {}; my $host = $hosts[$i]; my $files = get_files_for_host ($host); my %graphs = (); @@ -74,46 +77,31 @@ for (my $i = 0; $i < @hosts; $i++) $graphs{$type}->addFiles ($file); } # for (@$files) - print qq( "$host":\n {\n); + #print qq( ") . objToJson ({ foo => 123 }) . qq(":\n {\n); @graphs = keys %graphs; for (my $j = 0; $j < @graphs; $j++) { my $type = $graphs[$j]; my $graphs_num = $graphs{$type}->getGraphsNum (); - my @args = (); - for (my $k = 0; $k < $graphs_num; $k++) + if (!defined ($host_obj->{$type})) { - my $args = $graphs{$type}->getGraphArgs ($k); - my $url = 'http://' . $ENV{'SERVER_NAME'} . "/cgi-bin/graph.cgi?" . $args; - push (@args, $url); + $host_obj->{$type} = []; } - print qq( "$type": [ ) - . join (', ', map { qq("$_") } (@args)); - - if ($j == (@graphs - 1)) - { - print qq( ]\n); - } - else + for (my $k = 0; $k < $graphs_num; $k++) { - print qq( ],\n); + my $args = $graphs{$type}->getGraphArgs ($k); + my $url = "http://$ServerName/cgi-bin/collection3/bin/graph.cgi?" . $args; + push (@{$host_obj->{$type}}, $url); } } # for (keys %graphs) - if ($i == (@hosts - 1)) - { - print qq( }\n); - } - else - { - print qq( },\n\n); - } + $obj->{$host} = $host_obj; } # for (my $i = 0; $i < @hosts; $i++) -print "}\n"; +print STDOUT objToJson ($obj, { pretty => 1, indent => 2 }); exit (0);