Просмотр исходного кода

- fixed Experimental warning for old perl
- support both mac-table for bugged switches

root 4 лет назад
Родитель
Сommit
1848eb982b

+ 28 - 8
html/inc/common.php

@@ -1802,10 +1802,20 @@ function get_fdb_port_table($ip, $port_index, $community, $version)
         $version = '2';
     }
 
-    $fdb_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid2);
-    if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
-        $fdb_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid);
-    }
+    $fdb1_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid2);
+    $fdb2_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid);
+
+    if (!empty($fdb1_port_table)) { $fdb_port_table = $fdb1_port_table; }
+    if (!empty($fdb2_port_table)) {
+        if (empty($fdb_port_table)) {
+            $fdb_port_table = $fdb2_port_table;
+            } else {
+            foreach ($fdb2_port_table as $mac => $port) {
+                if (empty($fdb_port_table[$mac])) { $fdb_port_table[$mac]=$port; }
+                }
+            }
+        }
+
     // maybe cisco?!
     if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
         global $cisco_vlan_oid;
@@ -1882,10 +1892,20 @@ function get_fdb_table($ip, $community, $version)
         $version = '2';
     }
 
-    $fdb_port_table = get_mac_table($ip, $community, $version, $mac_table_oid2);
-    if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
-        $fdb_port_table = get_mac_table($ip, $community, $version, $mac_table_oid);
-    }
+    $fdb1_port_table = get_mac_table($ip, $community, $version, $mac_table_oid2);
+    $fdb2_port_table = get_mac_table($ip, $community, $version, $mac_table_oid);
+
+    if (!empty($fdb1_port_table)) { $fdb_port_table = $fdb1_port_table; }
+    if (!empty($fdb2_port_table)) {
+        if (empty($fdb_port_table)) {
+            $fdb_port_table = $fdb2_port_table;
+            } else {
+            foreach ($fdb2_port_table as $mac => $port) {
+                if (empty($fdb_port_table[$mac])) { $fdb_port_table[$mac]=$port; }
+                }
+            }
+        }
+
     // maybe cisco?!
     if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
         global $cisco_vlan_oid;

+ 1 - 1
scripts/Rstat/cmd.pm

@@ -6,8 +6,8 @@ package Rstat::cmd;
 
 use utf8;
 use strict;
-no warnings qw( experimental);
 use English;
+no if $] >= 5.018, warnings =>  "experimental::smartmatch";
 use FindBin '$Bin';
 use lib "$Bin";
 use base 'Exporter';

+ 0 - 1
scripts/Rstat/config.pm

@@ -7,7 +7,6 @@ package Rstat::config;
 use utf8;
 use strict;
 use English;
-no warnings qw( experimental);
 use FindBin '$Bin';
 use lib "$Bin";
 use base 'Exporter';

+ 0 - 1
scripts/Rstat/main.pm

@@ -7,7 +7,6 @@ package Rstat::main;
 use utf8;
 use strict;
 use English;
-no warnings qw( experimental);
 use FindBin '$Bin';
 use lib "$Bin";
 use base 'Exporter';

+ 0 - 1
scripts/Rstat/mysql.pm

@@ -7,7 +7,6 @@ package Rstat::mysql;
 use utf8;
 use strict;
 use English;
-no warnings qw( experimental);
 use FindBin '$Bin';
 use lib "$Bin";
 use base 'Exporter';

+ 292 - 0
scripts/Rstat/nagios.pm

