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
|
#! /usr/bin/perl
use strict;
use warnings;
use BER;
use DBI;
use POSIX;
use Time::HiRes;
use Net::Ping;
require 'SNMP_Session.pm';
use lib '../include';
use nms;
use threads;
poll_loop();
sub poll_loop {
my $dbh = nms::db_connect();
my $qcores = $dbh->prepare('SELECT DISTINCT coreswitches.sysname, coreswitches.switch, coreswitches.ip, coreswitches.community FROM uplinks JOIN switches AS coreswitches ON (uplinks.coreswitch = coreswitches.switch)');
my $qaps = $dbh->prepare("SELECT switches.sysname, switches.switch, uplinks.blade, uplinks.port FROM uplinks NATURAL JOIN switches WHERE uplinks.coreswitch = ?");
my $qpoll = $dbh->prepare("UPDATE ap_poll SET model=?, last_poll=now() WHERE switch = ?");
while (1) {
$qcores->execute();
my $cores = $qcores->fetchall_hashref("sysname");
foreach my $core (keys %$cores) {
my $ip = $cores->{$core}{'ip'};
my $community = $cores->{$core}{'community'};
printf "Polling %s (%s)\n", $core, $ip;
eval {
my $session = SNMPv2c_Session->open($ip, $community, 161) or die "Couldn't talk to switch";
$qaps->execute($cores->{$core}{'switch'});
while (my $aps = $qaps->fetchrow_hashref()) {
my $sysname = $aps->{'sysname'};
my $blade = $aps->{'blade'};
my $port = $aps->{'port'};
my $oid = BER::encode_oid(1,3,6,1,2,1,105,1,1,1,9,$blade,$port); # POWER-ETHERNET-MIB...pethPsePortType
my $mode = fetch_snmp($session, $oid);
$qpoll->execute($mode, $aps->{'switch'});
printf "%s (%s:%s/%s): %s\n", $sysname, $core, $blade, $port, $mode;
}
};
if ($@) {
mylog("ERROR: $@ (during poll of $ip)");
$dbh->rollback;
}
}
sleep 2;
}
}
# Kokt fra snmpfetch.pl, vi bør nok lage et lib
sub fetch_snmp {
my ($session, $oid) = @_;
if ($session->get_request_response($oid)) {
my ($bindings) = $session->decode_get_response ($session->{pdu_buffer});
my $binding;
while ($bindings ne '') {
($binding,$bindings) = &decode_sequence ($bindings);
my ($oid,$value) = &decode_by_template ($binding, "%O%@");
return BER::pretty_print($value);
}
}
die "Couldn't get info from switch";
}
sub mylog {
my $msg = shift;
my $time = POSIX::ctime(time);
$time =~ s/\n.*$//;
printf STDERR "[%s] %s\n", $time, $msg;
}
|