Thread SNMP query mittels Perl: SNMP query mittels Perl (7 answers)
Opened by tomi at 2005-11-21 16:05

tomi
 2005-11-21 17:49
#36940 #36940
User since
2005-11-21
3 Artikel
BenutzerIn
[default_avatar]
Hi,

das hab ich schon versucht...gleiches ergebnis :-(

anbei ein funktionstüchtiger code (host und community müssen entsprechend angepasst werden)

vielen dank
tomi


Code: (dl )
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
#!/usr/bin/perl

#use strict;

use SNMP;
use Getopt::Long;

# Nagios specific
#use lib "/opt/nagios/libexec";
#use utils qw(%ERRORS $TIMEOUT);

# prototype
sub checkInterfaces($$$$$$);

$host="cisco-router";
$community = $o_community || "pubic";
$version = $o_version || "1";
$timeout = $o_timeout || "10000000";
$retries = $o_retries || "10";

# create session
($session, $error) = new SNMP::Session(
   DestHost => $host,
   Community => $community,
   Version => $version,
   Timeout => $timeout,
   Retries => $retries);

# if session does not exist ... exit
if (!defined($session)) {
   print "ERROR opening session\n";
   exit $ERRORS{"UNKNOWN"};
}


   # get Number of Interfaces on this Host
   $ifcount = $session->get(["ifNumber",0]);

   # get sysDescr field and check if device is a 375x l3-switch
   #my $sysdescr = $session->get(["sysDescr",0]);

   $num_o_errors = checkInterfaces(0, $ifcount, $session, $version, \@longerr, $host);



sub checkInterfaces($$$$$$) {

   my($index, $ifcount, $border, $version, $session, $host, $longerr, $not_to_search);
   $index = $_[0];
   $ifcount = $_[1];
   $session= $_[2];
   $version = $_[3];
   $longerr = $_[4];
   $host = $_[5];
   my %admin_states = ((my $admin_state_ok=1)=>"adminUp",
                       2=>"adminDown",
                       3=>"adminTest");
   my %oper_states = ((my $oper_state_ok=1)=>"up",
                      2=>"down",
                      3=>"testing",
                      4=>"unknown",
                      5=>"sleeping");
   my $counter = 0;

      my $if_state = new SNMP::VarList(
                                 ["ifDescr"],
                                 ["ifAdminStatus"],
                                 ["ifOperStatus"],
                                 ["ifAlias"],  # .1.3.6.1.2.1.31.1.1.1.18
                                 ["ifName"]); # .1.3.6.1.2.1.31.1.1.1.1
       my $desc;
       my $desc2 = undef;

       do {
           my ($name,$admin,$oper,$desc1,$short) = $session->getnext($if_state);
           $index++;

           $desc = $desc1?$desc1:$desc2?$desc2:"N/A";
           $short = $name if (!$short);

           if ($session->{ErrorNum}) {
               next if ($session->{ErrorNum}==2);
               $counter++;
               push @$longerr, "$host no session: $session->{ErrorStr}";
               #last;
           }
           print "Name: $name, Admin-State: $admin, Oper-State: $oper, Desc: $desc, Shotname: $short\n";
       } while ($index <= $ifcount-1); # -1 to get rid of last...unused...element
   return $counter;


}

View full thread SNMP query mittels Perl: SNMP query mittels Perl