Bläddra i källkod

changed display information for restarted devices

Dmitriev Roman 1 år sedan
förälder
incheckning
76b0a8b
3 ändrade filer med 29 tillägg och 19 borttagningar
  1. 1 1
      scripts/hmonitor.pl
  2. 11 10
      scripts/restart_port_snmp.pl
  3. 17 8
      scripts/sync_mikrotik_poe_monitor.pl

+ 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;

+ 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;
+