1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#!@PERL@ -w
# Copyright (C) 2010 Stanislav Sinyagin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
# Stanislav Sinyagin <ssinyagin@k-open.com>
use strict;
use warnings;
BEGIN { require '@torrus_config_pl@'; }
use Getopt::Long;
use JSON;
use File::Copy;
use Torrus::ConfigTree;
use Torrus::SiteConfig;
use Torrus::Renderer;
use Torrus::Log;
exit(1) unless Torrus::SiteConfig::verify();
my %commands =
('info' => 1,
'search' => 1,
'render' => 1);
my $tree;
my $cmd;
my $nodeid;
my $print_details;
my $search_prefix;
my $search_substring;
my $render_view;
my $render_out;
my $help_needed;
my $ok = GetOptions('tree=s' => \$tree,
'cmd=s' => \$cmd,
'nodeid=s' => \$nodeid,
'details' => \$print_details,
'prefix=s' => \$search_prefix,
'substring=s' => \$search_substring,
'view=s' => \$render_view,
'out=s' => \$render_out,
'help' => \$help_needed);
if( not $ok or
not $tree or not $cmd or not $commands{$cmd} or
( ($cmd eq 'info' or $cmd eq 'render') and not $nodeid ) or
( $cmd eq 'search' and not ($search_prefix or $search_substring) ) or
$help_needed or scalar(@ARGV) > 0 )
{
print STDERR "Usage: $0 --tree=NAME --cmd=CMD [options...]\n",
"Options:\n",
" --tree=NAME tree name\n",
" --cmd=CMD Command (info|search|render)\n",
" --nodeid=NODEID nodeid (mandatory for info and render)\n",
" --details print nodeid details (valid with info and search)\n",
" --prefix=STR search prefix\n",
" --substring=STR search substring\n",
" --view=VIEW render view (optional)\n",
" --out=FILE render output\n",
" --help this help message\n";
exit 1;
}
if( not Torrus::SiteConfig::treeExists( $tree ) )
{
Error('Tree ' . $tree . ' does not exist');
exit 1;
}
{
my $config_tree = new Torrus::ConfigTree( -TreeName => $tree );
if( not defined($config_tree) )
{
Error("Configuration is not ready");
exit 1;
}
if( $cmd eq 'info' or $cmd eq 'render' )
{
my $token = $config_tree->getNodeByNodeid($nodeid);
if( not defined( $token ) )
{
Error('nodeid not found: ' . $nodeid);
exit(1);
}
if( $cmd eq 'info' )
{
print_nodeid($config_tree, [$token], $print_details);
}
else
{
render_node($config_tree, $token, $render_view, $render_out);
}
}
elsif( $cmd eq 'search' )
{
my $results;
if( defined($search_prefix) )
{
$results = $config_tree->searchNodeidPrefix($search_prefix);
}
else
{
$results = $config_tree->searchNodeidSubstring($search_substring);
}
if( defined( $results ) and scalar(@{$results}) > 0 )
{
my $tokens = [];
# results are pairs [nodeid,token]
foreach my $res ( @{$results} )
{
push(@{$tokens}, $res->[1]);
}
print_nodeid($config_tree, $tokens, $print_details);
}
else
{
print STDERR "Nothing found\n";
exit(1);
}
}
else
{
printf STDERR ("Unknown command: %s\n", $cmd);
exit(1);
}
}
exit(0);
sub print_nodeid
{
my $config_tree = shift;
my $tokens = shift;
my $details = shift;
my $json = new JSON;
$json->pretty();
$json->canonical();
my @all;
foreach my $token ( @{$tokens} )
{
my $info = {
'nodeid' => $config_tree->getNodeParam($token, 'nodeid', 1),
};
if( $details )
{
$info->{'path'} = $config_tree->path($token);
$info->{'is_leaf'} = $config_tree->isLeaf($token) ? 1:0;
$info->{'tree'} = $config_tree->treeName();
if( $info->{'is_leaf'} )
{
my $dsType = $config_tree->getNodeParam( $token, 'ds-type' );
$info->{'param:ds-type'} = $dsType;
if( $dsType eq 'collector' )
{
foreach my $param
('collector-type', 'collector-period',
'storage-type', 'data-file', 'data-dir', 'rrd-ds',
'ext-service-id',
'snmp-host', 'domain-name', 'snmp-object')
{
my $val = $config_tree->getNodeParam($token, $param);
if( defined( $val ) )
{
$info->{'param:' . $param} = $val;
}
}
}
}
}
push(@all, $info);
}
print $json->encode(\@all);
return;
}
sub render_node
{
my $config_tree = shift;
my $token = shift;
my $view = shift;
my $out = shift;
my $r = new Torrus::Renderer;
my($fname, $mimetype) = $r->render($config_tree, $token, $view);
if( defined($out) )
{
if( not copy( $fname, $out ) )
{
printf STDERR ("Failed to write to %s: %s\n", $out, $!);
exit(1);
}
$fname = $out;
}
my $json = new JSON;
$json->pretty();
print $json->encode({'Content-type' => $mimetype,
'Filename' => $fname});
return;
}
# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End:
|