Procházet zdrojové kódy

discovery: skip check down host
discovery: add timeout for snmp requests 15 s.

rajven před 4 roky
rodič
revize
d1560fcbf6
3 změnil soubory, kde provedl 22 přidání a 18 odebrání
  1. 4 2
      scripts/Rstat/net_utils.pm
  2. 13 14
      scripts/Rstat/snmp.pm
  3. 5 2
      scripts/fetch_new_arp.pl

+ 4 - 2
scripts/Rstat/net_utils.pm

@@ -293,8 +293,10 @@ $ret ? return 1: return 0;
 
 sub HostIsLive {
 my $host=shift;
-my $p = Net::Ping->new("tcp",1,1);
-my $ok= $p->ping($host);
+my $proto=shift || "tcp";
+if ($< eq 0) { $proto="icmp"; }
+my $p = Net::Ping->new($proto);
+my $ok= $p->ping($host,5);
 $p->close();
 return $ok;
 }

+ 13 - 14
scripts/Rstat/snmp.pm

@@ -44,7 +44,7 @@ $fdb_table_oid2
 $cisco_vlan_oid
 $port_vlan_oid
 $fdb_table;
-$timeout
+$snmp_timeout
 );
 
 
@@ -64,7 +64,7 @@ our $fdb_table_oid2='.1.3.6.1.2.1.17.7.1.2.2.1.2';
 our $port_vlan_oid ='.1.3.6.1.2.1.17.7.1.4.5.1.1';
 our $cisco_vlan_oid='.1.3.6.1.4.1.9.9.46.1.3.1.1.2';
 our $fdb_table;
-our $timeout = 5;
+our $snmp_timeout = 15;
 
 #---------------------------------------------------------------------------------
 
@@ -78,7 +78,8 @@ my ($session, $error) = Net::SNMP->session(
    -hostname  => $ip,
    -community => $community,
    -port      => $port,
-   -version   => $snmp_version
+   -version   => $snmp_version,
+   -timeout   => $snmp_timeout
 );
 my $result = $session->get_request( -varbindlist => [$oid]);
 $session->close;
@@ -100,7 +101,8 @@ my ($session, $error) = Net::SNMP->session(
    -hostname  => $ip,
    -community => $community,
    -port      => $port,
-   -version   => $snmp_version
+   -version   => $snmp_version,
+   -timeout   => $snmp_timeout
 );
 my $result = $session->set_request( -varbindlist => [$oid,INTEGER,$value]);
 $session->close;
@@ -117,7 +119,7 @@ sub get_arp_table {
     if (!$version) { $version='2'; }
 
     ### open SNMP session
-    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version);
+    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version, -timeout   => $snmp_timeout);
     return if (!defined($snmp_session));
     $snmp_session->translate([-all]);
 
@@ -268,9 +270,8 @@ sub get_interfaces {
     my ($host,$community,$snmp,$skip_empty) = @_;
 #    return if (!HostIsLive($host));
     my $port = 161;
-    my $timeout = 5;
     ### open SNMP session
-    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community );
+    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
     return if (!defined($snmp_session));
     $snmp_session->translate([-timeticks]);
     my $if_name = $snmp_session->get_table($ifName);
@@ -307,9 +308,8 @@ sub get_router_state {
     my ($host,$community,$snmp,$skip_empty) = @_;
 #    return if (!HostIsLive($host));
     my $port = 161;
-    my $timeout = 5;
     ### open SNMP session
-    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community );
+    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
     return if (!defined($snmp_session));
     $snmp_session->translate([-timeticks]);
     my $router_status = $snmp_session->get_table("1.3.6.1.4.1.10.1");
@@ -322,9 +322,8 @@ sub get_bgp {
     my ($host,$community,$snmp) = @_;
     return if (!HostIsLive($host));
     my $port = 161;
-    my $timeout = 5;
     ### open SNMP session
-    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community );
+    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community, -version => $snmp, -timeout => $snmp_timeout );
     return if (!defined($snmp_session));
     $snmp_session->translate([-timeticks]);
     #bgp annonce counter exists?
@@ -349,7 +348,7 @@ my ($host,$community,$oid,$version) = @_;
 #return if (!HostIsLive($host));
 if (!$version) { $version='2'; }
 ### open SNMP session
-my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version , -timeout     => $timeout, );
+my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version, -timeout => $snmp_timeout );
 return if (!defined($snmp_session));
 $snmp_session->translate([-timeticks]);
 my $result = $snmp_session->get_request(-varbindlist => [$oid]) or return;
@@ -364,7 +363,7 @@ my ($host,$community,$oid,$version) = @_;
 #return if (!HostIsLive($host));
 if (!$version) { $version='2'; }
 ### open SNMP session
-my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version , -timeout     => $timeout, );
+my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community , -version=>$version , -timeout     => $snmp_timeout, );
 return if (!defined($snmp_session));
 $snmp_session->translate([-timeticks]);
 my $table = $snmp_session->get_table($oid);
@@ -389,7 +388,7 @@ my ($session, $error) = Net::SNMP->session(
       -nonblocking => 1,
       -translate   => [-octetstring => 0],
       -version     => $version,
-      -timeout     => $timeout,
+      -timeout     => $snmp_timeout,
    );
 
 if (!defined $session) {

+ 5 - 2
scripts/fetch_new_arp.pl

@@ -71,8 +71,9 @@ my $pm_arp = Parallel::ForkManager->new($fork_count);
 $pm_arp -> run_on_finish (
 sub {
     my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
-    if (defined($data_structure_reference)) {  # children are not forced to send anything
-        my $result = ${$data_structure_reference};  # child passed a string reference
+    if (defined($data_structure_reference)) {
+        # children are not forced to send anything
+        my $result = ${$data_structure_reference};
         push(@arp_array,$result);
         }
     }
@@ -83,6 +84,7 @@ foreach my $router (@router_ref) {
 my $router_ip=$router->{ip};
 my $snmp_version=$router->{snmp_version};
 my $community=$router->{community};
+if (!HostIsLive($router_ip)) { log_info("Host id: $router->{id} name: $router->{device_name} ip: $router_ip is down! Skip."); next; }
 $pm_arp->start() and next DATA_LOOP;
 my $arp_table=get_arp_table($router_ip,$community,$snmp_version);
 $pm_arp->finish(0, \$arp_table);
@@ -199,6 +201,7 @@ sub {
 
 FDB_LOOP:
 foreach my $device (@device_list) {
+if (!HostIsLive($device->{ip})) { log_info("Host id: $device->{id} name: $device->{device_name} ip: $device->{ip} is down! Skip."); next; }
 $pm_fdb->start() and next FDB_LOOP;
 my $fdb=get_fdb_table($device->{ip},$device->{community},$device->{snmp_version});
 my $vlans = get_switch_vlans($device->{ip},$device->{community},$device->{snmp_version});