Преглед изворни кода

Merge branch 'main' of https://github.com/rajven/Eye

Roman Dmitriev пре 1 година
родитељ
комит
b11126ad39

+ 7 - 7
scripts/eyelib/cmd.pm

@@ -257,7 +257,7 @@ return 1;
 sub netdev_login {
 my $device = shift;
 #skip unknown vendor
-if (!$switch_auth{$device->{vendor_id}}) { return; }
+if (!$switch_auth{$device->{vendor_id}}) { return 0; }
 
 my $t;
 
@@ -272,7 +272,7 @@ if ($device->{proto} eq 'telnet') {
     if ($device->{vendor_id} eq '4') {
         eval {
             my $t1 = new Net::Telnet (Timeout => 5, Port => $device->{port}, Max_buffer_length=>10240000, Prompt =>"/$switch_auth{$device->{vendor_id}}{prompt}/");
-            $t1->open($device->{ip}) or return;
+            $t1->open($device->{ip}) or return 0;
             if (exists $switch_auth{$device->{vendor_id}}{login}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{login}/"); }
             $t1->print($device->{login});
             if (exists $switch_auth{$device->{vendor_id}}{password}) { $t1->waitfor("/$switch_auth{$device->{vendor_id}}{password}/"); }
@@ -314,7 +314,7 @@ if ($device->{proto} eq 'telnet') {
             log_cmd($t,"disable clipaging");
             }
         };
-    if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); } else { log_info("Login to $device->{device_name} ip: $device->{ip} by telnet success!"); }
+    if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); return 0; } else { log_info("Login to $device->{device_name} ip: $device->{ip} by telnet success!"); }
     }
 
 if ($device->{proto} eq 'ssh') {
@@ -335,7 +335,7 @@ if ($device->{proto} eq 'ssh') {
 	    ]
 	    );
 
