dhcp-log.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use Data::Dumper;
  8. use Rstat::config;
  9. use Rstat::main;
  10. use Rstat::mysql;
  11. use Rstat::net_utils;
  12. use strict;
  13. use warnings;
  14. use Getopt::Long;
  15. use Proc::Daemon;
  16. use POSIX;
  17. use Cwd;
  18. use IO::Socket::UNIX qw( SOCK_STREAM );
  19. use Net::Netmask;
  20. use File::Spec::Functions;
  21. use File::Copy qw(move);
  22. use Text::Iconv;
  23. my $pf = '/var/run/dhcp-log.pid';
  24. my $socket_path='/var/spool/dhcp-log.socket';
  25. my $mute_time=300;
  26. my $daemon = Proc::Daemon->new(
  27. pid_file => $pf,
  28. work_dir => $HOME_DIR
  29. );
  30. # are you running? Returns 0 if not.
  31. my $pid = $daemon->Status($pf);
  32. my $daemonize = 1;
  33. GetOptions(
  34. 'daemon!' => \$daemonize,
  35. "help" => \&usage,
  36. "reload" => \&reload,
  37. "restart" => \&restart,
  38. "start" => \&run,
  39. "status" => \&status,
  40. "stop" => \&stop
  41. ) or &usage;
  42. exit(0);
  43. sub stop {
  44. if ($pid) {
  45. print "Stopping pid $pid...";
  46. if ($daemon->Kill_Daemon($pf)) {
  47. print "Successfully stopped.\n";
  48. } else {
  49. print "Could not find $pid. Was it running?\n";
  50. }
  51. } else {
  52. print "Not running, nothing to stop.\n";
  53. }
  54. }
  55. sub status {
  56. if ($pid) {
  57. print "Running with pid $pid.\n";
  58. } else {
  59. print "Not running.\n";
  60. }
  61. }
  62. sub run {
  63. if (!$pid) {
  64. print "Starting...";
  65. if ($daemonize) {
  66. # when Init happens, everything under it runs in the child process.
  67. # this is important when dealing with file handles, due to the fact
  68. # Proc::Daemon shuts down all open file handles when Init happens.
  69. # Keep this in mind when laying out your program, particularly if
  70. # you use filehandles.
  71. $daemon->Init;
  72. }
  73. setpriority(0,0,19);
  74. my $converter = Text::Iconv->new("cp866", "utf8");
  75. while (1) {
  76. eval {
  77. my %leases;
  78. open(DHCP_SOCKET,$socket_path) || die("Error open fifo socket $socket_path: $!");
  79. # Create new database handle. If we can't connect, die()
  80. my $hdb = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  81. if ( !defined $hdb ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  82. while (my $logline = <DHCP_SOCKET>) {
  83. next unless defined $logline;
  84. chomp($logline);
  85. log_info("GET CLIENT REQUEST: $logline");
  86. my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname) = split (/\;/, $logline);
  87. next if (!$type);
  88. next if ($type!~/(old|add|del)/i);
  89. if (exists $leases{$ip} and time()-$leases{$ip}{last_time} <= $mute_time) { next; }
  90. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  91. my $client_hostname='UNDEFINED';
  92. if ($hostname and $hostname ne "undef") { $client_hostname=$hostname; } else {
  93. if ($sup_hostname) { $client_hostname=$sup_hostname; } else {
  94. if ($old_hostname) { $client_hostname=$old_hostname; }
  95. }
  96. }
  97. my $auth_network = $office_networks->match_string($ip);
  98. if (!$auth_network) {
  99. log_error("Unknown network in dhcp request! IP: $ip");
  100. next;
  101. }
  102. if (!$timestamp) { $timestamp=time(); }
  103. my $dhcp_event_time = GetNowTime($timestamp);
  104. my $ip_aton=StrToIp($ip);
  105. $mac=mac_splitted(isc_mac_simplify($mac));
  106. my $dhcp_record;
  107. $dhcp_record->{mac}=$mac;
  108. $dhcp_record->{ip}=$ip;
  109. $dhcp_record->{ip_aton}=$ip_aton;
  110. $dhcp_record->{hostname}=$client_hostname;
  111. $dhcp_record->{tags}=$tags;
  112. $dhcp_record->{network}=$auth_network;
  113. $dhcp_record->{type}=$type;
  114. $dhcp_record->{hostname_utf8}=$converter->convert($client_hostname);
  115. $dhcp_record->{timestamp} = $timestamp;
  116. $dhcp_record->{last_time} = time();
  117. $leases{$ip}=$dhcp_record;
  118. log_debug(uc($type).">>");
  119. log_debug("MAC: ".$dhcp_record->{mac});
  120. log_debug("IP: ".$dhcp_record->{ip});
  121. log_debug("TAGS: ".$dhcp_record->{tags});
  122. log_debug("HOSTNAME: ".$dhcp_record->{hostname});
  123. log_debug("TYPE: ".$dhcp_record->{type});
  124. log_debug("TIME: ".$dhcp_event_time);
  125. log_debug("UTF8 HOSTNAME: ".$dhcp_record->{hostname_utf8});
  126. log_debug("END GET");
  127. if ($type eq 'add') {
  128. my $res_id = resurrection_auth($hdb,$dhcp_record->{ip},$mac,$type,$dhcp_record->{hostname_utf8});
  129. log_info("Check for new auth. Found id: $res_id");
  130. }
  131. my $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');
  132. my $auth_id = $auth_record->{id};
  133. update_dns_record($hdb,$dhcp_record,$auth_record);
  134. if ($type=~/add/i and $dhcp_record->{hostname_utf8} and $dhcp_record->{hostname_utf8} !~/UNDEFINED/i) {
  135. my $auth_rec;
  136. $auth_rec->{dhcp_hostname} = $dhcp_record->{hostname_utf8};
  137. $auth_rec->{dhcp_time}=$dhcp_event_time;
  138. log_info("Add lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}");
  139. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  140. }
  141. if ($hotspot_networks->match_string($dhcp_record->{ip}) and $ignore_hotspot_dhcp_log) { next; }
  142. if ($ignore_update_dhcp_event and $type=~/old/i) { next; }
  143. if ($type=~/old/i) {
  144. my $auth_rec;
  145. $auth_rec->{dhcp_action}=$type;
  146. $auth_rec->{dhcp_time}=$dhcp_event_time;
  147. log_info("Update lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}");
  148. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  149. }
  150. if ($type=~/del/i and $auth_id) {
  151. if ($auth_record->{dhcp_time} =~ /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
  152. my $d_time = mktime($6,$5,$4,$3,$2-1,$1-1900);
  153. if (time()-$d_time>60 and ($auth_id == $config_ref{default_user_id} or $auth_id == $config_ref{hotspot_user_id})) {
  154. log_info("Remove user ip record by dhcp release event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}");
  155. my $auth_rec;
  156. $auth_rec->{deleted}="1";
  157. $auth_rec->{dhcp_action}=$type;
  158. $auth_rec->{dhcp_time}=$dhcp_event_time;
  159. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  160. }
  161. }
  162. }
  163. my $dhcp_log;
  164. $dhcp_log->{auth_id} = $auth_id;
  165. $dhcp_log->{ip} = $dhcp_record->{ip};
  166. $dhcp_log->{ip_int} = $dhcp_record->{ip_aton};
  167. $dhcp_log->{mac} = $dhcp_record->{mac};
  168. $dhcp_log->{action} = $type;
  169. $dhcp_log->{timestamp} = $dhcp_event_time;
  170. insert_record($hdb,'dhcp_log',$dhcp_log);
  171. }
  172. close DHCP_SOCKET;
  173. };
  174. if ($@) { log_error("Exception found: $@"); sleep(60); }
  175. }
  176. } else {
  177. print "Already Running with pid $pid\n";
  178. }
  179. }
  180. sub usage {
  181. print "usage: dhcp-log.pl (start|stop|status|restart)\n";
  182. exit(0);
  183. }
  184. sub reload {
  185. print "reload process not implemented.\n";
  186. }
  187. sub restart {
  188. stop;
  189. run;
  190. }