garbage.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use DBI;
  9. use Date::Parse;
  10. use Rstat::config;
  11. use Rstat::mysql;
  12. use Rstat::net_utils;
  13. db_log_info($dbh,'Garbage started.');
  14. #unblock users
  15. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  16. my $history_sql;
  17. my $history_rf;
  18. my %nets;
  19. foreach my $net (@office_network_list) {
  20. my $scope_name=$net;
  21. $scope_name =~s/\/\d+$//g;
  22. $nets{$scope_name}= new Net::Patricia;
  23. $nets{$scope_name}->add_string($net);
  24. }
  25. if ($day==1) {
  26. do_sql($dbh,"Update User_list set blocked=0");
  27. do_sql($dbh,"Update User_auth set blocked=0 where deleted=0");
  28. db_log_verbose($dbh,"Amnistuyemo all blocked user by traffic for a month");
  29. } else {
  30. #month stat
  31. my $month_sql="SELECT User_list.id, User_list.login, SUM( traf_all ) AS traf_sum, User_list.month_quota
  32. FROM ( SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats WHERE ( YEAR(User_stats.timestamp) = $year and MONTH(User_stats.timestamp) = $month ) GROUP BY User_stats.auth_id ) AS V, User_auth, User_list
  33. WHERE V.auth_id = User_auth.id AND User_auth.user_id = User_list.id and User_list.blocked=1 GROUP BY login";
  34. my $fth = $dbh->prepare($month_sql);
  35. $fth->execute;
  36. my $month_stats=$fth->fetchall_arrayref();
  37. $fth->finish;
  38. foreach my $row (@$month_stats) {
  39. my ($u_id,$u_login,$u_traf,$u_quota)=@$row;
  40. my $m_quota=$u_quota*$KB*$KB;
  41. next if ($m_quota<$u_traf);
  42. db_log_info($dbh,"Amnistuyemo blocked user $u_login [$u_id] by traffic for a day");
  43. do_sql($dbh,"UPDATE User_list set blocked=0 where id=$u_id");
  44. do_sql($dbh,"UPDATE User_auth set blocked=0 where user_id=$u_id");
  45. }
  46. }
  47. #### dhcpd ####
  48. my $clean_dhcp_time = time();
  49. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_dhcp_time);
  50. $month++;
  51. $year += 1900;
  52. my $clean_dhcp_str="$year-$month-$day";
  53. my $clean_dhcp_date=$dbh->quote($clean_dhcp_str);
  54. #clean temporary dhcp leases & connections
  55. my $users_sql = "Select id from User_auth where date(dhcp_time) < $clean_dhcp_date and (user_id=$default_user_id or user_id=$hotspot_user_id)";
  56. my $users_db = $dbh->prepare($users_sql);
  57. $users_db->execute;
  58. my $users_auth=$users_db->fetchall_arrayref();
  59. $users_db->finish;
  60. foreach my $row (@$users_auth) {
  61. my ($u_id)=@$row;
  62. do_sql($dbh,"delete from connections where auth_id='".$u_id."'");
  63. do_sql($dbh,"delete from dhcp_log where date(timestamp) < $clean_dhcp_date and auth_id='".$u_id."'" );
  64. }
  65. do_sql($dbh,"update User_auth set deleted=1 where date(dhcp_time) < $clean_dhcp_date and (user_id=$default_user_id or user_id=$hotspot_user_id)");
  66. db_log_verbose($dbh,"Clean dhcp leases for user Default older that ".$clean_dhcp_str);
  67. #clean dhcp log
  68. my $clean_dhcp_log = time()- $history_dhcp*3600*24;
  69. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_dhcp_log);
  70. $month++;
  71. $year += 1900;
  72. my $clean_dhcp_log_str="$year-$month-$day";
  73. my $clean_dhcp_log_date=$dbh->quote($clean_dhcp_log_str);
  74. do_sql($dbh,"delete from dhcp_log where date(timestamp) < $clean_dhcp_log_date" );
  75. db_log_verbose($dbh,"Clean dhcp leases for all older that ".$clean_dhcp_log_str);
  76. ##### clean old connections ########
  77. my $clean_con_time = time()-$connections_history*60*60*24;
  78. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_con_time);
  79. $month++;
  80. $year += 1900;
  81. my $clean_con_str="$year-$month-$day";
  82. my $clean_con_date=$dbh->quote($clean_con_str);
  83. $users_sql = "Select id from User_auth where date(last_found) < $clean_con_date and last_found>0 and deleted=0";
  84. db_log_debug($dbh,$users_sql) if ($debug);
  85. $users_db = $dbh->prepare($users_sql);
  86. $users_db->execute;
  87. $users_auth=$users_db->fetchall_arrayref();
  88. $users_db->finish;
  89. foreach my $row (@$users_auth) {
  90. my ($u_id)=@$row;
  91. db_log_debug($dbh,"Clear old connection for user_auth ".$u_id) if ($debug);
  92. do_sql($dbh,"Delete from connections where auth_id='".$u_id."'");
  93. if ($auth_clear) {
  94. db_log_debug($dbh,"Clear old user_auth ".$u_id) if ($debug);
  95. do_sql($dbh,"Update User_auth set deleted=1 where id='".$u_id."'");
  96. }
  97. }
  98. ##### clean dup connections ########
  99. my $conn_sql = "Select id,port_id,auth_id from connections order by port_id";
  100. my $conn_db = $dbh->prepare($conn_sql);
  101. $conn_db->execute;
  102. my $conn_ref=$conn_db->fetchall_arrayref();
  103. $conn_db->finish;
  104. my $old_port_id=0;
  105. my $old_auth_id=0;
  106. foreach my $row (@$conn_ref) {
  107. my ($c_id,$c_port_id,$c_auth_id)=@$row;
  108. if (!$c_port_id) { $c_port_id=0; }
  109. if (!$c_auth_id) { $c_auth_id=0; }
  110. if ($old_port_id ==0 or $old_auth_id==0) { $old_port_id=$c_port_id; $old_auth_id=$c_auth_id; next; }
  111. if ($old_port_id >0 and $old_port_id != $c_port_id) { $old_port_id=$c_port_id; $old_auth_id=$c_auth_id; next; }
  112. if ($old_auth_id >0 and $old_auth_id != $c_auth_id) { $old_port_id=$c_port_id; $old_auth_id=$c_auth_id; next; }
  113. do_sql($dbh,"delete from connections where id='".$c_id."'");
  114. db_log_verbose($dbh,"Remove dup connection $c_id: $c_port_id $c_auth_id");
  115. }
  116. ##### unknown mac clean ############
  117. $users_sql = "Select mac from User_auth where deleted=0";
  118. $users_db = $dbh->prepare($users_sql);
  119. $users_db->execute;
  120. $users_auth=$users_db->fetchall_arrayref();
  121. $users_db->finish;
  122. foreach my $row (@$users_auth) {
  123. my ($u_mac)=@$row;
  124. do_sql($dbh,"Delete from Unknown_mac where mac='".mac_simplify($u_mac)."'");
  125. }
  126. ##### traffic detail ######
  127. my $clean_time = time()-$history*60*60*24;
  128. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  129. $month++;
  130. $year += 1900;
  131. my $clean_str="$year-$month-$day";
  132. my $clean_date=$dbh->quote($clean_str);
  133. db_log_verbose($dbh,"Clean traffic detail older that ".$clean_str);
  134. #clean old traffic detail
  135. do_sql($dbh,"delete from Traffic_detail where date(timestamp) < $clean_date" );
  136. ##### log ######
  137. $clean_time = time()-$history_log_day*60*60*24;
  138. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  139. $month++;
  140. $year += 1900;
  141. $clean_str="$year-$month-$day";
  142. $clean_date=$dbh->quote($clean_str);
  143. db_log_verbose($dbh,"Clean worklog older that ".$clean_str);
  144. do_sql($dbh,"delete from syslog where date(timestamp) < $clean_date" );
  145. ##### syslog ######
  146. $clean_time = time()-$history_syslog_day*60*60*24;
  147. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  148. $month++;
  149. $year += 1900;
  150. $clean_str="$year-$month-$day";
  151. $clean_date=$dbh->quote($clean_str);
  152. db_log_verbose($dbh,"Clean syslog older that ".$clean_str);
  153. do_sql($dbh,"delete from remote_syslog where date(`date`) < $clean_date" );
  154. ##### Traffic stats ######
  155. $clean_time = time()-$history_trafstat_day*60*60*24;
  156. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  157. $month++;
  158. $year += 1900;
  159. $clean_str="$year-$month-$day";
  160. $clean_date=$dbh->quote($clean_str);
  161. db_log_verbose($dbh,"Clean traffic statistics older that ".$clean_str);
  162. do_sql($dbh,"delete from User_stats where date(timestamp) < $clean_date" );
  163. do_sql($dbh,"delete from User_traffic where date(timestamp) < $clean_date" );
  164. db_log_info($dbh,'Garbage stopped.');
  165. $dbh->disconnect;
  166. print "Done\n" if ($debug);
  167. exit 0;