dhcp-log.pl 8.5 KB

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