]> git.sesse.net Git - nms/blobdiff - clients/snmpfetch.pl
Name C&C3.
[nms] / clients / snmpfetch.pl
index fa50f60f487e5769344b4e8ca0d088ff3ab6a773..b5adb9a5ea4f816035831a4a0295963a9e0bcb95 100755 (executable)
@@ -8,11 +8,13 @@ use strict;
 use warnings;
 require 'SNMP_Session.pm';
 
+use lib '../include';
+use nms;
+
 my $password = 'removed';
 my $timeout = 15;
 
-my $dbh = DBI->connect("dbi:Pg:dbname=nms;host=localhost", "nms", "nms")
-       or die "Couldn't connect to database";
+my $dbh = nms::db_connect();
 $dbh->{AutoCommit} = 0;
 
 # normal mode: fetch switches from the database
@@ -47,7 +49,7 @@ my $qlock = $dbh->prepare("UPDATE switches SET locked='t', last_updated=now() WH
        or die "Couldn't prepare qlock";
 my $qunlock = $dbh->prepare("UPDATE switches SET locked='f', last_updated=now() WHERE switch=?")
        or die "Couldn't prepare qunlock";
-my $qpoll = $dbh->prepare("INSERT INTO polls (time, switch, port, bytes_in, bytes_out) VALUES (timeofday()::timestamp,?,?,?,?)")
+my $qpoll = $dbh->prepare("INSERT INTO polls (time, switch, port, bytes_in, bytes_out, errors_in, errors_out) VALUES (timeofday()::timestamp,?,?,?,?,?,?)")
        or die "Couldn't prepare qpoll";
 my $qtemppoll = $dbh->prepare("INSERT INTO temppoll (time, switch, temp) VALUES (timeofday()::timestamp,?::text::int,?::text::float)")
         or die "Couldn't prepare qtemppoll";
@@ -76,7 +78,7 @@ while (1) {
                        exit;
                } else {        
                        mylog("No available switches in pool, sleeping.");
-                       sleep 60;
+                       sleep 15;
                        next;
                }
        }
