~ubuntu-branches/ubuntu/wily/torrus/wily-proposed

« back to all changes in this revision

Viewing changes to bin/nodeid.in

  • Committer: Package Import Robot
  • Author(s): Marc Haber
  • Date: 2011-11-06 17:15:40 UTC
  • mto: (6.1.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20111106171540-myc0auwqqio8bmhl
Tags: upstream-2.01
ImportĀ upstreamĀ versionĀ 2.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@PERL@ -w
 
2
#  Copyright (C) 2010  Stanislav Sinyagin
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
17
 
 
18
# $Id$
 
19
# Stanislav Sinyagin <ssinyagin@yahoo.com>
 
20
 
 
21
BEGIN { require '@torrus_config_pl@'; }
 
22
 
 
23
use strict;
 
24
use Getopt::Long;
 
25
use JSON;
 
26
use File::Copy;
 
27
    
 
28
use Torrus::ConfigTree;
 
29
use Torrus::SiteConfig;
 
30
use Torrus::Renderer;
 
31
use Torrus::Log;
 
32
 
 
33
exit(1) if not Torrus::SiteConfig::verify();
 
34
 
 
35
 
 
36
my %commands =
 
37
    ('info'   => \&do_info,
 
38
     'search' => \&do_search,
 
39
     'render' => \&do_render);
 
40
 
 
41
 
 
42
 
 
43
my $tree;
 
44
my $cmd;
 
45
 
 
46
my $nodeid;
 
47
my $print_details;
 
48
my $search_prefix;
 
49
my $search_substring;
 
50
my $render_view;
 
51
my $render_out;
 
52
 
 
53
 
 
54
my $help_needed;
 
55
 
 
56
 
 
57
my $ok = GetOptions('tree=s'      => \$tree,
 
58
                    'cmd=s'       => \$cmd,
 
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);
 
66
 
 
67
if( not $ok or
 
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 )
 
72
{
 
73
    print STDERR "Usage: $0 --tree=NAME --cmd=CMD [options...]\n",
 
74
    "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";
 
84
    exit 1;
 
85
}
 
86
 
 
87
 
 
88
if( not Torrus::SiteConfig::treeExists( $tree ) )
 
89
{
 
90
    Error('Tree ' . $tree . ' does not exist');
 
91
    exit 1;
 
92
}
 
93
 
 
94
 
 
95
&Torrus::DB::setSafeSignalHandlers();
 
96
 
 
97
{
 
98
    my $config_tree = new Torrus::ConfigTree( -TreeName => $tree );
 
99
    if( not defined($config_tree) )
 
100
    {
 
101
        Error("Configuration is not ready");
 
102
        exit 1;
 
103
    }
 
104
        
 
105
    if( $cmd eq 'info' or $cmd eq 'render' )
 
106
    {
 
107
        my $token = $config_tree->getNodeByNodeid($nodeid);
 
108
        if( not defined( $token ) )
 
109
        {
 
110
            Error('nodeid not found: ' . $nodeid);
 
111
            exit(1);
 
112
        }
 
113
 
 
114
        if( $cmd eq 'info' )
 
115
        {
 
116
            print_nodeid($config_tree, [$token], $print_details);
 
117
        }
 
118
        else
 
119
        {
 
120
            render_node($config_tree, $token, $render_view, $render_out);
 
121
        }
 
122
    }
 
123
    elsif( $cmd eq 'search' )
 
124
    {
 
125
        my $results;
 
126
        if( defined($search_prefix) )
 
127
        {
 
128
            $results = $config_tree->searchNodeidPrefix($search_prefix);
 
129
        }
 
130
        else
 
131
        {
 
132
            $results = $config_tree->searchNodeidSubstring($search_substring);
 
133
        }
 
134
 
 
135
        if( defined( $results ) and scalar(@{$results}) > 0 )
 
136
        {
 
137
            my $tokens = [];
 
138
            # results are pairs [nodeid,token]
 
139
            foreach my $res ( @{$results} )
 
140
            {
 
141
                push(@{$tokens}, $res->[1]);
 
142
            }
 
143
            print_nodeid($config_tree, $tokens, $print_details);
 
144
        }
 
145
        else
 
146
        {
 
147
            print STDERR "Nothing found\n";
 
148
            exit(1);
 
149
        }
 
150
    }
 
151
    else
 
152
    {
 
153
        printf STDERR ("Unknown command: %s\n", $cmd);
 
154
        exit(1);
 
155
    }
 
156
}
 
157
 
 
158
exit(0);
 
159
 
 
160
 
 
161
sub print_nodeid
 
162
{
 
163
    my $config_tree = shift;
 
164
    my $tokens = shift;
 
165
    my $details = shift;
 
166
 
 
167
    my $json = new JSON;
 
168
    $json->pretty();
 
169
    $json->canonical();
 
170
 
 
171
    my @all;
 
172
 
 
173
    foreach my $token ( @{$tokens} )
 
174
    {
 
175
        my $info = {
 
176
            'nodeid' => $config_tree->getNodeParam($token, 'nodeid', 1),
 
177
        };
 
178
 
 
179
        if( $details )
 
180
        {
 
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'} )
 
185
            {
 
186
                my $dsType = $config_tree->getNodeParam( $token, 'ds-type' );
 
187
                $info->{'param:ds-type'} = $dsType;
 
188
                if( $dsType eq 'collector' )
 
189
                {
 
190
                    foreach my $param
 
191
                        ('collector-type', 'collector-period',
 
192
                         'storage-type', 'data-file', 'data-dir', 'rrd-ds',
 
193
                         'ext-service-id',
 
194
                         'snmp-host', 'domain-name', 'snmp-object')
 
195
                    {
 
196
                        my $val = $config_tree->getNodeParam($token, $param);
 
197
 
 
198
                        if( defined( $val ) )
 
199
                        {
 
200
                            $info->{'param:' . $param} = $val;
 
201
                        }
 
202
                    }
 
203
                }
 
204
            }
 
205
        }
 
206
 
 
207
        push(@all, $info);
 
208
    }
 
209
 
 
210
    print $json->encode(\@all);
 
211
}
 
212
            
 
213
 
 
214
 
 
215
sub render_node
 
216
{
 
217
    my $config_tree = shift;
 
218
    my $token = shift;
 
219
    my $view = shift;
 
220
    my $out = shift;
 
221
 
 
222
    my $r = new Torrus::Renderer;
 
223
 
 
224
    my($fname, $mimetype) = $r->render($config_tree, $token, $view);
 
225
 
 
226
    if( defined($out) )
 
227
    {
 
228
        if( not copy( $fname, $out ) )
 
229
        {
 
230
            printf STDERR ("Failed to write to %s: %s\n", $out, $!);
 
231
            exit(1);
 
232
        }
 
233
 
 
234
        $fname = $out;
 
235
    }
 
236
        
 
237
    my $json = new JSON;
 
238
    $json->pretty();
 
239
 
 
240
    print $json->encode({'Content-type' => $mimetype,
 
241
                         'Filename' => $fname});
 
242
}
 
243
        
 
244
 
 
245
 
 
246
 
 
247
 
 
248
# Local Variables:
 
249
# mode: perl
 
250
# indent-tabs-mode: nil
 
251
# perl-indent-level: 4
 
252
# End: