9 require 'SNMP_Session.pm';
11 my $password = 'removed';
14 my $dbh = DBI->connect("dbi:Pg:dbname=nms;host=localhost", "nms", "nms")
15 or die "Couldn't connect to database";
16 $dbh->{AutoCommit} = 0;
18 # normal mode: fetch switches from the database
19 # instant mode: poll the switches specified on the command line
20 my $instant = (defined($ARGV[0]));
23 $qualification = "sysname=?";
25 $qualification = <<"EOF";
26 (last_updated IS NULL OR now() - last_updated > poll_frequency)
27 AND (locked='f' OR now() - last_updated > '15 minutes'::interval)
31 my $qswitch = $dbh->prepare(<<"EOF")
34 DATE_TRUNC('second', now() - last_updated - poll_frequency) AS overdue
37 NATURAL LEFT JOIN switchtypes
43 FOR UPDATE OF switches
45 or die "Couldn't prepare qswitch";
46 my $qlock = $dbh->prepare("UPDATE switches SET locked='t', last_updated=now() WHERE switch=?")
47 or die "Couldn't prepare qlock";
48 my $qunlock = $dbh->prepare("UPDATE switches SET locked='f', last_updated=now() WHERE switch=?")
49 or die "Couldn't prepare qunlock";
50 my $qpoll = $dbh->prepare("INSERT INTO polls (time, switch, port, bytes_in, bytes_out) VALUES (timeofday()::timestamp,?,?,?,?)")
51 or die "Couldn't prepare qpoll";
52 my $qtemppoll = $dbh->prepare("INSERT INTO temppoll (time, switch, temp) VALUES (timeofday()::timestamp,?::text::int,?::text::float)")
53 or die "Couldn't prepare qtemppoll";
54 my $qcpupoll = $dbh->prepare("INSERT INTO cpuloadpoll (time, switch, entity, value) VALUES (timeofday()::timestamp,?::text::int,?,?)")
55 or die "Couldn't prepare qtemppoll";
60 $sysname = shift @ARGV;
61 exit if (!defined($sysname));
62 $qswitch->execute($sysname)
63 or die "Couldn't get switch";
65 # Find a switch to grab
67 or die "Couldn't get switch";
69 my $switch = $qswitch->fetchrow_hashref();
71 if (!defined($switch)) {
75 mylog("No such switch $sysname available, quitting.");
78 mylog("No available switches in pool, sleeping.");
84 $qlock->execute($switch->{'switch'})
85 or die "Couldn't lock switch";
88 if ($switch->{'locked'}) {
89 mylog("WARNING: Lock timed out on $switch->{'ip'}, breaking lock");
93 if (defined($switch->{'overdue'})) {
94 $msg = sprintf "Polling ports %s on %s (%s), %s overdue.",
95 $switch->{'ports'}, $switch->{'ip'}, $switch->{'sysname'}, $switch->{'overdue'};
97 $msg = sprintf "Polling ports %s on %s (%s), never polled before.",
98 $switch->{'ports'}, $switch->{'ip'}, $switch->{'sysname'};
102 my $ip = $switch->{'ip'};
104 if ($ip eq '127.0.0.1') {
105 mylog("Polling disabled for this switch, skipping.");
106 $qunlock->execute($switch->{'switch'})
107 or die "Couldn't unlock switch";
112 my $community = $switch->{'community'};
113 my $start = [Time::HiRes::gettimeofday];
116 if ($switch->{'wide_counters'}) {
117 $session = SNMPv2c_Session->open($ip, $community, 161)
118 or die "Couldn't talk to switch";
120 $session = SNMP_Session->open($ip, $community, 161)
121 or die "Couldn't talk to switch";
123 my @ports = expand_ports($switch->{'ports'});
125 for my $port (@ports) {
126 my $in = fetch_data($session, $port, 0, $switch->{'wide_counters'});
127 my $out = fetch_data($session, $port, 1, $switch->{'wide_counters'});
129 $qpoll->execute($switch->{'switch'}, $port, $in, $out);
131 my $conn = switch_connect($ip);
132 if (!defined($conn)) {
133 print "Could not connect to switch ".$switch->{'switch'}."\n";
134 } elsif ($switch->{'switchtype'} eq 'es3024') {
135 my @data = switch_exec('sys monitor status', $conn);
136 my @fields = split(/\s+/, $data[2]);
137 # The temp fields are 6, 7, 8
138 print "$fields[7] + $fields[8] + $fields[9]\n";
139 my $avgtemp = ($fields[7] + $fields[8] + $fields[9]) / 3;
140 print $avgtemp." avgtemp\n";
141 $qtemppoll->execute($switch->{'switch'},
142 $avgtemp) or die "Could not exec qtemppoll";
143 } elsif ($switch->{'switchtype'} eq 'cisco6509') {
146 my $oid = BER::encode_oid(1, 3, 6, 1, 4, 1, 9, 9, 109, 1, 1, 1, 1, 2, $i);
147 my $entity = fetch_snmp($session, $oid);
149 next if (!defined($entity));
151 # last-minute load for the given entity
152 $oid = BER::encode_oid(1, 3, 6, 1, 4, 1, 9, 9, 109, 1, 1, 1, 1, 4, $i);
153 my $value = fetch_snmp($session, $oid);
155 $qcpupoll->execute($switch->{'switch'}, $entity, $value)
156 or die "Could not exec qcpupoll";
162 mylog("ERROR: $@ (during poll of $ip)");
166 my $elapsed = Time::HiRes::tv_interval($start);
167 $msg = sprintf "Polled $switch->{'ip'} in %5.3f seconds.", $elapsed;
170 $qunlock->execute($switch->{'switch'})
171 or die "Couldn't unlock switch";
176 my ($session, $port, $out, $wide_counters) = @_;
179 if ($wide_counters) {
181 $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 10, $port); # interfaces.ifTable.ifEntry.ifHCOutOctets
183 $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 6, $port); # interfaces.ifTable.ifEntry.ifHCInOctets
187 $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 2, 2, 1, 16, $port); # interfaces.ifTable.ifEntry.ifOutOctets
189 $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 2, 2, 1, 10, $port); # interfaces.ifTable.ifEntry.ifInOctets
193 return fetch_snmp($session, $oid);
197 my ($session, $oid) = @_;
199 if ($session->get_request_response($oid)) {
200 my ($bindings) = $session->decode_get_response ($session->{pdu_buffer});
202 while ($bindings ne '') {
203 ($binding,$bindings) = &decode_sequence ($bindings);
204 my ($oid,$value) = &decode_by_template ($binding, "%O%@");
205 return BER::pretty_print($value);
208 die "Couldn't get info from switch";
213 my @ranges = split /,/, $in;
216 for my $range (@ranges) {
217 if ($range =~ /^\d+$/) {
219 } elsif ($range =~ /^(\d+)-(\d+)$/) {
224 die "Couldn't understand '$range' in ports";
228 return (sort { $a <=> $b } @ret);
233 my $time = POSIX::ctime(time);
235 printf STDERR "[%s] %s\n", $time, $msg;
239 my ($cmd, $conn) = @_;
241 # Send the command and get data from switch
242 # $conn->dump_log(*STDOUT);
243 my @data = $conn->cmd($cmd);
245 foreach my $line (@data) {
246 # Remove escape-7 sequence
247 $line =~ s/\x1b\x37//g;
257 my $conn = new Net::Telnet( Timeout => $timeout,
258 Dump_Log => '/tmp/dumplog-tempfetch',
260 Prompt => '/es-3024|e(\-)?\d+\-\dsw>/i');
261 my $ret = $conn->open( Host => $ip);
262 if (!$ret || $ret != 1) {
265 # XXX: Just send the password as text, I did not figure out how to
266 # handle authentication with only password through $conn->login().
267 #$conn->login( Prompt => '/password[: ]*$/i',
269 # Password => $password);
270 my @data = $conn->cmd($password);