Kaynağa Gözat

fix log dhcp events

Roman Dmitriev 2 yıl önce
ebeveyn
işleme
c5e7c8f6d4

+ 18 - 16
docs/mysql/mysql.sql

@@ -439,9 +439,11 @@ CREATE TABLE `dhcp_log` (
   `auth_id` bigint(20) UNSIGNED NOT NULL,
   `dhcp_hostname` varchar(250) DEFAULT NULL,
   `circuit-id` varchar(255) DEFAULT NULL,
-  `remote-id` varchar(255) DEFAULT NULL
+  `remote-id` varchar(255) DEFAULT NULL,
+  `client-id` varchar(250) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
+
 -- --------------------------------------------------------
 
 --
@@ -48871,33 +48873,33 @@ CREATE TABLE `User_auth` (
   `id` int(11) NOT NULL,
   `user_id` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
   `ou_id` int(11) DEFAULT NULL,
-  `ip` varchar(18) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
+  `ip` varchar(18) NOT NULL DEFAULT '',
   `ip_int` bigint(10) UNSIGNED NOT NULL DEFAULT 0,
   `save_traf` tinyint(1) NOT NULL DEFAULT 0,
   `enabled` tinyint(1) NOT NULL DEFAULT 0,
   `dhcp` tinyint(1) NOT NULL DEFAULT 1,
+  `timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
+  `client-id` varchar(250) DEFAULT NULL,
+  `nagios` tinyint(1) NOT NULL DEFAULT 0,
+  `nagios_status` varchar(10) NOT NULL DEFAULT 'UP',
   `filter_group_id` tinyint(1) NOT NULL DEFAULT 0,
   `deleted` tinyint(4) NOT NULL DEFAULT 0,
-  `comments` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
-  `dns_name` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
-  `WikiName` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
-  `dhcp_acl` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+  `comments` text DEFAULT NULL,
+  `dns_name` varchar(60) NOT NULL DEFAULT '',
+  `WikiName` varchar(250) DEFAULT NULL,
+  `dhcp_acl` text DEFAULT NULL,
   `queue_id` int(11) NOT NULL DEFAULT 0,
-  `mac` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
-  `dhcp_action` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
+  `mac` varchar(20) NOT NULL DEFAULT '',
+  `dhcp_action` varchar(10) DEFAULT '',
   `dhcp_time` datetime NOT NULL DEFAULT current_timestamp(),
-  `dhcp_hostname` varchar(60) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
-  `last_found` datetime NOT NULL DEFAULT current_timestamp(),
+  `dhcp_hostname` varchar(60) DEFAULT NULL,
+  `last_found` datetime DEFAULT current_timestamp(),
   `blocked` tinyint(1) NOT NULL DEFAULT 0,
   `day_quota` int(11) NOT NULL DEFAULT 0,
   `month_quota` int(11) NOT NULL DEFAULT 0,
   `device_model_id` int(11) DEFAULT 87,
-  `firmware` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
-  `timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
-  `clientid` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
-  `nagios` tinyint(1) NOT NULL DEFAULT 0,
-  `nagios_status` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
-  `nagios_handler` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
+  `firmware` varchar(100) DEFAULT NULL,
+  `nagios_handler` varchar(50) DEFAULT '',
   `link_check` tinyint(1) NOT NULL DEFAULT 0,
   `changed` tinyint(1) NOT NULL DEFAULT 0,
   `dhcp_changed` int(11) DEFAULT NULL,

+ 1 - 8
html/inc/common.php

@@ -2945,19 +2945,12 @@ function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community
     return;
 }
 
-function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community, $version, $state)
+function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community='public', $version='2', $state)
 {
     if (!isset($ip)) {
         return;
     }
 
-    if (!isset($community)) {
-        $community = 'public';
-    }
-    if (!isset($version)) {
-        $version = '2';
-    }
-
     //default poe status
     $poe_enable = 1;
     $poe_disable = 2;

+ 8 - 8
scripts/Rstat/mysql.pm

@@ -841,7 +841,7 @@ my $ip_record = shift;
 my $ip = $ip_record->{ip};
 my $mac = $ip_record->{mac};
 my $action = $ip_record->{type};
-my $hostname = $ip_record->{hostname};
+my $hostname = $ip_record->{hostname_utf8};
 
 if (!exists $ip_record->{ip_aton}) { $ip_record->{ip_aton}=StrToIp($ip); }
 if (!exists $ip_record->{hotspot}) { $ip_record->{hotspot}=is_hotspot($db,$ip); }
@@ -859,13 +859,13 @@ $new_record->{last_found}=$timestamp;
 if ($record->{user_id}) {
     #update timestamp and return
     if ($action!~/arp/i) {
-	$new_record->{dhcp_action}=$action;
-	$new_record->{dhcp_time}=$timestamp;
-	if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
-	update_record($db,'User_auth',$new_record,"id=$record->{id}");
-	} else {
-	update_record($db,'User_auth',$new_record,"id=$record->{id}");
-	}
+	    $new_record->{dhcp_action}=$action;
+	    $new_record->{dhcp_time}=$timestamp;
+	    if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
+	    update_record($db,'User_auth',$new_record,"id=$record->{id}");
+	    } else {
+	    update_record($db,'User_auth',$new_record,"id=$record->{id}");
+	    }
     return $record->{id};
     }
 