@@ -0,0 +1,292 @@
+package Rstat::nagios;
+
+#use v5.28;
+use utf8;
+use open ":encoding(utf8)";
+no if $] >= 5.018, warnings =>  "experimental::smartmatch";
+use strict;
+use English;
+use FindBin '$Bin';
+use lib "$Bin";
+use base 'Exporter';
+use vars qw(@EXPORT @ISA);
+use Rstat::config;
+use Rstat::main;
+use Rstat::mysql;
+use Time::Local;
+use Data::Dumper;
+
+@ISA = qw(Exporter);
+@EXPORT = qw(
+nagios_send_command
+nagios_host_svc_disable
+nagios_host_svc_enable
+print_nagios_cfg
+@cfg_dirs
+);
+
+BEGIN
+{
+
+#---------------------------------------------------------------------------------
+
+sub nagios_send_command {
+my $command = shift;
+next if (!$command);
+if (!-e $config_ref{nagios_cmd}) { die("Command socket $config_ref{nagios_cmd} not found!"); }
+log_info("Send command: $command to $config_ref{nagios_cmd}");
+open(FH, ">> $config_ref{nagios_cmd}");
+print FH "$command\n";
+close(FH);
+}
+
+#---------------------------------------------------------------------------------
+
+sub nagios_host_svc_disable {
+my $hostname = shift;
+my $full = shift || 0;
+my $utime = timelocal(localtime());
+my $cmd = "[$utime] DISABLE_HOST_SVC_CHECKS;$hostname";
+#dont run!!!
+#check nagios option work?
+#nagios_send_command($cmd);
+if ($full) {
+    $cmd = "[$utime] DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST;$hostname";
+    nagios_send_command($cmd);
+    }
+$cmd = "[$utime] DISABLE_HOST_SVC_NOTIFICATIONS;$hostname";
+nagios_send_command($cmd);
+}
+
+#---------------------------------------------------------------------------------
+
+sub nagios_host_svc_enable {
+my $hostname = shift;
+my $full = shift || 0;
+my $utime = timelocal(localtime());
+my $cmd = "[$utime] ENABLE_HOST_SVC_CHECKS;$hostname";
+nagios_send_command($cmd);
+if ($full) {
+    $cmd = "[$utime] ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST;$hostname";
+    nagios_send_command($cmd);
+    }
+$cmd = "[$utime] ENABLE_HOST_SVC_NOTIFICATIONS;$hostname";
+nagios_send_command($cmd);
+}
+
+#---------------------------------------------------------------------------------
+
+sub read_host_template {
+my $device = shift;
+my $template_file = shift;
+my $result;
+my @custom_cfg=();
+if (-e $template_file) { @custom_cfg = read_file($template_file); } else { return; }
+if (@custom_cfg and scalar(@custom_cfg)) {
+    foreach my $row (@custom_cfg) {
+	next if (!$row);
+        $row=~s/\%HOSTNAME\%/$device->{name}/;
+        $row=~s/\%HOST\%/$device->{name}/;
+	$row=~s/\%HOSTIP\%/$device->{ip}/;
+	$row=~s/\%HOST_IP\%/$device->{ip}/;
+	$row=~s/\%COMMUNITY\%/$device->{community}/ if ($device->{community});
+	$row=~s/\%RW_COMMUNITY\%/$device->{rw_community}/ if ($device->{rw_community});
+	$row=~s/\%SNMP_VERSION\%/$device->{snmp_version}/ if ($device->{snmp_version});
+	$row=~s/\%MODEL\%/$device->{device_model}->{model_name}/ if ($device->{device_model}->{model_name});
+        push(@{$result->{template}},$row);
+	if ($row=~/\s+service_description\s+(.*)$/i) { $result->{services}->{$1}=1; }
+	}
+    }
+return $result;
+}
+
+sub print_single_host {
+my $device = shift;
+my $host_template = 'generic-host';
+my $default_service="local-service";
+
+my $ping_enable = $device->{ou}->{nagios_ping};
+if ($device->{ou}->{nagios_host_use}) { $host_template=$device->{ou}->{nagios_host_use}; }
+if ($device->{ou}->{nagios_default_service}) { $default_service=$device->{ou}->{nagios_default_service}; }
+
+my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
+open(FH, "> $cfg_file");
+print(FH "define host{\n");
+print(FH "       use                     $host_template\n");
+print(FH "       host_name               $device->{name}\n");
+print(FH "       alias                   $device->{name}\n");
+print(FH "       address                 $device->{ip}\n");
+print(FH "       _ID			 $device->{auth_id}\n"); 
+print(FH "       _TYPE			 user\n");
+if ($device->{device_model}) {
+	print(FH "       notes		$device->{device_model}->{model_name}\n"); 
+	}
+if ($device->{parent_name}) {
+        print(FH "       parents                    $device->{parent_name}\n");
+        }
+print(FH "       notes_url       ".$config_ref{stat_url}."/admin/users/editauth.php?id=".$device->{auth_id}."\n");
+print(FH "       }\n\n");
+
+if ($ping_enable) {
+	print(FH "define service{\n");
+	print(FH "       use                    ping-service\n");
+	print(FH "       host_name              $device->{name}\n");
+	print(FH "       service_description    ping $device->{name}\n");
+	print(FH "       check_command          check_ping_icmp!100.0,20%!500.0,60%\n");
+	print(FH "       }\n");
+	print(FH "\n");
+    }
+if ($device->{parent_name} and $device->{link_check} and $device->{parent_snmp_version}) {
+	#port status
+        print(FH "define service{\n");
+        print(FH "       use                        $default_service\n");
+        print(FH "       host_name                  $device->{parent_name}\n");
+        print(FH "       service_description port $device->{parent_port} - $device->{name}\n");
+        print(FH "       check_command              check_ifoperstatus!$device->{parent_port_snmp_index}!$device->{parent_community}\n");
+        print(FH "       }\n");
+        print(FH "\n");
+        #crc
+        print(FH "define service{\n");
+        print(FH "       use                        service-snmp-crc\n");
+        print(FH "       host_name                  $device->{parent_name}\n");
+        print(FH "       service_description port $device->{parent_port} - $device->{name} CRC Errors\n");
+        print(FH "       check_command              check_snmp_switch_crc!$device->{parent_community}!$device->{parent_port_snmp_index}\n");
+        print(FH "       }\n\n");
+    }
+close(FH);
+return $cfg_file;
+}
+#---------------------------------------------------------------------------------
+
+sub print_nagios_cfg {
+
+my $device = shift;
+return if (!$device);
+my $device_id = $device->{device_id};
+my $custom_cfg;
+my $device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device->{name}.".cfg";
+if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
+$device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device_id.".cfg";
+if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
+my $default_service="local-service";
+
+if (!$device->{ou}->{nagios_dir}) { print Dumper($device); }
+
+my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
+
+if ($custom_cfg->{template}) {
+    open(FH, "> $cfg_file");
+    my @custom_cfg = @{$custom_cfg->{template}};
+    if (@custom_cfg and scalar(@custom_cfg)) {
+	foreach my $row (@custom_cfg) {
+	    next if (!$row);
+            print(FH $row."\n");
+	    }
+	}
+    close(FH);
+    return;
+    }
+
+#switch | router
+if ($device->{type} ~~ [1,2]) {
+    open(FH, "> $cfg_file");
+    my $device_template = 'switches';
+    if ($device->{type} eq 2) {  $device_template='routers'; }
+    print(FH "define  host {\n");
+    print(FH "       use                     $device_template\n");
+    print(FH "       host_name               $device->{name}\n");
+    print(FH "       alias                   $device->{name}\n");
+    print(FH "       address                 $device->{ip}\n");
+    print(FH "       _ID                     $device->{device_id}\n");
+    print(FH "       _TYPE                   device\n");
+    if ($device->{device_model}) {
+	print(FH "       notes		$device->{device_model}->{model_name}\n");
+	}
+    if ($device->{parent_name}) {
+        print(FH "       parents                    $device->{parent_name}\n");
+        }
+    print(FH "       notes_url       ".$config_ref{stat_url}."/admin/devices/editdevice.php?id=$device->{device_id}\n");
+    print(FH "       }\n\n");
+    #ping
+    print(FH "define service{\n");
+    print(FH "        use                             ping-service         ; Name of service template to use\n");
+    print(FH "        host_name                       $device->{name}\n");
+    print(FH "        service_description             ping $device->{name}\n");
+    print(FH "        check_command                   check_ping_icmp!100.0,20%!500.0,60%\n");
+    print(FH "        }\n");
+    #uptime
+    if ($device->{snmp_version}) {
+        print(FH "define service{\n");
+	print(FH "        use                             $default_service\n");
+        print(FH "        host_name                       $device->{name}\n");
+	print(FH "        service_description             Uptime\n");
+        print(FH "        check_command                   check_snmp_uptime!$device->{community}!161!$device->{snmp_version}\n");
+	print(FH "        }\n");
+        print(FH "\n");
+        #uplink
+        if (exists $device->{uplink}) {
+	    print(FH "define service{\n");
+    	    print(FH "       use                        service-snmp-crc\n");
+            print(FH "       host_name                  $device->{name}\n");
+            my $port_description = $device->{parent_name};
+            my $conn = $device->{uplink};
+            print(FH "       service_description port $conn->{port} - $port_description CRC Errors\n");
+            print(FH "       check_command              check_snmp_switch_crc!$device->{community}!$conn->{snmp_index}\n");
+            print(FH "       }\n\n");
+    	    }
+	foreach my $conn (@{$device->{downlinks}}) {
+	    #id,port,snmp_index,comment
+	    print(FH "define service{\n");
+    	    print(FH "       use                        $default_service\n");
+            print(FH "       host_name                  $device->{name}\n");
+            my $port_description=translit($conn->{comment});
+            if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
+            print(FH "       service_description port $conn->{port} - $port_description \n");
+            print(FH "       check_command              check_ifoperstatus!$conn->{snmp_index}!$device->{community}\n");
+            print(FH "       }\n\n");
+            #src
+	    print(FH "define service{\n");
+    	    print(FH "       use                        service-snmp-crc\n");
+            print(FH "       host_name                  $device->{name}\n");
+            my $port_description=translit($conn->{comment});
+            if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
+            print(FH "       service_description port $conn->{port} - $port_description CRC Errors\n");
+            print(FH "       check_command              check_snmp_switch_crc!$device->{community}!$conn->{snmp_index}\n");
+            print(FH "       }\n\n");
+            #band
+	    print(FH "define service{\n");
+    	    print(FH "       use                        service-snmp-bandwidth\n");
+            print(FH "       host_name                  $device->{name}\n");
+            my $port_description=translit($conn->{comment});
+            if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
+            print(FH "       service_description port $conn->{port} - $port_description bandwidth usage\n");
+            print(FH "       check_command              check_snmp_bandwidth!$device->{community}!$conn->{snmp_index}\n");
+            print(FH "       }\n\n");
+	    }
+	}
+    close FH;
+    }
+
+#auth record
+if ($device->{type} eq 3) {
+    my $cfg_file = print_single_host($device);
+    open(FH, ">> $cfg_file");
+    my $dev_cfg;
+    if ($device->{device_model} and $device->{device_model}->{nagios_template}) { $dev_cfg = read_host_template($device,$config_ref{nagios_dir}.'/gen_template/'.$device->{device_model}->{nagios_template}); }
+    if ($dev_cfg and $dev_cfg->{template}) {
+	    my @dev_cfg = @{$dev_cfg->{template}};
+	    if (@dev_cfg and scalar(@dev_cfg)) {
+		foreach my $row (@dev_cfg) {
+		    next if (!$row);
+    		    print(FH $row."\n");
+		    }
+		}
+        }
+    close FH;
+    }
+}
+
+#---------------------------------------------------------------------------------
+
+1;
+}