-        if ($t->error) {  log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); }
+        if ($t->error) {  log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: ".$t->error); return 0; }
 
         netdev_set_enable($t,$device);
 
@@ -349,9 +349,9 @@ if ($device->{proto} eq 'ssh') {
         if ($device->{vendor_id} eq '6') {
             $t->capture("terminal length 0");
             }
-        if ($device->{vendor_id} eq '9') {
-            $t->capture("/system note set show-at-login=no");
-            }
+#        if ($device->{vendor_id} eq '9') {
+#            $t->capture("/system note set show-at-login=no");
+#            }
         if ($device->{vendor_id} eq '16') {
             $t->capture("terminal width 0");
             }

+ 1 - 1
scripts/eyelib/config.pm

@@ -180,7 +180,7 @@ chomp($HOSTNAME1);
 our $HOSTNAME=$HOSTNAME1;
 
 ### debug
-our $debug=1;
+our $debug=0;
 
 our $log_enable = 1;
 

+ 1 - 1
scripts/hmonitor.pl

@@ -172,7 +172,7 @@ if ($hoststate ne $old_state) {
             db_log_info($hdb,"Event handler $nagios_handler for $hostname [$hostip] => $hoststate found!");
             if ($nagios_handler=~/restart-port/i) {
                     my $run_cmd = $HOME_DIR."/restart_port_snmp.pl $hostip & ";
-                    db_log_info($hdb,"Nagios eventhandler restart-port started for ip: $hostip");
+                    db_log_info($hdb,"Nagios eventhandler restart-port started for $hostname [".$hostip."]");
                     db_log_info($hdb,"Run handler: $run_cmd");
                     system($run_cmd);
                     }

+ 11 - 10
scripts/restart_port_snmp.pl

@@ -38,15 +38,14 @@ my $HOST_IP = $ARGV[0];
 
 my $IP_ATON=StrToIp($HOST_IP);
 
-my $auth_id = get_id_record($dbh,'User_auth',"deleted=0 and ip_int='".$IP_ATON."'");
-if (!$auth_id) { db_log_error("Record with ip $HOST_IP not found! Bye."); exit; }
+my $auth_rec = get_record_sql($dbh,'SELECT * FROM User_auth WHERE deleted=0 and ip_int='.$IP_ATON);
+if (!$auth_rec) { db_log_error("Record with ip $HOST_IP not found! Bye."); exit; }
 
-my $ip;
-my $model;
-my $port;
-my $snmp_index;
-my $community;
-my $snmp_version;
+my $auth_id = $auth_rec->{id};
+my $auth_name = $auth_rec->{dns_name};
+
+my $auth_ident = $HOST_IP;
+if ($auth_name) { $auth_ident = $auth_name."[".$HOST_IP."]"; }
 
 my $d_sql="SELECT D.ip, D.device_name, D.vendor_id, D.device_model_id, DP.port, DP.snmp_index, D.rw_community, D.snmp_version  FROM devices AS D, device_ports AS DP, connections AS C WHERE D.snmp_version>0 and D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=$auth_id AND DP.uplink=0";
 
@@ -55,7 +54,9 @@ my $dev_port = get_record_sql($dbh,$d_sql);
 if (!$dev_port) { db_log_error($dbh,"Connection for $HOST_IP not found! Bye."); exit; }
 
 my $ip=$dev_port->{ip};
-my $model=$dev_port->{device_model_id};
+my $model_id=$dev_port->{device_model_id};
+my $model_rec = get_record_sql($dbh,'SELECT model_name FROM device_models WHERE id='.$model_id);
+my $model = $model_rec->{model_name};
 my $port=$dev_port->{port};
 my $vendor_id = $dev_port->{vendor_id};
 my $snmp_index=$dev_port->{snmp_index};
@@ -63,7 +64,7 @@ my $community=$dev_port->{rw_community};
 my $snmp_version=$dev_port->{snmp_version};
 my $device_name = $dev_port->{device_name};
 
-db_log_warning($dbh,"Restart $HOST_IP at $device_name ($model $ip) [$port] request found. Try.");
+db_log_warning($dbh,"Restart $auth_ident at $device_name ($model $ip) [$port] request found. Try.");
 
 my $poe_oid;
 my $admin_oid;

+ 46 - 26
scripts/sync_mikrotik.pl

@@ -79,6 +79,19 @@ next if (!$gate);
 $pm->start and next;
 $dbh = init_db();
 
+my @cmd_list=();
+
+$gate = netdev_set_auth($gate);
+$gate->{login}.='+ct400w';
+my $t = netdev_login($gate);
+
+if (!$t) {
+    log_error("Login to $gate->{device_name} [$gate->{ip}] failed! Skip gateway.");
+    $dbh->disconnect();
+    $pm->finish;
+    next;
+    }
+
 my $router_name=$gate->{device_name};
 my $router_ip=$gate->{ip};
 my $shaper_enabled = $gate->{queue_enabled};
@@ -104,12 +117,6 @@ if (@gw_subnets and scalar @gw_subnets) {
     }
 }
 
-my @cmd_list=();
-
-$gate = netdev_set_auth($gate);
-$gate->{login}.='+ct400w';
-my $t = netdev_login($gate);
-
 foreach my $int (@lan_int) { #interface dhcp loop
 next if (!$int);
 $int=trim($int);
@@ -412,8 +419,18 @@ do_sql($dbh,"DELETE FROM Group_filters WHERE filter_id NOT IN (SELECT id FROM Fi
 my @grouplist_ref = get_records_sql($dbh,"SELECT `group_id`,`filter_id`,`order`,`action` FROM Group_filters ORDER BY Group_filters.group_id,Group_filters.order");
 
 my %group_filters;
-my $index=0;
+my $index = 0;
+my $cur_group;
+
 foreach my $row (@grouplist_ref) {
+
+    if (!$cur_group) { $cur_group = $row->{group_id}; }
+
+    if ($cur_group != $row->{group_id}) {
+        $index = 0;
+        $cur_group = $row->{group_id};
+        }
+
     #if dst dns filter not found
     if (!$filters{$row->{filter_id}}->{dns_dst}) {
         $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$row->{filter_id};
@@ -421,16 +438,16 @@ foreach my $row (@grouplist_ref) {
         $index++;
     } else {
         #if found dns dst filters - add
-	    if (exists $dyn_filters{$row->{filter_id}}) {
+	if (exists $dyn_filters{$row->{filter_id}}) {
 	        my @dyn_ips = @{$dyn_filters{$row->{filter_id}}};
 	        if (scalar @dyn_ips >0) {
 		        for (my $i = 0; $i < scalar @dyn_ips; $i++) {
-        	        $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$dyn_ips[$i];
-                    $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
-        	        $index++;
-        	    }
+                            $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$dyn_ips[$i];
+                            $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
+                            $index++;
+        	        }
 	        }
-        }
+            }
     }
 }
 
@@ -513,18 +530,28 @@ foreach my $group_name (keys %group_filters) {
 }
 
 my %chain_rules;
-foreach my $group_name (keys %group_filters) {
+foreach my $group_name (sort keys %group_filters) {
+
 next if (!$group_name);
+
 next if (!exists($group_filters{$group_name}));
-foreach my $filter_index (sort keys %{$group_filters{$group_name}}) {
-    my $filter_id=$group_filters{$group_name}->{$filter_index}->{filter_id};
+
+my %group_filter = %{$group_filters{$group_name}};
+
+foreach my $filter_index (sort keys %group_filter) {
+
+    my $filter = $group_filter{$filter_index};
+
+    my $filter_id=$filter->{filter_id};
+
     next if (!$filters{$filter_id});
+
     next if ($filters{$filter_id}->{dns_dst});
 
     my $src_rule='chain='.$group_name;
     my $dst_rule='chain='.$group_name;
 
-    if ($group_filters{$group_name}->{$filter_index}->{action}) {
+    if ($filter->{action}) {
 	$src_rule=$src_rule." action=accept";
 	$dst_rule=$dst_rule." action=accept";
 	} else {
@@ -570,18 +597,13 @@ foreach my $filter_index (sort keys %{$group_filters{$group_name}}) {
     }
 }
 
-
 #chain filters
-foreach my $group_name (keys %group_filters) {
-
+foreach my $group_name (sort keys %group_filters) {
 next if (!$group_name);
-
 my @get_filter=netdev_cmd($gate,$t,$gate->{proto},'/ip firewall filter print terse without-paging where chain='.$group_name,1);
 chomp(@get_filter);
-
 my @cur_filter=();
 my $chain_ok=1;
-
 foreach (my $f_index=0; $f_index<scalar(@get_filter); $f_index++) {
     my $filter_str=trim($get_filter[$f_index]);
     next if (!$filter_str);
@@ -592,7 +614,6 @@ foreach (my $f_index=0; $f_index<scalar(@get_filter); $f_index++) {
     next if (!$filter_str);
     push(@cur_filter,$filter_str);
 }
-
 log_debug("Current filters:".Dumper(\@cur_filter));
 log_debug("New filters:".Dumper($chain_rules{$group_name}));
 
@@ -605,7 +626,6 @@ foreach (my $f_index=0; $f_index<scalar(@cur_filter); $f_index++) {
 	last;
 	}
     }
-
 #new rules
 if ($chain_ok and $chain_rules{$group_name} and scalar(@{$chain_rules{$group_name}})) {
     foreach (my $f_index=0; $f_index<scalar(@{$chain_rules{$group_name}}); $f_index++) {
@@ -851,7 +871,7 @@ if (scalar(@cmd_list)) {
     };
     if ($@) {
         $all_ok = 0;
-	    log_debug("Error programming gateway! Err: ".$@);
+	log_debug("Error programming gateway! Err: ".$@);
         }
     }
 

+ 17 - 8
scripts/sync_mikrotik_poe_monitor.pl

@@ -40,12 +40,17 @@ my $switch_ip=$device->{ip};
 
 my @cmd_list=();
 
-my @auth_list = get_records_sql($dbh,"SELECT DP.port,AU.ip FROM `device_ports` AS DP, `User_auth` as AU, `connections` as C WHERE DP.id=C.port_id and C.auth_id=AU.id and AU.deleted=0 and AU.link_check=1 and AU.nagios=1 and C.device_id=".$device->{id}."");
+my @auth_list = get_records_sql($dbh,"SELECT DP.port,AU.ip,AU.dns_name FROM `device_ports` AS DP, `User_auth` as AU, `connections` as C WHERE DP.id=C.port_id and C.auth_id=AU.id and AU.deleted=0 and AU.nagios=1 and C.device_id=".$device->{id}."");
 
 my %work_list;
 foreach my $auth (@auth_list) {
 next if (!$auth);
-$work_list{'ether'.$auth->{port}}=$auth->{ip};
+$work_list{'ether'.$auth->{port}}{ip}=$auth->{ip};
+if ($auth->{dns_name}) {
+    $work_list{'ether'.$auth->{port}}{dns_name}=$auth->{dns_name};
+    } else {
+    $work_list{'ether'.$auth->{port}}{dns_name}=$auth->{ip};
+    }
 }
 
 $device = netdev_set_auth($device);
@@ -82,20 +87,23 @@ $current_list{$port_name}=$ping_address;
 
 foreach my $current_port (keys %current_list) {
 if (defined $work_list{$current_port}) {
-    if ($work_list{$current_port} ne $current_list{$current_port}) {
-        db_log_info($dbh,"Change settings poe monitor at $switch_name [$current_port] to ip: $work_list{$current_port}");
-	push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-address='.$work_list{$current_port}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=3m'); 
-	}
+    if ($work_list{$current_port}{ip} ne $current_list{$current_port}) {
+        db_log_info($dbh,"Change settings poe monitor at $switch_name [$current_port] to ip: $work_list{$current_port}{ip}");
+        push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-address='.$work_list{$current_port}{ip}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=5m'); 
+        push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] comment='.$work_list{$current_port}{dns_name}); 
+        }
     } else {
     db_log_info($dbh,"Disable poe monitor at $switch_name [$current_port]");
     push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] power-cycle-ping-enabled=no');
+    push(@cmd_list,'/interface ethernet set [ find default-name='.$current_port.' ] comment=""'); 
     }
 }
 
 foreach my $work_port (keys %work_list) {
 if (!defined $current_list{$work_port}) {
-    db_log_info($dbh,"Enable poe monitor at $switch_name [$work_port] for $work_list{$work_port}");
-    push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] power-cycle-ping-address='.$work_list{$work_port}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=3m');
+    db_log_info($dbh,"Enable poe monitor at $switch_name [$work_port] for $work_list{$work_port}{ip}");
+    push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] power-cycle-ping-address='.$work_list{$work_port}{ip}.' power-cycle-ping-enabled=yes power-cycle-ping-timeout=5m');
+    push(@cmd_list,'/interface ethernet set [ find default-name='.$work_port.' ] comment='.$work_list{$work_port}{dns_name});
     }
 }
 
@@ -112,3 +120,4 @@ $dbh->disconnect();
 if (IsMyPID($SPID)) { Remove_PID($SPID); };
 
 do_exit 0;
+

+ 1 - 0
scripts/updates/2-6-2/version.sql

@@ -0,0 +1 @@
+REPLACE INTO `version` (`version`) VALUES ('2.6.2');

+ 2 - 1
scripts/updates/upgrade.pl

@@ -33,12 +33,13 @@ my @old_releases = (
 '2.5.2',
 '2.5.3',
 '2.6.1',
+'2.6.2',
 );
 
 my $r_index = 0;
 my %old_releases_h = map {$_ => $r_index++ } @old_releases;
 
-my $eye_release = '2.6.1';
+my $eye_release = '2.6.2';
 
 if (!$config_ref{version}) { $config_ref{version}='2.4.12'; }