/
observium-poller
210 lines (188 loc) · 7.75 KB
/
observium-poller
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
#!/usr/bin/perl
use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
use Schema;
use PHP::Serialization qw(unserialize);
use Net::SNMP;
use Net::SNMP::Util::OID qw(system* interfaces* at* ip* icmp* tcp* udp* egp* snmp* tcp*);
use SNMP;
use Getopt::Long;
use Poller;
use Utils;
use Config::Any;
use Regexp::Common qw/net/;
use RRDUtils;
use Time::HiRes qw/gettimeofday tv_interval/;
use DateTime;
use Data::Dumper;
sub snmp_get_cb {
my ( $session, $device ) = @_;
$device->{snmp_elapsed} = tv_interval( $device->{snmp_start}, [ gettimeofday ] );
my $list;
$list = $session->var_bind_list();
print "List is: ", Dumper $list;
$device->{snmp_data} = $list;
$device->last_polled( DateTime->now( time_zone => 'local' ) );
# Device poll time is not applicable since we're polling them all at once
return;
}
my $poller_start = [ gettimeofday ];
my $start_time = DateTime->now( time_zone => 'local' );
our $cfg = Config::Any->load_files( { files => [ $FindBin::Bin."/observium.yml", $FindBin::Bin."/os.json" ], use_ext => 1 } );
our ($hosts, $debug, $norrd, $quiet, $modules) = ( '', 0, 0, 0, undef );
my $result = GetOptions ( 'hosts=s' => \$hosts, 'debug=i' => \$debug, 'norrd' => \$norrd, 'quiet' => \$quiet, 'modules=s' => \$modules );
sub usage {
print STDERR 'Usage: observium-poller --hosts=[all|numbers] --debug=debug_level --norrd --quiet', "\n";
exit EX_USAGE;
}
if ( !$result || $hosts le '' ) {
usage();
}
debug_msg( 5, 'Config::Any: ', join(', ', Config::Any->plugins()), "\n", join(':',Config::Any->extensions()), "\n" );
debug_msg( 4, Dumper $cfg );
our $config = $cfg->[0]->{ $FindBin::Bin.'/observium.yml' };
my $os = $cfg->[1]->{ $FindBin::Bin.'/os.json' };
if ( ref $config->{snmp}->{mibdir} eq 'ARRAY' and scalar @{ $config->{snmp}->{mibdir} } > 0 ) {
SNMP::addMibDirs( @{ $config->{snmp}->{mibdir} } );
}
my $data = Schema->connect( join( ':', 'dbi', $config->{database}->{driver}, 'host='.$config->{database}->{host}.';dbname='.$config->{database}->{database} ),
$config->{database}->{user}, $config->{database}->{password}, { lc($config->{database}->{drive}).'_enable_utf8' => 1, RaiseError => 1, AutoCommit => 1 });
my $devices = $data->resultset('Device')->search_rs( { disabled => 0 }, );
if ( $hosts ne 'all' ) {
# Do not support BETWEEN right now
$devices = $devices->search_rs( { device_id => { -in => [ split/,/, $hosts ] } } );
}
my $devlist = { map +( $_->hostname => $_ ), $devices->all() };
#print Dumper $devlist;
# First of all: fping them
my $pinglist = { map { +"$_" => 0 } keys %$devlist };
my $fping = $config->{network}->{fping}." -t ".$config->{network}->{timeout}." -c $config->{network}->{pings} -q ";
# Create RRD directory, if needed, and init all default RRDs
map {
my $rrdpath = $config->{rrddir}.'/'.$_;
if ( !-e $rrdpath ) {
mkdir $rrdpath or die "Could not make $rrdpath: $!";
}
RRDUtils::InitRRD( $rrdpath, 'status.rrd', "DS:status:GAUGE:600:0:1 " )
or die "Cannot create $rrdpath/status.rrd: $!";
RRDUtils::InitRRD( $rrdpath, 'ping.rrd', "DS:ping:GAUGE:600:0:65535 " )
or die "Cannot create $rrdpath/ping.rrd: $!";
RRDUtils::InitRRD( $rrdpath, 'ping_snmp.rrd', "DS:ping_snmp:GAUGE:600:0:65535 " )
or die "Cannot create $rrdpath/ping_snmp.rrd: $!";
} keys %$devlist;
my $snmplist = [];
#print $fping;
my $try = 0;
while ( ($try++ < $config->{network}->{retries}) && scalar(keys(%$pinglist)) > 0 ) {
debug_msg(1, "Pinging ", join(' ', keys %$pinglist), "\n");
open(PING, "$fping ".join(' ', keys %$pinglist)." 2>&1 |")
or die "Could not $fping: $!";
# fping returns
# audi-371.cpn.infotel.ru : xmt/rcv/%loss = 5/0/100%
# skoda-889.cpn.infotel.ru : xmt/rcv/%loss = 5/5/0%, min/avg/max = 44.8/45.1/45.5
# We're interested in hostname and loss percentage
while (<PING>) {
die unless m|^$RE{net}{domain}{-keep}\s+: xmt/rcv/\%loss = (\d+)/(\d+)/(\d+)\%|;
if ( $4 != 100 ) {
my $host = $1;
die "$_ is wrong"
unless m|^$RE{net}{domain}{-keep}\s+: xmt/rcv/\%loss = (\d+)/(\d+)/(\d+)\%, min/avg/max = (\d+(\.\d+)?)/(\d+(\.\d+)?)/(\d+(\.\d+)?)|;
$devlist->{$host}->{ping} = $7;
delete $pinglist->{$host};
my $device = $devlist->{$host};
# print join(' ', $device->{authname}, $device->{authalgo},$device->{authpass}, $device->{cryptoalgo},$device->{cryptopass}),
"\n";
my ($snmp,$error) = Net::SNMP->session(
-hostname => $host,
-version => 'snmp'.$device->snmpver,
-username => $device->authname,
-authprotocol => $device->authalgo,
-authpassword => $device->authpass,
-privprotocol => $device->cryptoalgo,
-privpassword => $device->cryptopass,
-port => $device->port, -timeout => $device->timeout||3,
-retries => $device->retries||$config->{snmp}->{retries},
-nonblocking => 1,
);
if ( !defined $snmp ) {
print "Error adding SNMP for $host: $error\n";
}
else {
$devlist->{$host}->{snmp} = $snmp;
push @$snmplist, $host;
}
}
}
close PING;
}
# Now, for all alive devices, get SNMP
foreach ( @$snmplist ) {
my $result = $devlist->{$_}->{snmp}->get_request(
-callback => [ \&snmp_get_cb, $devlist->{$_} ],
-varbindlist => [oid("sysObjectID")],
);
if ( !$result ) {
print "Cannot bulk for $_ ", $devlist->{$_}->{snmp}->error(), "\n";
$devlist->{$_}->{snmp}->close();
delete $devlist->{$_}->{snmp};
}
}
debug_msg(1, "Starting dispatcher\n");
snmp_dispatcher();
my $poller = new Poller( schema => $data );
my $status_type = '(snmp)';
foreach my $devname ( sort keys %$devlist ) {
my $device = $devlist->{$devname};
debug_msg(1, $device->device_id, ': ', $device->hostname, ', OS: ', $device->os, ' ', $device->status, "\n");
if ( $os->{os}->{ $device->os } ) {
my $os_group = $os->{os}->{ $device->os }->{group};
$device->{os_group} = $os_group;
}
my $newstatus = exists $device->{ping} && exists $device->{snmp_data};
my $rrdpath = $config->{rrddir}.'/'.$device->hostname;
if ( exists $device->{ping} ) {
debug_msg(1," * ping: ", $device->{ping}, "\n");
RRDUtils::UpdateRRD( $rrdpath, 'ping.rrd', 'N:'.$device->{ping} );
if ( exists $device->{snmp_data} ) {
debug_msg(1," Going to add SNMP according to tables\n");
RRDUtils::UpdateRRD( $rrdpath, 'ping_snmp.rrd', 'N:'.$device->{snmp_elapsed} );
$poller->requests( $device );
}
else {
debug_msg(1, " * Dead (SNMP)\n");
$status_type = '(snmp)';
RRDUtils::UpdateRRD( $rrdpath, 'ping_snmp.rrd', 'N:U' );
}
} else {
debug_msg(1, " * Dead (ping)\n");
$status_type = '(ping)';
RRDUtils::UpdateRRD( $rrdpath, 'ping.rrd', 'N:U' );
RRDUtils::UpdateRRD( $rrdpath, 'ping_snmp.rrd', 'N:U' );
}
RRDUtils::UpdateRRD( $rrdpath, 'status.rrd', 'N:'.$newstatus );
if ( $newstatus != $device->status ) {
# print " # Status changed\n";
$device->log_event( 'Device status changed to ' . ($newstatus == 1 ? 'Up' : 'Down') . $status_type, 'system', '');
}
$device->status( $newstatus );
}
debug_msg(1, "Starting SNMP dispatcher\n");
snmp_dispatcher();
debug_msg(1, "Ended SNMP dispatcher\n");
my $polled_time = tv_interval( $poller_start, [ gettimeofday ] );;
map { eval {
my $devname = $_;
$poller->process( $devlist->{$devname} );
$polled_time = tv_interval( $poller_start, [ gettimeofday ] );
$devlist->{$devname}->last_polled_timetaken( $polled_time );
RRDUtils::UpdateRRD( $config->{rrddir}.'/'.$devname , 'perf-poller.rrd', 'N:'.$polled_time );
$devlist->{$devname}->insert_or_update();
};
print "Error evaling $_: $@\n" if $@;} sort keys %$devlist;
$data->resultset('PerfTime')->new( { type => 'poll',
start => $start_time,
duration => $polled_time,
devices => scalar(keys(%$devlist)),
doing => $hosts,
} )->insert_or_update();