Răsfoiți Sursa

try detect device by dhcp request

Roman Dmitriev 2 ani în urmă
părinte
comite
79f32ed6e1
4 a modificat fișierele cu 23 adăugiri și 10 ștergeri
  1. 1 1
      docs/Readme.en.md
  2. 1 1
      docs/Readme.ru.md
  3. 18 5
      scripts/dhcp-log.pl
  4. 3 3
      scripts/dnsmasq-hook.sh

+ 1 - 1
docs/Readme.en.md

@@ -3,7 +3,7 @@ Installation
 1. Install the packages
 
 apt install apache2 git fping perl mariadb-server php php-mysql php-bcmath php-intl \
-php-mbstring php-date php-mail php-snmp php-zip xxd \
+php-mbstring php-date php-mail php-snmp php-zip xxd bsdmainutils \
 libnet-patricia-perl libnetaddr-ip-perl libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
 libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl libnet-telnet-perl libdbi-perl \
 libdbd-mysql-perl libparallel-forkmanager-perl libproc-daemon-perl libdatetime-format-dateparse-perl \

+ 1 - 1
docs/Readme.ru.md

@@ -3,7 +3,7 @@
 1. Ставим пакеты
 
 apt install apache2 git fping perl mariadb-server php php-mysql php-bcmath php-intl \
-php-mbstring php-date php-mail php-snmp php-zip xxd \
+php-mbstring php-date php-mail php-snmp php-zip xxd bsdmainutils \
 libnet-patricia-perl libnetaddr-ip-perl libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
 libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl libnet-telnet-perl libdbi-perl \
 libdbd-mysql-perl libparallel-forkmanager-perl libproc-daemon-perl libdatetime-format-dateparse-perl \

+ 18 - 5
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,$client_id) = split (/\;/, $logline);
+            my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname,$circut_id,$remote_id,$client_id,$decoded_circuit_id,$decoded_remote_id) = split (/\;/, $logline);
             next if (!$type);
             next if ($type!~/(old|add|del)/i);
 
@@ -129,11 +129,22 @@ if (!$pid) {
 
             if (!$timestamp) { $timestamp=time(); }
 
-            my $dhcp_event_time = GetNowTime($timestamp);
-
             my $ip_aton=StrToIp($ip);
             $mac=mac_splitted(isc_mac_simplify($mac));
 
+            #detect switch
+            if (!$decoded_remote_id) {
+                $decoded_remote_id=mac_splitted(isc_mac_simplify($decoded_remote_id));
+                my $device = get_record_sql($hdb,"SELECT D.device_name,D.ip,A.mac FROM `devices` AS D,`User_auth` AS A WHERE D.user_id=A.User_id AND D.ip=A.ip AND A.deleted=0 AND A.mac='".$decoded_remote_id."'");
+                if (!$device) { 
+                    $remote_id = $decoded_remote_id;
+                    $circut_id = $decoded_circuit_id;
+                    db_log_verbose($hdb,"Dhcp request type: ".$type." ip=".$dhcp_record->{ip}." and mac=".$mac." from ".$device->{'device_name'});
+                    }
+            }
+
+            my $dhcp_event_time = GetNowTime($timestamp);
+
             my $dhcp_record;
             $dhcp_record->{'mac'}=$mac;
             $dhcp_record->{'ip'}=$ip;
@@ -180,8 +191,10 @@ if (!$pid) {
 
             #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') {
+                #don't create record by del request! 
+                #because when the host address is changed, the new address will be overwritten by the old one being released
+                if ($type=~/old/i) {
+                    db_log_warning($hdb,"Record for dhcp request type: ".$type." ip=".$dhcp_record->{ip}." and mac=".$mac." does not exists!");
                     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."!");

+ 3 - 3
scripts/dnsmasq-hook.sh

@@ -2,9 +2,9 @@
 
 time=$(date +%s)
 
-CIRCUIT_ID=$(echo ${DNSMASQ_CIRCUIT_ID}  | /usr/bin/xxd -l 16 -p )
-REMOTE_ID=$(echo ${DNSMASQ_REMOTE_ID} | /usr/bin/xxd -l 16 -p )
+CIRCUIT_ID=$(echo ${DNSMASQ_CIRCUIT_ID} | /usr/bin/hexdump -v -e '/1 "%02X"' | sed 's/0A$//i' )
+REMOTE_ID=$(echo ${DNSMASQ_REMOTE_ID} | /usr/bin/hexdump -v -e '/1 "%02X"' | sed 's/0A$//i' )
 
-echo "$1;$2;$3;$4;${time};${DNSMASQ_TAGS};${DNSMASQ_SUPPLIED_HOSTNAME};${DNSMASQ_OLD_HOSTNAME};${CIRCUIT_ID};${REMOTE_ID};${DNSMASQ_CLIENT_ID}" >>/var/log/dhcp.log &
+echo "$1;$2;$3;$4;${time};${DNSMASQ_TAGS};${DNSMASQ_SUPPLIED_HOSTNAME};${DNSMASQ_OLD_HOSTNAME};${CIRCUIT_ID};${REMOTE_ID};${DNSMASQ_CLIENT_ID};${CIRCUIT_ID};${REMOTE_ID}" >>/var/log/dhcp.log &
 
 exit