summaryrefslogtreecommitdiff
path: root/scripts/vyatta-show-snmp-v3.pl
blob: dc8162309dd9e11c6f5c029672e5c9718909449a (plain)
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
#! /usr/bin/perl

use Getopt::Long;

sub show_view() {
    print <<END;

SNMPv3 Views:

END

    foreach my $view ( listNodes("view") ) {
        print "View : $view\nOIDs :\n";
        foreach my $oid ( listNodes("view $view oid") ) {
            my $exclude = '';
            $exclude = ' exclude'
              if ( isExists("view $view oid $oid exclude") );
            my $mask = '';
            if ( isExists("view $view oid $oid mask") ) {
                my $value = returnValue("view $view oid $oid mask");
                $mask = " mask $value";
            }
            print "       .$oid$exclude$mask\n";
        }
        print "\n";
    }
}

sub show_group() {
    print <<END;

SNMPv3 Groups:

Group               View
-----               ----
END

    foreach my $group ( listNodes("group") ) {
        my $view = returnValue("group $group view");
        my $mode = returnValue("group $group mode");
        if ( length($group) >= 20 ) {
            print "$group\n                    $view($mode)\n";
        }
        else {
            $~ = "GROUP_FORMAT";
            format GROUP_FORMAT =
@<<<<<<<<<<<<<<<<<< @*(@*)
$group $view $mode
.
            write;
        }
    }
    print "\n";
}

sub show_user() {
    print <<END;

SNMPv3 Users:

User                Auth Priv Mode Group
----                ---- ---- ---- -----
END

    foreach my $user ( listNodes("user") ) {
        my $auth  = returnValue("user $user auth type");
        my $priv  = returnValue("user $user privacy type");
        my $mode  = returnValue("user $user mode");
        my $group = returnValue("user $user group");
        if ( length($user) >= 20 ) {
            print "$user\n                    $auth  $priv  $mode   $group\n";
        }
        else {
            $~ = "USER_FORMAT";
            format USER_FORMAT =
@<<<<<<<<<<<<<<<<<< @<<< @<<< @<<< @*
$user $auth $priv $mode $group
.
            write;
        }
    }
    print "\n";
}

sub show_trap() {
    print <<END;

SNMPv3 Trap-targets:

Tpap-target                   Port   Protocol Auth Priv Type   EngineID                 User
-----------                   ----   -------- ---- ---- ----   --------                 ----
END

    foreach my $trap ( listNodes("trap-target") ) {
        my $auth     = returnValue("trap-target $trap auth type");
        my $priv     = returnValue("trap-target $trap privacy type");
        my $type     = returnValue("trap-target $trap type");
        my $port     = returnValue("trap-target $trap port");
        my $user     = returnValue("trap-target $trap user");
        my $protocol = returnValue("trap-target $trap protocol");
        my $engineid = returnValue("trap-target $trap engineid");
        if ( length($trap) >= 30 ) {
            $~ = "TRAP_BIG_FORMAT";
            format TRAP_BIG_FORMAT =
^*
$trap
                              @<<<<< @<<<<<<< @<<< @<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<... @*
$port $protocol $auth $priv $type $engineid $user
.
            write;
        }
        else {
            $~ = "TRAP_FORMAT";
            format TRAP_FORMAT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< @<<<<<<< @<<< @<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<... @*
$trap $port $protocol $auth $priv $type $engineid $user
.
            write;
        }
    }
    print "\n";
}

sub show_all() {
    show_user();
    show_group();
    show_view();
    show_trap();
}

sub listNodes {
    my $path = shift;
    my @nodes =
      split( ' ', `cli-shell-api listActiveNodes service snmp v3 $path` );
    return map { substr $_, 1, -1 } @nodes;
}

sub returnValue {
    my $path  = shift;
    my $value = `cli-shell-api returnActiveValue service snmp v3 $path`;
    return $value;
}

sub isExists {
    my $path = shift;
    system("cli-shell-api existsActive service snmp v3 $path");
    return !$?;
}

my $all;
my $view;
my $group;
my $user;
my $trap;

GetOptions(
    "all!"   => \$all,
    "view!"  => \$view,
    "group!" => \$group,
    "user!"  => \$user,
    "trap!"  => \$trap,
);

show_all()   if ($all);
show_view()  if ($view);
show_group() if ($group);
show_user()  if ($user);
show_trap()  if ($trap);