+ 0 - 1
scripts/Rstat/net_utils.pm

@@ -6,7 +6,6 @@ package Rstat::net_utils;
 
 use utf8;
 use strict;
-no warnings qw( experimental);
 use English;
 use FindBin qw($Bin);
 use lib "$Bin";

+ 0 - 1
scripts/Rstat/snmp.pm

@@ -8,7 +8,6 @@ use utf8;
 use strict;
 use English;
 use FindBin '$Bin';
-no warnings qw( experimental);
 use lib "$Bin";
 use base 'Exporter';
 use vars qw(@EXPORT @ISA);

+ 1 - 1
scripts/check_dhcp_pool.pl

@@ -8,7 +8,7 @@ use utf8;
 use English;
 use base;
 use FindBin '$Bin';
-no warnings qw( experimental);
+no if $] >= 5.018, warnings =>  "experimental::smartmatch";
 use lib "$Bin/";
 use strict;
 use DBI;

+ 1 - 2
scripts/set_port_descr.pl

@@ -21,8 +21,7 @@ use Rstat::snmp;
 use Rstat::cmd;
 use Net::SNMP qw(:snmp);
 use Fcntl qw(:flock);
-
-no warnings qw( experimental);
+no if $] >= 5.018, warnings =>  "experimental::smartmatch";
 
 
 open(SELF,"<",$0) or die "Cannot open $0 - $!";