+ 42 - 17
scripts/dhcp-log.pl

@@ -106,7 +106,7 @@ if (!$pid) {
             chomp($logline);
 
             log_verbose("GET CLIENT REQUEST: $logline");
-            my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname,$circut_id,$remote_id) = split (/\;/, $logline);
+            my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname,$circut_id,$remote_id,$client_id) = split (/\;/, $logline);
             next if (!$type);
             next if ($type!~/(old|add|del)/i);
 
@@ -146,6 +146,7 @@ if (!$pid) {
             $dhcp_record->{'timestamp'} = $timestamp;
             $dhcp_record->{'last_time'} = time();
             $dhcp_record->{'circuit-id'} = $circut_id;
+            $dhcp_record->{'client-id'} = $client_id;
             $dhcp_record->{'remote-id'} = $remote_id;
             $dhcp_record->{'hotspot'}=is_hotspot($dbh,$dhcp_record->{ip});
             $leases{$ip}=$dhcp_record;
@@ -166,12 +167,31 @@ if (!$pid) {
 	        if (!$auth_record and $type eq 'old' ) { $type='add'; }
 
             if ($type eq 'add') {
-                my $res_id = resurrection_auth($hdb,$dhcp_record);
-                next if (!$res_id);
-                $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
-                db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
-                } else { $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE ip="'.$dhcp_record->{ip}.'" and mac="'.$mac.'" and deleted=0 ORDER BY last_found DESC'); }
+                    my $res_id = resurrection_auth($hdb,$dhcp_record);
+                    if (!$res_id) {
+                        db_log_error($hdb,"Error creating an ip address record for ip=".$dhcp_record->{ip}." and mac=".$mac."!");
+                        next;
+                        }
+                    $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
+                    db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
+                } else {
+                    $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE ip="'.$dhcp_record->{ip}.'" and mac="'.$mac.'" and deleted=0 ORDER BY last_found DESC'); 
+                }
 
+            #create new record for refresh dhcp packet
+            if (!$auth_record) {
+                db_log_warning($hdb,"Record for dhcp request type: ".$type." ip=".$dhcp_record->{ip}." and mac=".$mac." does not exists!");
+                if ($type eq 'old') {
+                    my $res_id = resurrection_auth($hdb,$dhcp_record);
+                    if (!$res_id) {
+                        db_log_error($hdb,"Error creating an ip address record for ip=".$dhcp_record->{ip}." and mac=".$mac."!");
+                        next;
+                        }
+                    $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
+                    db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
+                    } else { next; }
+                }
+                
             my $auth_id = $auth_record->{id};
 	        my $auth_ou_id = $auth_record->{ou_id};
 
@@ -181,7 +201,7 @@ if (!$pid) {
                 my $auth_rec;
                 $auth_rec->{dhcp_hostname} = $dhcp_record->{hostname_utf8};
                 $auth_rec->{dhcp_time}=$dhcp_event_time;
-                db_log_verbose($hdb,"Add lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
+                db_log_verbose($hdb,"Add lease by dhcp event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
                 update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
                 }
 
@@ -193,7 +213,7 @@ if (!$pid) {
                     my $auth_rec;
                     $auth_rec->{dhcp_action}=$type;
                     $auth_rec->{dhcp_time}=$dhcp_event_time;
-                    db_log_verbose($hdb,"Update lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
+                    db_log_verbose($hdb,"Update lease by dhcp event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
                     update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
                 }
 
@@ -201,15 +221,15 @@ if (!$pid) {
                 if ($auth_record->{dhcp_time} =~ /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
                     my $d_time = mktime($6,$5,$4,$3,$2-1,$1-1900);
                     if (time()-$d_time>60 and ($auth_ou_id == $default_user_ou_id or $auth_ou_id==$default_hotspot_ou_id)) {
-                        db_log_info($hdb,"Remove user ip record by dhcp release event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
+                        db_log_info($hdb,"Remove user ip record by dhcp release event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
                         my $auth_rec;
                         $auth_rec->{deleted}="1";
                         $auth_rec->{dhcp_action}=$type;
                         $auth_rec->{dhcp_time}=$dhcp_event_time;
                         update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
                         my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$auth_record->{'user_id'});
-		        if (!$u_count) {
-				delete_record($hdb,"User_list","id=".$auth_record->{'user_id'});
+		                if (!$u_count) {
+				            delete_record($hdb,"User_list","id=".$auth_record->{'user_id'});
 	                        db_log_info($hdb,"Remove dynamic user id: $auth_record->{'user_id'} by dhcp request",$auth_id);
 	                        }
                         }
@@ -218,12 +238,17 @@ if (!$pid) {
 
             my $dhcp_log;
             if (!$auth_id) { $auth_id=0; }
-            $dhcp_log->{auth_id} = $auth_id;
-            $dhcp_log->{ip} = $dhcp_record->{ip};
-            $dhcp_log->{ip_int} = $dhcp_record->{ip_aton};
-            $dhcp_log->{mac} = $dhcp_record->{mac};
-            $dhcp_log->{action} = $type;
-            $dhcp_log->{timestamp} = $dhcp_event_time;
+            $dhcp_log->{'auth_id'} = $auth_id;
+            $dhcp_log->{'ip'} = $dhcp_record->{'ip'};
+            $dhcp_log->{'ip_int'} = $dhcp_record->{'ip_aton'};
+            $dhcp_log->{'mac'} = $dhcp_record->{'mac'};
+            $dhcp_log->{'action'} = $type;
+            $dhcp_log->{'dhcp_hostname'} = $dhcp_record->{'hostname_utf8'};
+            $dhcp_log->{'timestamp'} = $dhcp_event_time;
+            $dhcp_log->{'circuit-id'} = $circut_id;
+            $dhcp_log->{'client-id'} = $client_id;
+            $dhcp_log->{'remote-id'} = $remote_id;
+
             insert_record($hdb,'dhcp_log',$dhcp_log);
             }
         };

+ 1 - 1
scripts/dnsmasq-hook.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
 time=$(date +%s)
-echo "$1;$2;$3;$4;${time};${DNSMASQ_TAGS};${DNSMASQ_SUPPLIED_HOSTNAME};${DNSMASQ_OLD_HOSTNAME};${DNSMASQ_CIRCUIT_ID};${DNSMASQ_REMOTE_ID}" >>/var/log/dhcp.log &
+echo "$1;$2;$3;$4;${time};${DNSMASQ_TAGS};${DNSMASQ_SUPPLIED_HOSTNAME};${DNSMASQ_OLD_HOSTNAME};${DNSMASQ_CIRCUIT_ID};${DNSMASQ_REMOTE_ID};${DNSMASQ_CLIENT_ID}" >>/var/log/dhcp.log &
 
 exit

+ 2 - 0
updates/2-4-12/mysql-devices-patch.sql

@@ -25,3 +25,5 @@ DELETE FROM `config` WHERE `option_id` = 19;
 INSERT INTO `config_options` (`id`, `option_name`, `description.russian`, `description.english`, `uniq`, `type`, `default_value`, `min_value`, `max_value`) VALUES (68, 'config_mode', 'Режим конфигурирования. Скрипт опроса устройств не выполняется.', 'Configuration mode. The device polling script is not running.', '1', 'bool', '0', '0', '1');
 ALTER TABLE `User_auth` ADD `dhcp_changed` INT NULL DEFAULT NULL AFTER `changed`;
 ALTER TABLE `device_ports` ADD `tagged_vlan` VARCHAR(250) NULL DEFAULT NULL AFTER `vlan`, ADD `untagged_vlan` VARCHAR(250) NULL DEFAULT NULL AFTER `tagged_vlan`, ADD `forbidden_vlan` VARCHAR(250) NULL DEFAULT NULL AFTER `untagged_vlan`;
+ALTER TABLE `dhcp_log` ADD `client-id` VARCHAR(250) NULL DEFAULT NULL AFTER `remote-id`;
+ALTER TABLE `User_auth` CHANGE `clientid` `client-id` VARCHAR(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;