dhcp-log.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use Encode;
  8. no warnings 'utf8';
  9. use English;
  10. use base;
  11. use FindBin '$Bin';
  12. use lib "/opt/Eye/scripts";
  13. use Data::Dumper;
  14. use eyelib::config;
  15. use eyelib::main;
  16. use eyelib::database;
  17. use eyelib::net_utils;
  18. use strict;
  19. use warnings;
  20. use Getopt::Long;
  21. use Proc::Daemon;
  22. use POSIX;
  23. use Net::Netmask;
  24. use Text::Iconv;
  25. use File::Tail;
  26. use Fcntl qw(:flock);
  27. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  28. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  29. setpriority(0,0,19);
  30. my $mute_time=300;
  31. my $log_file='/var/log/dhcp.log';
  32. my $proc_name = $MY_NAME;
  33. $proc_name =~ s/\.[^.]+$//;
  34. my $pid_file = '/run/eye/'.$proc_name;
  35. my $pf = $pid_file.'.pid';
  36. my $daemon = Proc::Daemon->new(
  37. pid_file => $pf,
  38. work_dir => $HOME_DIR
  39. );
  40. # are you running? Returns 0 if not.
  41. my $pid = $daemon->Status($pf);
  42. my $daemonize = 1;
  43. GetOptions(
  44. 'daemon!' => \$daemonize,
  45. "help" => \&usage,
  46. "reload" => \&reload,
  47. "restart" => \&restart,
  48. "start" => \&run,
  49. "status" => \&status,
  50. "stop" => \&stop
  51. ) or &usage;
  52. exit(0);
  53. sub stop {
  54. if ($pid) {
  55. print "Stopping pid $pid...";
  56. if ($daemon->Kill_Daemon($pf)) {
  57. print "Successfully stopped.\n";
  58. } else {
  59. print "Could not find $pid. Was it running?\n";
  60. }
  61. } else {
  62. print "Not running, nothing to stop.\n";
  63. }
  64. }
  65. sub status {
  66. if ($pid) {
  67. print "Running with pid $pid.\n";
  68. } else {
  69. print "Not running.\n";
  70. }
  71. }
  72. sub run {
  73. if (!$pid) {
  74. print "Starting...";
  75. if ($daemonize) {
  76. # when Init happens, everything under it runs in the child process.
  77. # this is important when dealing with file handles, due to the fact
  78. # Proc::Daemon shuts down all open file handles when Init happens.
  79. # Keep this in mind when laying out your program, particularly if
  80. # you use filehandles.
  81. $daemon->Init;
  82. }
  83. setpriority(0,0,19);
  84. my $converter = Text::Iconv->new("cp866", "utf8");
  85. while (1) {
  86. eval {
  87. my %leases;
  88. # Create new database handle. If we can't connect, die()
  89. my $hdb = init_db();
  90. #parse log
  91. my $dhcp_log=File::Tail->new(name=>$log_file,maxinterval=>5,interval=>1,ignore_nonexistant=>1) || die "$log_file not found!";
  92. #truncate current log file
  93. #truncate $log_file, 0;
  94. while (my $logline=$dhcp_log->read) {
  95. next if (!$logline);
  96. chomp($logline);
  97. log_verbose("GET CLIENT REQUEST: $logline");
  98. $logline =~ s/[^\p{L}\p{N}\p{P}\p{Z}]//g;
  99. log_debug("Filter printable : $logline");
  100. my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname,$circuit_id,$remote_id,$client_id,$decoded_circuit_id,$decoded_remote_id) = split (/\;/, $logline);
  101. next if (!$type);
  102. next if ($type!~/(old|add|del)/i);
  103. #mute doubles
  104. if (exists $leases{$ip} and $leases{$ip}{'type'} eq $type and time()-$leases{$ip}{'last_time'} <= $mute_time) { next; }
  105. #update config variables every 1 minute
  106. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  107. my $dhcp_record = process_dhcp_request($hdb, $type, $mac, $ip, $hostname, $client_id, $decoded_circuit_id, $decoded_remote_id);
  108. next if (!$dhcp_record);
  109. #save record for mute
  110. $leases{$ip}=$dhcp_record;
  111. my $auth_id = $dhcp_record->{auth_id};
  112. my $switch;
  113. my $switch_port;
  114. my $t_remote_id;
  115. my $t_circuit_id = $circuit_id;
  116. #detect connection
  117. if ($type =~/(add|old)/) {
  118. #detect switch by decoded remote-id
  119. if ($decoded_remote_id) {
  120. $t_remote_id = $decoded_remote_id;
  121. #fill '0' to remote-id for full mac lenght
  122. if (length($t_remote_id)<12) {
  123. for (my $i = length($decoded_remote_id); $i < 12; $i++) { $t_remote_id = $t_remote_id."0"; }
  124. }
  125. $t_remote_id=mac_splitted(isc_mac_simplify($t_remote_id));
  126. my $devSQL = "SELECT D.id, 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='".$t_remote_id."'";
  127. log_debug($devSQL);
  128. $switch = get_record_sql($hdb,$devSQL);
  129. if ($switch) {
  130. $remote_id = $t_remote_id;
  131. $circuit_id = $decoded_circuit_id;
  132. $dhcp_record->{'circuit-id'} = $circuit_id;
  133. $dhcp_record->{'remote-id'} = $remote_id;
  134. }
  135. }
  136. #detect switch by original remote-id
  137. if (!$switch and $remote_id) {
  138. $t_remote_id = $remote_id;
  139. #fill '0' to remote-id for full mac lenght
  140. if (length($t_remote_id)<12) {
  141. for (my $i = length($decoded_remote_id); $i < 12; $i++) { $t_remote_id = $t_remote_id."0"; }
  142. }
  143. $t_remote_id=mac_splitted(isc_mac_simplify($t_remote_id));
  144. my $devSQL = "SELECT D.id, 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='".$t_remote_id."'";
  145. log_debug($devSQL);
  146. $switch = get_record_sql($hdb,$devSQL);
  147. if ($switch) {
  148. $remote_id = $t_remote_id;
  149. $dhcp_record->{'circuit-id'} = $circuit_id;
  150. $dhcp_record->{'remote-id'} = $remote_id;
  151. }
  152. }
  153. #maybe remote-id is string name device?
  154. if (!$switch and $remote_id) {
  155. my @id_words = split(/ /,$remote_id);
  156. if ($id_words[0]) {
  157. my $devSQL = "SELECT D.id, 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 D.device_name like '".$id_words[0]."%'";
  158. log_debug($devSQL);
  159. $switch = get_record_sql($hdb,$devSQL);
  160. }
  161. }
  162. #maybe mikrotik?!
  163. if (!$switch and $circuit_id) {
  164. my @id_words = split(/ /,$circuit_id);
  165. if ($id_words[0]) {
  166. my $devSQL = "SELECT D.id, 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 D.device_name like '".$id_words[0]."%'";
  167. log_debug($devSQL);
  168. $switch = get_record_sql($hdb,$devSQL);
  169. #fucking mikrotik - swap variables
  170. if ($switch) {
  171. $circuit_id = $remote_id;
  172. $remote_id = $t_circuit_id;
  173. $dhcp_record->{'circuit-id'} = $circuit_id;
  174. $dhcp_record->{'remote-id'} = $remote_id;
  175. }
  176. }
  177. }
  178. if ($switch) {
  179. $t_circuit_id=~s/[\+\-\s]+/ /g;
  180. #detect port by name
  181. my @device_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=".$switch->{id});
  182. my %device_ports_h;
  183. foreach my $port_data (@device_ports) {
  184. if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
  185. $device_ports_h{$port_data->{port}} = $port_data;
  186. if ($t_circuit_id=~/\s*$port_data->{'ifName'}$/i or $t_circuit_id=~/^$port_data->{'ifName'}\s+/i ) { $switch_port = $port_data; last; }
  187. }
  188. #detect hex - get last 2 byte
  189. if (!$switch_port) {
  190. my $hex_port = substr($decoded_circuit_id, -2);
  191. if ($hex_port) {
  192. my $t_port = hex($hex_port);
  193. #try find port by index
  194. if (exists $device_ports_h{$t_port}) { $switch_port =$device_ports_h{$t_port}; }
  195. }
  196. }
  197. if ($switch_port) {
  198. db_log_verbose($hdb,"Dhcp request type: ".$type." ip=".$ip." and mac=".$mac." from ".$switch->{'device_name'}." and port ".$switch_port->{'ifName'});
  199. #check connection
  200. my $connection=get_records_sql($dbh,"SELECT * FROM connections WHERE auth_id=".$auth_id);
  201. my $new_connection;
  202. if (!$connection) {
  203. $new_connection->{port_id} = $switch_port->{id};
  204. $new_connection->{device_id} = $switch->{id};
  205. $new_connection->{auth_id} = $auth_id;
  206. insert_record($hdb,'connections',$new_connection);
  207. }
  208. # else
  209. # {
  210. # $new_connection->{port_id} = $switch_port->{id};
  211. # $new_connection->{device_id} = $switch->{id};
  212. # update_record($hdb,'connections',$new_connection,"id=".$connection->{id});
  213. # }
  214. } else {
  215. db_log_verbose($hdb,"Dhcp request type: ".$type." ip=".$ip." and mac=".$mac." from ".$switch->{'device_name'}." from unknown port");
  216. }
  217. }
  218. }
  219. log_debug("SWITCH: ".$switch->{'device_name'}) if ($switch);
  220. log_debug("SWITCH PORT:".$switch_port->{'ifName'}) if ($switch_port);
  221. }
  222. };
  223. if ($@) { log_error("Exception found: $@"); sleep(60); }
  224. }
  225. } else {
  226. print "Already Running with pid $pid\n";
  227. }
  228. }
  229. sub usage {
  230. print "usage: $MY_NAME (start|stop|status|restart)\n";
  231. exit(0);
  232. }
  233. sub reload {
  234. print "reload process not implemented.\n";
  235. }
  236. sub restart {
  237. stop;
  238. run;
  239. }