@@ -125,8 +127,10 @@ while (1) {
                for my $port (@ports) {
                        my $in = fetch_data($session, $port, 0, $switch->{'wide_counters'});
                        my $out = fetch_data($session, $port, 1, $switch->{'wide_counters'});
+                       my $ine = fetch_errors($session, $port, 0);
+                       my $oute = fetch_errors($session, $port, 1);
 
-                       $qpoll->execute($switch->{'switch'}, $port, $in, $out);
+                       $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute);
                }
                 my $conn = switch_connect($ip);
                 if (!defined($conn)) {
@@ -140,7 +144,8 @@ while (1) {
                        print $avgtemp." avgtemp\n";
                        $qtemppoll->execute($switch->{'switch'},
                                        $avgtemp) or die "Could not exec qtemppoll";
-                } elsif ($switch->{'switchtype'} eq 'cisco6509') {
+                } elsif ($switch->{'switchtype'} eq 'cisco6509' && 0) {
+                       # fetch load data for the entities
                        for my $i (1..5) {
                                # find the ID
                                my $oid = BER::encode_oid(1, 3, 6, 1, 4, 1, 9, 9, 109, 1, 1, 1, 1, 2, $i);
@@ -155,6 +160,23 @@ while (1) {
                                $qcpupoll->execute($switch->{'switch'}, $entity, $value)
                                        or die "Could not exec qcpupoll";
                        }
+                       
+                       # fetch load data for the matrix
+                       for my $i ([1,0], [1,1], [2,0], [2,1], [4,0], [5,0], [6,0]) {
+                               # in
+                               my $oid = BER::encode_oid(1, 3, 6, 1, 4, 1, 9, 9, 217, 1, 3, 1, 1, 6, $i->[0], $i->[1]);
+                               my $in_util = fetch_snmp($session, $oid);
+
+                               $qcpupoll->execute($switch->{'switch'}, 100000 + $i->[0] * 10 + $i->[1], $in_util)
+                                       or die "Could not exec qcpupoll";
+                               
+                               # ou
+                               $oid = BER::encode_oid(1, 3, 6, 1, 4, 1, 9, 9, 217, 1, 3, 1, 1, 7, $i->[0], $i->[1]);
+                               my $out_util = fetch_snmp($session, $oid);
+
+                               $qcpupoll->execute($switch->{'switch'}, 200000 + $i->[0] * 10 + $i->[1], $out_util)
+                                       or die "Could not exec qcpupoll";
+                       }
                }
                $session->close;
        };
@@ -192,6 +214,18 @@ sub fetch_data {
 
        return fetch_snmp($session, $oid);
 }
+sub fetch_errors {
+       my ($session, $port, $out) = @_;
+       
+       my $oid;
+       if ($out) {
+               $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 2, 2, 1, 20, $port);     # interfaces.ifTable.ifEntry.ifOutErrors
+       } else {
+               $oid = BER::encode_oid(1, 3, 6, 1, 2, 1, 2, 2, 1, 14, $port);     # interfaces.ifTable.ifEntry.ifInErrors
+       }
+
+       return fetch_snmp($session, $oid);
+}
        
 sub fetch_snmp {
        my ($session, $oid) = @_;
@@ -235,41 +269,41 @@ sub mylog {
        printf STDERR "[%s] %s\n", $time, $msg;
 }
 
-sub switch_exec {
-       my ($cmd, $conn) = @_;
-
-       # Send the command and get data from switch
-#      $conn->dump_log(*STDOUT);
-       my @data = $conn->cmd($cmd);
-       my @lines = ();
-       foreach my $line (@data) {
-               # Remove escape-7 sequence
-               $line =~ s/\x1b\x37//g;
-               push @lines, $line;
-       }
+#sub switch_exec {
+#      my ($cmd, $conn) = @_;
+#
+#      # Send the command and get data from switch
+##     $conn->dump_log(*STDOUT);
+#      my @data = $conn->cmd($cmd);
+#      my @lines = ();
+#      foreach my $line (@data) {
+#              # Remove escape-7 sequence
+#              $line =~ s/\x1b\x37//g;
+#              push @lines, $line;
+#      }
+#
+#      return @lines;
+#}
 
-       return @lines;
-}
-
-sub switch_connect {
-       my ($ip) = @_;
-
-       my $conn = new Net::Telnet(     Timeout => $timeout,
-                                       Dump_Log => '/tmp/dumplog-tempfetch',
-                                       Errmode => 'return',
-                                       Prompt => '/es-3024|e(\-)?\d+\-\dsw>/i');
-       my $ret = $conn->open(  Host => $ip);
-       if (!$ret || $ret != 1) {
-               return (0);
-       }
-       # XXX: Just send the password as text, I did not figure out how to
-       # handle authentication with only password through $conn->login().
-       #$conn->login(  Prompt => '/password[: ]*$/i',
-       #              Name => $password,
-       #              Password => $password);
-       my @data = $conn->cmd($password);
-       # Get rid of banner
-       $conn->get;
-       return $conn;
-}
+#sub switch_connect {
+#      my ($ip) = @_;
+#
+#      my $conn = new Net::Telnet(     Timeout => $timeout,
+#                                      Dump_Log => '/tmp/dumplog-tempfetch',
+#                                      Errmode => 'return',
+#                                      Prompt => '/es-3024|e(\-)?\d+\-\dsw>/i');
+#      my $ret = $conn->open(  Host => $ip);
+#      if (!$ret || $ret != 1) {
+#              return (0);
+#      }
+#      # XXX: Just send the password as text, I did not figure out how to
+#      # handle authentication with only password through $conn->login().
+#      #$conn->login(  Prompt => '/password[: ]*$/i',
+#      #              Name => $password,
+#      #              Password => $password);
+#      my @data = $conn->cmd($password);
+#      # Get rid of banner
+#      $conn->get;
+#      return $conn;
+#}