line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# server::snmp Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Server::Snmp; |
7
|
1
|
|
|
1
|
|
775
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
7
|
use base qw(Metabrik::System::Package); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
518
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Default attribute values put here will BE inherited by subclasses |
13
|
|
|
|
|
|
|
sub brik_properties { |
14
|
|
|
|
|
|
|
return { |
15
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
16
|
|
|
|
|
|
|
tags => [ qw(unstable agent) ], |
17
|
|
|
|
|
|
|
author => 'GomoR ', |
18
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
19
|
|
|
|
|
|
|
attributes => { |
20
|
|
|
|
|
|
|
datadir => [ qw(datadir) ], |
21
|
|
|
|
|
|
|
hostname => [ qw(listen_hostname) ], |
22
|
|
|
|
|
|
|
port => [ qw(listen_port) ], |
23
|
|
|
|
|
|
|
community => [ qw(community_string) ], |
24
|
|
|
|
|
|
|
_snmp => [ qw(INTERNAL) ], |
25
|
|
|
|
|
|
|
_snmpd_pid => [ qw(INTERNAL) ], |
26
|
|
|
|
|
|
|
}, |
27
|
|
|
|
|
|
|
attributes_default => { |
28
|
|
|
|
|
|
|
hostname => 'localhost', |
29
|
|
|
|
|
|
|
port => 161, |
30
|
|
|
|
|
|
|
community => 'public', |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
commands => { |
33
|
|
|
|
|
|
|
install => [ ], # Inherited |
34
|
|
|
|
|
|
|
start => [ qw(listen_hostname|OPTIONAL listen_port|OPTIONAL datadir|OPTIONAL) ], |
35
|
|
|
|
|
|
|
stop => [ ], |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
require_modules => { |
38
|
|
|
|
|
|
|
'NetSNMP::agent' => [ ], |
39
|
|
|
|
|
|
|
'NetSNMP::ASN' => [ ], |
40
|
|
|
|
|
|
|
'Metabrik::File::Text' => [ ], |
41
|
|
|
|
|
|
|
'Metabrik::Shell::Command' => [ ], |
42
|
|
|
|
|
|
|
'Metabrik::Worker::Fork' => [ ], |
43
|
|
|
|
|
|
|
}, |
44
|
|
|
|
|
|
|
require_binaries => { |
45
|
|
|
|
|
|
|
'snmpd' => [ ], |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
need_packages => { |
48
|
|
|
|
|
|
|
ubuntu => [ qw(libsnmp-dev snmpd) ], |
49
|
|
|
|
|
|
|
debian => [ qw(libsnmp-dev snmpd) ], |
50
|
|
|
|
|
|
|
kali => [ qw(libsnmp-dev snmpd) ], |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _create_snmpd_conf { |
56
|
0
|
|
|
0
|
|
|
my $self = shift; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
59
|
0
|
|
|
|
|
|
my $port = $self->port; |
60
|
0
|
|
|
|
|
|
my $community = $self->community; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $conf = <
|
63
|
|
|
|
|
|
|
agentAddress udp:$hostname:$port |
64
|
|
|
|
|
|
|
#agentAddress udp:161,udp6:[::1]:161 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# SNMP v1, v2c |
67
|
|
|
|
|
|
|
#rocommunity public default -V systemonly |
68
|
|
|
|
|
|
|
rocommunity $community |
69
|
|
|
|
|
|
|
#rocommunity secret 10.0.0.0/16 |
70
|
|
|
|
|
|
|
#rwcommunity private default |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Full read-only access for SNMPv3 |
73
|
|
|
|
|
|
|
rouser authOnlyUser |
74
|
|
|
|
|
|
|
#rwuser authPrivUser priv |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sysLocation Metabrik |
77
|
|
|
|
|
|
|
sysContact GomoR |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Run as an AgentX master agent |
80
|
|
|
|
|
|
|
#master agentx |
81
|
|
|
|
|
|
|
EOF |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; |
84
|
0
|
|
|
|
|
|
$ft->overwrite(1); |
85
|
0
|
|
|
|
|
|
$ft->append(0); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
$ft->write($conf, $self->datadir.'/snmpd.conf') |
88
|
|
|
|
|
|
|
or return $self->log->error("_create_snmpd_conf: write failed"); |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _start_snmpd { |
94
|
0
|
|
|
0
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $snmpd_conf = $self->datadir.'/snmpd.conf'; |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; |
99
|
0
|
0
|
|
|
|
|
my $wf = Metabrik::Worker::Fork->new_from_brik_init($self) or return; |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
defined(my $pid = $wf->start) or return $self->log->error("_start_snmpd: start failed"); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Son |
104
|
0
|
0
|
|
|
|
|
if (! $pid) { |
105
|
0
|
|
|
|
|
|
my $cmd = "snmpd -f -C -c $snmpd_conf 2>&1"; |
106
|
0
|
0
|
|
|
|
|
$sc->system($cmd) or return $self->log->error("_start_snmpd: system failed"); |
107
|
0
|
|
|
|
|
|
exit(0); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Father |
111
|
0
|
|
|
|
|
|
$self->_snmpd_pid($pid); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub start { |
117
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
118
|
0
|
|
|
|
|
|
my ($hostname, $port, $root) = @_; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
0
|
|
|
|
$hostname ||= $self->hostname; |
121
|
0
|
|
0
|
|
|
|
$port ||= $self->port; |
122
|
0
|
|
0
|
|
|
|
$root ||= $self->datadir; |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
0
|
|
|
|
if ($port < 1024 && $< != 0) { |
125
|
0
|
|
|
|
|
|
return $self->log->error("start: need root privileges to bind port [$port]"); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Create snmpd.conf file |
129
|
0
|
0
|
|
|
|
|
$self->_create_snmpd_conf or return; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Start snmpd |
132
|
0
|
0
|
|
|
|
|
$self->_start_snmpd or return; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#sub do_one { return int(rand(10)) }; |
135
|
|
|
|
|
|
|
#sub do_two { return "two" }; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
#my $root_oid = '1.3.6.1.4.1.8072.9999.9999.123'; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#my %handlers = ( |
140
|
|
|
|
|
|
|
#'1' => { handler => \&do_one, type => &NetSNMP::ASN::ASN_GAUGE }, |
141
|
|
|
|
|
|
|
#'2' => { handler => \&do_two }, # default type ASN_OCTET_STR |
142
|
|
|
|
|
|
|
#); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
#my $agent = SNMP::Agent->new( |
145
|
|
|
|
|
|
|
#'brik_snmp_agent', |
146
|
|
|
|
|
|
|
#$root_oid, |
147
|
|
|
|
|
|
|
#\%handlers, |
148
|
|
|
|
|
|
|
#); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
#return $self->_snmp($agent)->run; |
151
|
0
|
|
|
|
|
|
return 1; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub stop { |
155
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
#if (defined($self->_snmp)) { |
158
|
|
|
|
|
|
|
#$self->_snmp->shutdown; |
159
|
|
|
|
|
|
|
#$self->_snmp(undef); |
160
|
|
|
|
|
|
|
#} |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if (defined($self->_snmpd_pid)) { |
163
|
0
|
|
|
|
|
|
kill('INT', $self->_snmpd_pid); |
164
|
0
|
|
|
|
|
|
$self->_snmpd_pid(undef); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
return 1; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |