dhcp-log.pl 8.7 KB

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