2 # Copyright (C) 2010 Stanislav Sinyagin
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 # $Id: nodeid.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 BEGIN { require '@torrus_config_pl@'; }
28 use Torrus::ConfigTree;
29 use Torrus::SiteConfig;
33 exit(1) if not Torrus::SiteConfig::verify();
38 'search' => \&do_search,
39 'render' => \&do_render);
57 my $ok = GetOptions('tree=s' => \$tree,
59 'nodeid=s' => \$nodeid,
60 'details' => \$print_details,
61 'prefix=s' => \$search_prefix,
62 'substring=s' => \$search_substring,
63 'view=s' => \$render_view,
64 'out=s' => \$render_out,
65 'help' => \$help_needed);
68 not $tree or not $cmd or not $commands{$cmd} or
69 ( ($cmd eq 'info' or $cmd eq 'render') and not $nodeid ) or
70 ( $cmd eq 'search' and not ($search_prefix or $search_substring) ) or
71 $help_needed or scalar(@ARGV) > 0 )
73 print STDERR "Usage: $0 --tree=NAME --cmd=CMD [options...]\n",
75 " --tree=NAME tree name\n",
76 " --cmd=CMD Command (info|search|render)\n",
77 " --nodeid=NODEID nodeid (mandatory for info and render)\n",
78 " --details print nodeid details (valid with info and search)\n",
79 " --prefix=STR search prefix\n",
80 " --substring=STR search substring\n",
81 " --view=VIEW render view (optional)\n",
82 " --out=FILE render output\n",
83 " --help this help message\n";
88 if( not Torrus::SiteConfig::treeExists( $tree ) )
90 Error('Tree ' . $tree . ' does not exist');
95 &Torrus::DB::setSafeSignalHandlers();
98 my $config_tree = new Torrus::ConfigTree( -TreeName => $tree );
99 if( not defined($config_tree) )
101 Error("Configuration is not ready");
105 if( $cmd eq 'info' or $cmd eq 'render' )
107 my $token = $config_tree->getNodeByNodeid($nodeid);
108 if( not defined( $token ) )
110 Error('nodeid not found: ' . $nodeid);
116 print_nodeid($config_tree, [$token], $print_details);
120 render_node($config_tree, $token, $render_view, $render_out);
123 elsif( $cmd eq 'search' )
126 if( defined($search_prefix) )
128 $results = $config_tree->searchNodeidPrefix($search_prefix);
132 $results = $config_tree->searchNodeidSubstring($search_substring);
135 if( defined( $results ) and scalar(@{$results}) > 0 )
138 # results are pairs [nodeid,token]
139 foreach my $res ( @{$results} )
141 push(@{$tokens}, $res->[1]);
143 print_nodeid($config_tree, $tokens, $print_details);
147 print STDERR "Nothing found\n";
153 printf STDERR ("Unknown command: %s\n", $cmd);
163 my $config_tree = shift;
173 foreach my $token ( @{$tokens} )
176 'nodeid' => $config_tree->getNodeParam($token, 'nodeid', 1),
181 $info->{'path'} = $config_tree->path($token);
182 $info->{'is_leaf'} = $config_tree->isLeaf($token) ? 1:0;
183 $info->{'tree'} = $config_tree->treeName();
184 if( $info->{'is_leaf'} )
186 my $dsType = $config_tree->getNodeParam( $token, 'ds-type' );
187 $info->{'param:ds-type'} = $dsType;
188 if( $dsType eq 'collector' )
191 ('collector-type', 'collector-period',
192 'storage-type', 'data-file', 'data-dir', 'rrd-ds',
194 'snmp-host', 'domain-name', 'snmp-object')
196 my $val = $config_tree->getNodeParam($token, $param);
198 if( defined( $val ) )
200 $info->{'param:' . $param} = $val;
210 print $json->encode(\@all);
217 my $config_tree = shift;
222 my $r = new Torrus::Renderer;
224 my($fname, $mimetype) = $r->render($config_tree, $token, $view);
228 if( not copy( $fname, $out ) )
230 printf STDERR ("Failed to write to %s: %s\n", $out, $!);
240 print $json->encode({'Content-type' => $mimetype,
241 'Filename' => $fname});
250 # indent-tabs-mode: nil
251 # perl-indent-level: 4