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 Net::Netmask;
  21. use Text::Iconv;
  22. use File::Tail;
  23. my $pf = '/var/run/dhcp-log.pid';
  24. my $log_file='/var/log/dhcp.log';
  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. # Create new database handle. If we can't connect, die()
  79. my $hdb = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  80. if ( !defined $hdb ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  81. #parse log
  82. my $dhcp_log=File::Tail->new(name=>$log_file,maxinterval=>5,interval=>1,ignore_nonexistant=>1) || die "$log_file not found!";
  83. while (my $logline=$dhcp_log->read) {
  84. next if (!$logline);
  85. chomp($logline);
  86. log_verbose("GET CLIENT REQUEST: $logline");
  87. my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname) = split (/\;/, $logline);
  88. next if (!$type);
  89. next if ($type!~/(old|add|del)/i);
  90. if (exists $leases{$ip} and time()-$leases{$ip}{last_time} <= $mute_time) { next; }
  91. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  92. my $client_hostname='UNDEFINED';
  93. if ($hostname and $hostname ne "undef") { $client_hostname=$hostname; } else {
  94. if ($sup_hostname) { $client_hostname=$sup_hostname; } else {
  95. if ($old_hostname) { $client_hostname=$old_hostname; }
  96. }
  97. }
  98. my $auth_network = $office_networks->match_string($ip);
  99. if (!$auth_network) {
  100. log_error("Unknown network in dhcp request! IP: $ip");
  101. next;
  102. }
  103. if (!$timestamp) { $timestamp=time(); }
  104. my $dhcp_event_time = GetNowTime($timestamp);
  105. my $ip_aton=StrToIp($ip);
  106. $mac=mac_splitted(isc_mac_simplify($mac));
  107. my $dhcp_record;
  108. $dhcp_record->{mac}=$mac;
  109. $dhcp_record->{ip}=$ip;
  110. $dhcp_record->{ip_aton}=$ip_aton;
  111. $dhcp_record->{hostname}=$client_hostname;
  112. $dhcp_record->{tags}=$tags;
  113. $dhcp_record->{network}=$auth_network;
  114. $dhcp_record->{type}=$type;
  115. $dhcp_record->{hostname_utf8}=$converter->convert($client_hostname);
  116. $dhcp_record->{timestamp} = $timestamp;
  117. $dhcp_record->{last_time} = time();
  118. $dhcp_record->{hotspot}=is_hotspot($dbh,$dhcp_record->{ip});
  119. $leases{$ip}=$dhcp_record;
  120. log_debug(uc($type).">>");
  121. log_debug("MAC: ".$dhcp_record->{mac});
  122. log_debug("IP: ".$dhcp_record->{ip});
  123. log_debug("TAGS: ".$dhcp_record->{tags});
  124. log_debug("HOSTNAME: ".$dhcp_record->{hostname});
  125. log_debug("TYPE: ".$dhcp_record->{type});
  126. log_debug("TIME: ".$dhcp_event_time);
  127. log_debug("UTF8 HOSTNAME: ".$dhcp_record->{hostname_utf8});
  128. log_debug("END GET");
  129. 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');
  130. if (!$auth_record and $type eq 'old' ) { $type='add'; }
  131. if ($type eq 'add') {
  132. my $res_id = resurrection_auth($hdb,$dhcp_record);
  133. next if (!$res_id);
  134. $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
  135. db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
  136. } 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'); }
  137. my $auth_id = $auth_record->{id};
  138. my $auth_ou_id = $auth_record->{ou_id};
  139. update_dns_record($hdb,$dhcp_record,$auth_record);
  140. if ($type=~/add/i and $dhcp_record->{hostname_utf8} and $dhcp_record->{hostname_utf8} !~/UNDEFINED/i) {
  141. my $auth_rec;
  142. $auth_rec->{dhcp_hostname} = $dhcp_record->{hostname_utf8};
  143. $auth_rec->{dhcp_time}=$dhcp_event_time;
  144. db_log_verbose($hdb,"Add lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
  145. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  146. }
  147. if ($dhcp_record->{hotspot} and $ignore_hotspot_dhcp_log) { next; }
  148. if ($ignore_update_dhcp_event and $type=~/old/i) { next; }
  149. if ($type=~/old/i) {
  150. my $auth_rec;
  151. $auth_rec->{dhcp_action}=$type;
  152. $auth_rec->{dhcp_time}=$dhcp_event_time;
  153. db_log_verbose($hdb,"Update lease by dhcp event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
  154. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  155. }
  156. if ($type=~/del/i and $auth_id) {
  157. if ($auth_record->{dhcp_time} =~ /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
  158. my $d_time = mktime($6,$5,$4,$3,$2-1,$1-1900);
  159. if (time()-$d_time>60 and ($auth_ou_id == $default_user_ou_id or $auth_ou_id==$default_hotspot_ou_id)) {
  160. db_log_info($hdb,"Remove user ip record by dhcp release event for dynamic clients id:$auth_id ip: $dhcp_record->{ip}",$auth_id);
  161. my $auth_rec;
  162. $auth_rec->{deleted}="1";
  163. $auth_rec->{dhcp_action}=$type;
  164. $auth_rec->{dhcp_time}=$dhcp_event_time;
  165. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  166. my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$auth_record->{'user_id'});
  167. if (!$u_count) {
  168. delete_record($hdb,"User_list","id=".$auth_record->{'user_id'});
  169. db_log_info($hdb,"Remove dynamic user id: $auth_record->{'user_id'} by dhcp request",$auth_id);
  170. }
  171. }
  172. }
  173. }
  174. my $dhcp_log;
  175. if (!$auth_id) { $auth_id=0; }
  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. };
  185. if ($@) { log_error("Exception found: $@"); sleep(60); }
  186. }
  187. } else {
  188. print "Already Running with pid $pid\n";
  189. }
  190. }
  191. sub usage {
  192. print "usage: dhcp-log.pl (start|stop|status|restart)\n";
  193. exit(0);
  194. }
  195. sub reload {
  196. print "reload process not implemented.\n";
  197. }
  198. sub restart {
  199. stop;
  200. run;
  201. }