summaryrefslogtreecommitdiff
path: root/scripts/snmp/vyatta-snmp.pl
blob: fb4e22e7a07de5a066518c104a321fd898e093c7 (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/perl
#
# Module: vyatta-snmp.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Stig Thormodsrud
# Date: October 2007
# Description: Script to glue vyatta cli to snmp daemon
#
# **** End License ****
#

use strict;
use warnings;

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
use Vyatta::Misc;
use NetAddr::IP;
use Getopt::Long;
use File::Copy;
use Socket;
use Socket6;

my $mibdir    = '/opt/vyatta/share/snmp/mibs';
my $snmp_init = 'invoke-rc.d snmpd';
my $snmp_conf = '/etc/snmp/snmpd.conf';
my $snmp_client = '/etc/snmp/snmp.conf';
my $snmp_tmp  = "/tmp/snmpd.conf.$$";
my $snmp_snmpv3_user_conf = '/usr/share/snmp/snmpd.conf';
my $snmp_snmpv3_createuser_conf = '/var/lib/snmp/snmpd.conf';
my $versionfile = '/opt/vyatta/etc/version';
my $local_agent = 'unix:/var/run/snmpd.socket';
my $password_file = '/config/snmp/superuser_pass';

my $snmp_level = 'service snmp';

sub snmp_running {
    open(my $pidf, '<', "/var/run/snmpd.pid")
        or return;
    my $pid = <$pidf>;
    close $pidf;

    chomp $pid;
    my $exe = readlink "/proc/$pid/exe";

    return (defined($exe) && $exe eq "/usr/sbin/snmpd");
}

sub snmp_stop {
    system("$snmp_init stop > /dev/null 2>&1");
}

sub snmp_stop {
    system("$snmp_init restart > /dev/null 2>&1");
}

sub snmp_start {

    # we must stop snmpd first for creating vyatta user
    system("$snmp_init stop > /dev/null 2>&1");
    open(my $fh, '>', $snmp_tmp)
        or die "Couldn't open $snmp_tmp - $!";

    select $fh;
    snmp_get_constants();
    snmp_get_values();
    snmp_get_traps();
    close $fh;
    select STDOUT;

    snmp_client_config();

    move($snmp_tmp, $snmp_conf)
        or die "Couldn't move $snmp_tmp to $snmp_conf - $!";
}

sub get_version {
    my $version = "unknown-version";

    if (open(my $f, '<', $versionfile)) {
        while (<$f>) {
            chomp;
            if (m/^Version\s*:\s*(.*)$/) {
                $version = $1;
                last;
            }
        }
        close $f;
    }
    return $version;
}

# convert address to snmpd transport syntax
sub transport_syntax {
    my ($addr, $port) = @_;
    my $ip = new NetAddr::IP $addr;
    die "$addr: not a valid IP address" unless $ip;

    my $version = $ip->version();
    return "udp:$addr:$port"    if ($version == 4);
    return "udp6:[$addr]:$port" if ($version == 6);
    die "$addr: unknown IP version $version";
}

# Test if IPv6 is possible by opening a socket
sub ipv6_disabled {
    socket(my $s, PF_INET6, SOCK_DGRAM, 0)
        or return 1;
    close($s);
    return;
}

# Find SNMP agent listening addresses
sub get_listen_address {
    my $config = new Vyatta::Config;
    my @listen;

    $config->setLevel('service snmp listen-address');
    my @address = $config->listNodes();

    if(@address) {
        foreach my $addr (@address) {
            my $port = $config->returnValue("$addr port");
            push @listen, transport_syntax($addr, $port);
        }
    } else {

        # default if no address specified
        @listen = ('udp:161');
        push @listen, 'udp6:161' unless ipv6_disabled();
        return @listen;
    }

    return @listen;
}

sub snmp_get_constants {
    my $version = get_version();
    my $now = localtime;
    my @addr = get_listen_address();

    # add local unix domain target for use by operational commands
    unshift @addr, $local_agent;

    print "# autogenerated by vyatta-snmp.pl on $now\n";
    print "sysDescr $version\n";
    print "sysObjectID 1.3.6.1.4.1.44641\n";
    print "sysServices 14\n";
    print "master agentx\n";	# maybe needed by lldpd
    print "agentaddress ", join(',',@addr), "\n";

    # add hook to read IF-MIB::ifAlias from sysfs
    print "pass .1.3.6.1.2.1.31.1.1.1.18 /opt/vyatta/sbin/if-mib-alias\n";

    print "smuxpeer .1.3.6.1.4.1.3317.1.2.2\n";		# ospfd
    print "smuxpeer .1.3.6.1.4.1.3317.1.2.5\n";		# bgpd
    print "smuxpeer .1.3.6.1.4.1.3317.1.2.3\n";		# ripd
    print "smuxpeer .1.3.6.1.4.1.3317.1.2.9\n";		# mribd
    print "smuxpeer .1.3.6.1.2.1.83\n";			# mribd
    print "smuxpeer .1.3.6.1.4.1.3317.1.2.8\n";		# pimd
    print "smuxpeer .1.3.6.1.2.1.157\n";		# pimd
    print "smuxsocket localhost\n";
}

# generate a random character hex string
sub randhex {
    my $length = shift;
    return join "", map {unpack "H*", chr(rand(256))} 1..($length/2);
}

# output snmpd.conf file syntax for community
sub print_community {
    my ($config, $community) = @_;
    my $ro = $config->returnValue('authorization');
    $ro = 'ro' unless $ro;

    my @clients = $config->returnValues('client');
    my @networks = $config->returnValues('network');

    my @restriction = (@clients, @networks);
    if (!@restriction) {
        print $ro . "community $community\n";
        print $ro . "community6 $community\n" unless ipv6_disabled();
        return;
    }

    foreach my $addr (@restriction) {
        my $ip = new NetAddr::IP $addr;
        die "$addr: Not a valid IP address" unless $ip;

        if ($ip->version() == 4) {
            print $ro . "community $community $addr\n";
        } elsif ($ip->version() == 6) {
            print $ro . "community6 $community $addr\n";
        } else {
            die "$addr: bad IP version ", $ip->version();
        }
    }
}

sub snmp_get_values {
    my $config = new Vyatta::Config;

    my @communities = $config->listNodes("service snmp community");
    foreach my $community (@communities) {
        $config->setLevel("service snmp community $community");
        print_community($config, $community);
    }

    $config->setLevel("service snmp smux-peer");
    my @smuxpeers = $config->returnValues();
    foreach my $smuxpeer (@smuxpeers) {
        print "smuxpeer $smuxpeer \n";
    }

    $config->setLevel($snmp_level);
    my $contact = $config->returnValue("contact");
    if (defined $contact) {
        print "SysContact $contact \n";
    }

    my $description = $config->returnValue("description");
    if (defined $description) {
        print "SysDescr $description \n";
    }

    my $location = $config->returnValue("location");
    if (defined $location) {
        print "SysLocation $location \n";
    }
}

sub snmp_get_traps {
    my $config = new Vyatta::Config;
    $config->setLevel($snmp_level);

    # linkUp/Down configure the Event MIB tables to monitor
    # the ifTable for network interfaces being taken up or down
    # for making internal queries to retrieve any necessary information

    # create an internal snmpv3 user of the form 'vyattaxxxxxxxxxxxxxxxx'
    my $vyatta_user = "vyatta" . randhex(16);
    snmp_create_snmpv3_user($vyatta_user);
    snmp_write_snmpv3_user($vyatta_user);
    print "iquerySecName $vyatta_user\n";

    # Modified from the default linkUpDownNotification
    # to include more OIDs and poll more frequently
    print <<EOF;
notificationEvent  linkUpTrap    linkUp   ifIndex ifDescr ifType ifAdminStatus ifOperStatus
notificationEvent  linkDownTrap  linkDown ifIndex ifDescr ifType ifAdminStatus ifOperStatus
monitor  -r 10 -e linkUpTrap   "Generate linkUp" ifOperStatus != 2
monitor  -r 10 -e linkDownTrap "Generate linkDown" ifOperStatus == 2
EOF

    my @trap_targets = $config->listNodes("trap-target");
    return unless @trap_targets;

    foreach my $trap_target (@trap_targets) {
        my $port = $config->returnValue("trap-target $trap_target port");
        my $community= $config->returnValue("trap-target $trap_target community");

        print "trap2sink $trap_target";
        print ":$port" if $port;
        print " $community" if $community;
        print "\n";
    }
}

# Configure SNMP client parameters
sub snmp_client_config {
    my $config = new Vyatta::Config;
    $config->setLevel($snmp_level);

    open(my $cf, '>', $snmp_client)
        or die "Couldn't open $snmp_client - $!";

    my $version = get_version();
    my $now = localtime;
    print {$cf}  "# autogenerated by vyatta-snmp.pl on $now\n";

    my $trap_source = $config->returnValue('trap-source');
    print {$cf} "clientaddr $trap_source\n" if ($trap_source);
    close $cf;
}

sub snmp_create_snmpv3_user {

    my $vyatta_user = shift;
    my $passphrase = randhex(32);

    my $createuser = "createUser $vyatta_user MD5 \"$passphrase\" DES";
    open(my $fh, '>', $snmp_snmpv3_createuser_conf) || die "Couldn't open $snmp_snmpv3_createuser_conf - $!";
    print $fh $createuser;
    close $fh;

    open(my $pass_file, '>', $password_file) || die "Couldn't open $password_file - $!";
    print $pass_file $passphrase;
    close $pass_file;
}

sub snmp_write_snmpv3_user {

    my $vyatta_user = shift;
    my $user = "rwuser $vyatta_user\n";
    open(my $fh, '>', $snmp_snmpv3_user_conf) || die "Couldn't open $snmp_snmpv3_user_conf - $!";
    print $fh $user;
    close $fh;
}

#
# main
#
my $update_snmp;
my $stop_snmp;
my $restart_snmp;

GetOptions(
    "update-snmp!"  => \$update_snmp,
    "restart-snmp!" => \$restart_snmp,
    "stop-snmp!"    => \$stop_snmp
);

snmp_start()   if ($update_snmp);
snmp_restart() if ($restart_snmp);
snmp_stop()    if ($stop_snmp);