line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# server::snmptrap Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Server::Snmptrap; |
7
|
1
|
|
|
1
|
|
769
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Metabrik::System::Package); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
748
|
|
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 snmp trap trapd snmptrapd) ], |
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
|
|
|
|
|
|
|
_wf => [ qw(INTERNAL) ], |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
attributes_default => { |
26
|
|
|
|
|
|
|
hostname => 'localhost', |
27
|
|
|
|
|
|
|
port => 162, |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
commands => { |
30
|
|
|
|
|
|
|
install => [ ], # Inherited |
31
|
|
|
|
|
|
|
start => [ qw(listen_hostname|OPTIONAL listen_port|OPTIONAL datadir|OPTIONAL) ], |
32
|
|
|
|
|
|
|
stop => [ ], |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
require_modules => { |
35
|
|
|
|
|
|
|
'Net::SNMPTrapd' => [ ], |
36
|
|
|
|
|
|
|
'Metabrik::Worker::Fork' => [ ], |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
need_packages => { |
39
|
|
|
|
|
|
|
ubuntu => [ qw(libsnmp-dev) ], |
40
|
|
|
|
|
|
|
debian => [ qw(libsnmp-dev) ], |
41
|
|
|
|
|
|
|
kali => [ qw(libsnmp-dev) ], |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub start { |
47
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
48
|
0
|
|
|
|
|
|
my ($hostname, $port, $root) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
0
|
|
|
|
$hostname ||= $self->hostname; |
51
|
0
|
|
0
|
|
|
|
$port ||= $self->port; |
52
|
0
|
|
0
|
|
|
|
$root ||= $self->datadir; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
0
|
|
|
|
if ($port < 1024 && $< != 0) { |
55
|
0
|
|
|
|
|
|
return $self->log->error("start: need root privileges to bind port [$port]"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
my $proc = Net::SNMPTrapd->new |
59
|
|
|
|
|
|
|
or return $self->log->error("start: ".Net::SNMPTrapd->error); |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
my $wf = Metabrik::Worker::Fork->new_from_brik_init($self) or return; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
defined(my $pid = $wf->start) or return $self->log->error("start: start failed"); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Son |
66
|
0
|
0
|
|
|
|
|
if (! $pid) { |
67
|
0
|
|
|
|
|
|
$self->log->debug("start: son process started: $$"); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
while (1) { |
70
|
0
|
|
|
|
|
|
my $trap = $proc->get_trap; |
71
|
0
|
0
|
|
|
|
|
if (! defined($trap)) { |
|
|
0
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
printf "$0: %s\n", Net::SNMPTrapd->error; |
73
|
0
|
|
|
|
|
|
exit(1); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif ($trap == 0) { |
76
|
0
|
|
|
|
|
|
next; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (! defined($trap->process_trap)) { |
80
|
0
|
|
|
|
|
|
printf("$0: %s\n", Net::SNMPTrapd->error); |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
printf("%s\t%i\t%i\t%s\n", |
83
|
|
|
|
|
|
|
$trap->remoteaddr, |
84
|
|
|
|
|
|
|
$trap->remoteport, |
85
|
|
|
|
|
|
|
$trap->version, |
86
|
|
|
|
|
|
|
$trap->community, |
87
|
|
|
|
|
|
|
); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
$self->log->debug("start: son process exited: $$"); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
exit(0); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Father |
97
|
0
|
|
|
|
|
|
$self->_wf($wf); |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return $wf->pid; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub stop { |
103
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $wf = $self->_wf; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if (defined($wf)) { |
108
|
0
|
|
|
|
|
|
$self->log->verbose("stop: process with pid [".$wf->pid."]"); |
109
|
0
|
|
|
|
|
|
$wf->stop; |
110
|
0
|
|
|
|
|
|
$self->_wf(undef); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |