dhcp-log.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. $debug = 1;
  24. my $pf = '/var/run/dhcp-log.pid';
  25. my $log_file='/var/log/dhcp.log';
  26. my $mute_time=300;
  27. my $daemon = Proc::Daemon->new(
  28. pid_file => $pf,
  29. work_dir => $HOME_DIR
  30. );
  31. # are you running? Returns 0 if not.
  32. my $pid = $daemon->Status($pf);
  33. my $daemonize = 1;
  34. GetOptions(
  35. 'daemon!' => \$daemonize,
  36. "help" => \&usage,
  37. "reload" => \&reload,
  38. "restart" => \&restart,
  39. "start" => \&run,
  40. "status" => \&status,
  41. "stop" => \&stop
  42. ) or &usage;
  43. exit(0);
  44. sub stop {
  45. if ($pid) {
  46. print "Stopping pid $pid...";
  47. if ($daemon->Kill_Daemon($pf)) {
  48. print "Successfully stopped.\n";
  49. } else {
  50. print "Could not find $pid. Was it running?\n";
  51. }
  52. } else {
  53. print "Not running, nothing to stop.\n";
  54. }
  55. }
  56. sub status {
  57. if ($pid) {
  58. print "Running with pid $pid.\n";
  59. } else {
  60. print "Not running.\n";
  61. }
  62. }
  63. sub run {
  64. if (!$pid) {
  65. print "Starting...";
  66. if ($daemonize) {
  67. # when Init happens, everything under it runs in the child process.
  68. # this is important when dealing with file handles, due to the fact
  69. # Proc::Daemon shuts down all open file handles when Init happens.
  70. # Keep this in mind when laying out your program, particularly if
  71. # you use filehandles.
  72. $daemon->Init;
  73. }
  74. setpriority(0,0,19);
  75. my $converter = Text::Iconv->new("cp866", "utf8");
  76. while (1) {
  77. eval {
  78. my %leases;
  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. #parse log
  83. my $dhcp_log=File::Tail->new(name=>$log_file,maxinterval=>5,interval=>1,ignore_nonexistant=>1) || die "$log_file not found!";
  84. #truncate current log file
  85. truncate $log_file, 0;
  86. while (my $logline=$dhcp_log->read) {
  87. next if (!$logline);
  88. chomp($logline);
  89. log_verbose("GET CLIENT REQUEST: $logline");
  90. my ($type,$mac,$ip,$hostname,$timestamp,$tags,$sup_hostname,$old_hostname,$circut_id,$remote_id,$client_id,$decoded_circuit_id,$decoded_remote_id) = 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 $ip_aton=StrToIp($ip);
  108. $mac=mac_splitted(isc_mac_simplify($mac));
  109. my $dhcp_event_time = GetNowTime($timestamp);
  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. $dhcp_record->{'circuit-id'} = $circut_id;
  122. $dhcp_record->{'client-id'} = $client_id;
  123. $dhcp_record->{'remote-id'} = $remote_id;
  124. $dhcp_record->{'hotspot'}=is_hotspot($dbh,$dhcp_record->{ip});
  125. $leases{$ip}=$dhcp_record;
  126. my $switch;
  127. #detect switch
  128. if ($decoded_remote_id) {
  129. #fill '0' to remote-id for full mac lenght
  130. if (length($decoded_remote_id)<12) {
  131. for (my $i = length($decoded_remote_id); $i < 12; $i++) {
  132. $decoded_remote_id = $decoded_remote_id."0";
  133. }
  134. }
  135. $decoded_remote_id=mac_splitted(isc_mac_simplify($decoded_remote_id));
  136. my $devSQL = "SELECT 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='".$decoded_remote_id."'";
  137. log_debug($devSQL);
  138. $switch = get_record_sql($hdb,$devSQL);
  139. if ($switch) {
  140. $remote_id = $decoded_remote_id;
  141. $circut_id = $decoded_circuit_id;
  142. $dhcp_record->{'circuit-id'} = $circut_id;
  143. $dhcp_record->{'remote-id'} = $remote_id;
  144. }
  145. }
  146. #maybe string?
  147. if (!$switch and $remote_id) {
  148. my @id_words = split(/ /,$remote_id);
  149. if ($id_words[0]) {
  150. my $devSQL = "SELECT 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]."%'";
  151. log_debug($devSQL);
  152. $switch = get_record_sql($hdb,$devSQL);
  153. }
  154. }
  155. if ($switch) {
  156. db_log_verbose($hdb,"Dhcp request type: ".$type." ip=".$ip." and mac=".$mac." from ".$switch->{'device_name'});
  157. }
  158. log_debug(uc($type).">>");
  159. log_debug("MAC: ".$dhcp_record->{'mac'});
  160. log_debug("IP: ".$dhcp_record->{'ip'});
  161. log_debug("TAGS: ".$dhcp_record->{'tags'});
  162. log_debug("CIRCUIT-ID:".$dhcp_record->{'circuit-id'});
  163. log_debug("REMOTE-ID: ".$dhcp_record->{'remote-id'});
  164. log_debug("HOSTNAME: ".$dhcp_record->{'hostname'});
  165. log_debug("TYPE: ".$dhcp_record->{'type'});
  166. log_debug("TIME: ".$dhcp_event_time);
  167. log_debug("UTF8 NAME: ".$dhcp_record->{'hostname_utf8'});
  168. log_debug("SWITCH: ".$switch->{'device_name'}) if ($switch);
  169. log_debug("END GET");
  170. 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');
  171. if (!$auth_record and $type eq 'old' ) { $type='add'; }
  172. if ($type eq 'add') {
  173. my $res_id = resurrection_auth($hdb,$dhcp_record);
  174. if (!$res_id) {
  175. db_log_error($hdb,"Error creating an ip address record for ip=".$dhcp_record->{ip}." and mac=".$mac."!");
  176. next;
  177. }
  178. $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
  179. db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
  180. } else {
  181. $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');
  182. }
  183. #create new record for refresh dhcp packet
  184. if (!$auth_record) {
  185. #don't create record by del request!
  186. #because when the host address is changed, the new address will be overwritten by the old one being released
  187. if ($type=~/old/i) {
  188. db_log_warning($hdb,"Record for dhcp request type: ".$type." ip=".$dhcp_record->{ip}." and mac=".$mac." does not exists!");
  189. my $res_id = resurrection_auth($hdb,$dhcp_record);
  190. if (!$res_id) {
  191. db_log_error($hdb,"Error creating an ip address record for ip=".$dhcp_record->{ip}." and mac=".$mac."!");
  192. next;
  193. }
  194. $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
  195. db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
  196. } else { next; }
  197. }
  198. my $auth_id = $auth_record->{id};
  199. my $auth_ou_id = $auth_record->{ou_id};
  200. update_dns_record($hdb,$dhcp_record,$auth_record);
  201. if ($type=~/add/i and $dhcp_record->{hostname_utf8} and $dhcp_record->{hostname_utf8} !~/UNDEFINED/i) {
  202. my $auth_rec;
  203. $auth_rec->{dhcp_hostname} = $dhcp_record->{hostname_utf8};
  204. $auth_rec->{dhcp_time}=$dhcp_event_time;
  205. db_log_verbose($hdb,"Add lease by dhcp event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
  206. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  207. }
  208. if ($dhcp_record->{hotspot} and $ignore_hotspot_dhcp_log) { next; }
  209. if ($ignore_update_dhcp_event and $type=~/old/i) { next; }
  210. if ($type=~/old/i) {
  211. my $auth_rec;
  212. $auth_rec->{dhcp_action}=$type;
  213. $auth_rec->{dhcp_time}=$dhcp_event_time;
  214. db_log_verbose($hdb,"Update lease by dhcp event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
  215. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  216. }
  217. if ($type=~/del/i and $auth_id) {
  218. if ($auth_record->{dhcp_time} =~ /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
  219. my $d_time = mktime($6,$5,$4,$3,$2-1,$1-1900);
  220. if (time()-$d_time>60 and ($auth_ou_id == $default_user_ou_id or $auth_ou_id==$default_hotspot_ou_id)) {
  221. db_log_info($hdb,"Remove user ip record by dhcp release event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
  222. my $auth_rec;
  223. $auth_rec->{deleted}="1";
  224. $auth_rec->{dhcp_action}=$type;
  225. $auth_rec->{dhcp_time}=$dhcp_event_time;
  226. update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
  227. my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$auth_record->{'user_id'});
  228. if (!$u_count) {
  229. delete_record($hdb,"User_list","id=".$auth_record->{'user_id'});
  230. db_log_info($hdb,"Remove dynamic user id: $auth_record->{'user_id'} by dhcp request",$auth_id);
  231. }
  232. }
  233. }
  234. }
  235. my $dhcp_log;
  236. if (!$auth_id) { $auth_id=0; }
  237. $dhcp_log->{'auth_id'} = $auth_id;
  238. $dhcp_log->{'ip'} = $dhcp_record->{'ip'};
  239. $dhcp_log->{'ip_int'} = $dhcp_record->{'ip_aton'};
  240. $dhcp_log->{'mac'} = $dhcp_record->{'mac'};
  241. $dhcp_log->{'action'} = $type;
  242. $dhcp_log->{'dhcp_hostname'} = $dhcp_record->{'hostname_utf8'};
  243. $dhcp_log->{'timestamp'} = $dhcp_event_time;
  244. $dhcp_log->{'circuit-id'} = $circut_id;
  245. $dhcp_log->{'client-id'} = $client_id;
  246. $dhcp_log->{'remote-id'} = $remote_id;
  247. insert_record($hdb,'dhcp_log',$dhcp_log);
  248. }
  249. };
  250. if ($@) { log_error("Exception found: $@"); sleep(60); }
  251. }
  252. } else {
  253. print "Already Running with pid $pid\n";
  254. }
  255. }
  256. sub usage {
  257. print "usage: dhcp-log.pl (start|stop|status|restart)\n";
  258. exit(0);
  259. }
  260. sub reload {
  261. print "reload process not implemented.\n";
  262. }
  263. sub restart {
  264. stop;
  265. run;
  266. }