garbage.pl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. use DateTime;
  14. db_log_info($dbh,'Garbage started.');
  15. sub is_dhcp_pool {
  16. my $pools = shift;
  17. my $ip_int = shift;
  18. foreach my $subnet (keys %{$pools}) {
  19. #print "net: $subnet ip: $ip_int pool: $pools->{$subnet}->{first_ip} .. $pools->{$subnet}->{last_ip}\n";
  20. if ($ip_int <= $pools->{$subnet}->{last_ip} and $ip_int>= $pools->{$subnet}->{first_ip}) { return $subnet; }
  21. }
  22. return 0;
  23. }
  24. #unblock users
  25. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  26. my $history_sql;
  27. my $history_rf;
  28. my %nets;
  29. my %dhcp_conf;
  30. foreach my $net (@office_network_list) {
  31. my $scope_name=$net;
  32. $scope_name =~s/\/\d+$//g;
  33. $nets{$scope_name}= new Net::Patricia;
  34. $nets{$scope_name}->add_string($net);
  35. }
  36. my $dhcp_networks = new Net::Patricia;
  37. my $now = DateTime->now(time_zone=>'local');
  38. $now->set(day=>1);
  39. my $month_start=$dbh->quote($now->ymd("-")." 00:00:00");
  40. my $month_dur = DateTime::Duration->new( months => 1 );
  41. my $next_month = $now + $month_dur;
  42. $next_month->set(day=>1);
  43. my $month_stop = $dbh->quote($next_month->ymd("-")." 00:00:00");
  44. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and vpn=0 ORDER BY ip_int_start');
  45. foreach my $subnet (@subnets) {
  46. $dhcp_networks->add_string($subnet->{subnet});
  47. my $subnet_name = $subnet->{subnet};
  48. $subnet_name=~s/\/\d+$//g;
  49. $dhcp_conf{$subnet_name}->{first_ip}=$subnet->{dhcp_start};
  50. $dhcp_conf{$subnet_name}->{last_ip}=$subnet->{dhcp_stop};
  51. }
  52. if ($day==1) {
  53. do_sql($dbh,"Update User_list set blocked=0");
  54. do_sql($dbh,"Update User_auth set blocked=0, changed=1 WHERE blocked=1 and deleted=0");
  55. db_log_verbose($dbh,"Amnistuyemo all blocked user by traffic for a month");
  56. } else {
  57. #month stat
  58. my $month_sql="SELECT User_list.id, User_list.login, SUM( traf_all ) AS traf_sum, User_list.month_quota as uquota
  59. FROM ( SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  60. WHERE User_stats.`timestamp`>=$month_start AND User_stats.`timestamp`< $month_stop
  61. GROUP BY User_stats.auth_id ) AS V, User_auth, User_list
  62. WHERE V.auth_id = User_auth.id AND User_auth.user_id = User_list.id and User_list.blocked=1 GROUP BY login";
  63. my @month_stats = get_records_sql($dbh,$month_sql);
  64. foreach my $row (@month_stats) {
  65. my $m_quota=$row->{uquota}*$KB*$KB;
  66. next if ($m_quota < $row->{traf_sum});
  67. db_log_info($dbh,"Amnistuyemo blocked user $row->{login} [$row->{id}] by traffic for a day");
  68. do_sql($dbh,"UPDATE User_list set blocked=0 WHERE id=$row->{id}");
  69. do_sql($dbh,"UPDATE User_auth set blocked=0, changed=1 WHERE user_id=$row->{id}");
  70. }
  71. }
  72. #### dhcpd ####
  73. my $dhcp_networks = new Net::Patricia;
  74. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets ORDER BY ip_int_start');
  75. foreach my $subnet (@subnets) {
  76. next if (!$subnet->{subnet} or ! $subnet->{dhcp_lease_time});
  77. $dhcp_networks->add_string($subnet->{subnet},$subnet->{dhcp_lease_time});
  78. }
  79. #clean temporary dhcp leases & connections only for dhcp pool ip
  80. my $users_sql = "SELECT * FROM User_auth WHERE deleted=0 AND (`user_id`=".$default_user_id." OR `user_id`=".$hotspot_user_id.")";
  81. my @users_auth = get_records_sql($dbh,$users_sql);
  82. foreach my $row (@users_auth) {
  83. next if (!is_dhcp_pool(\%dhcp_conf,$row->{ip_int}));
  84. my $last_dhcp_time = GetUnixTimeByStr($row->{dhcp_time});
  85. if ($dhcp_networks->match_string($row->{ip})) {
  86. my $clean_dhcp_time = $last_dhcp_time + 60*$dhcp_networks->match_string($row->{ip});
  87. if (time() - $clean_dhcp_time>0) {
  88. db_log_verbose($dbh,"Clean overdue dhcp leases for ip: $row->{ip} id: $row->{id} last dhcp: $row->{dhcp_time} clean time: ".GetTimeStrByUnixTime($clean_dhcp_time)." now: ".GetNowTime());
  89. do_sql($dbh,"DELETE FROM connections WHERE auth_id='".$row->{id}."'");
  90. do_sql($dbh,"DELETE FROM dhcp_log WHERE auth_id='".$row->{id}."'");
  91. do_sql($dbh,"UPDATE User_auth SET deleted=1 WHERE id='".$row->{id}."'");
  92. }
  93. }
  94. }
  95. #clean dhcp log
  96. my $clean_dhcp_log = time()- $history_dhcp*3600*24;
  97. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_dhcp_log);
  98. $month++;
  99. $year += 1900;
  100. my $clean_dhcp_log_str="$year-$month-$day";
  101. my $clean_dhcp_log_date=$dbh->quote($clean_dhcp_log_str);
  102. do_sql($dbh,"DELETE FROM dhcp_log WHERE `timestamp` < $clean_dhcp_log_date" );
  103. db_log_verbose($dbh,"Clean dhcp leases for all older that ".$clean_dhcp_log_str);
  104. ##### clean old connections ########
  105. my $clean_con_time = time()-$connections_history*60*60*24;
  106. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_con_time);
  107. $month++;
  108. $year += 1900;
  109. my $clean_con_str="$year-$month-$day";
  110. my $clean_con_date=$dbh->quote($clean_con_str);
  111. $users_sql = "SELECT id FROM User_auth WHERE `last_found` < $clean_con_date and last_found>0";
  112. db_log_debug($dbh,$users_sql) if ($debug);
  113. @users_auth=get_records_sql($dbh,$users_sql);
  114. foreach my $row (@users_auth) {
  115. db_log_debug($dbh,"Clear old connection for user_auth ".$row->{id}) if ($debug);
  116. do_sql($dbh,"DELETE FROM connections WHERE auth_id='".$row->{id}."'");
  117. }
  118. ##### clean dup connections ########
  119. my $conn_sql = "SELECT id,port_id,auth_id FROM connections order by port_id";
  120. my @conn_ref = get_records_sql($dbh,$conn_sql);
  121. my $old_port_id=0;
  122. my $old_auth_id=0;
  123. foreach my $row (@conn_ref) {
  124. my $c_id = $row->{id};
  125. my $c_port_id = $row->{port_id};
  126. my $c_auth_id = $row->{auth_id};
  127. if (!$c_port_id) { $c_port_id=0; }
  128. if (!$c_auth_id) { $c_auth_id=0; }
  129. if ($old_port_id ==0 or $old_auth_id==0) { $old_port_id=$c_port_id; $old_auth_id=$c_auth_id; next; }
  130. 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; }
  131. 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; }
  132. do_sql($dbh,"DELETE FROM connections WHERE id='".$c_id."'");
  133. db_log_verbose($dbh,"Remove dup connection $c_id: $c_port_id $c_auth_id");
  134. }
  135. ##### unknown mac clean ############
  136. $users_sql = "SELECT mac FROM User_auth WHERE deleted=0";
  137. @users_auth = get_records_sql($dbh,$users_sql);
  138. foreach my $row (@users_auth) {
  139. do_sql($dbh,"DELETE FROM Unknown_mac WHERE mac='".mac_simplify($row->{mac})."'");
  140. }
  141. ##### traffic detail ######
  142. my $clean_time = time()-$history*60*60*24;
  143. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  144. $month++;
  145. $year += 1900;
  146. my $clean_str="$year-$month-$day";
  147. my $clean_date=$dbh->quote($clean_str);
  148. db_log_verbose($dbh,"Clean traffic detail older that ".$clean_str);
  149. #clean old traffic detail
  150. do_sql($dbh,"DELETE FROM Traffic_detail WHERE `timestamp` < $clean_date" );
  151. ##### log ######
  152. $clean_time = time()-$history_log_day*60*60*24;
  153. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  154. $month++;
  155. $year += 1900;
  156. $clean_str="$year-$month-$day";
  157. $clean_date=$dbh->quote($clean_str);
  158. db_log_verbose($dbh,"Clean worklog older that ".$clean_str);
  159. do_sql($dbh,"DELETE FROM syslog WHERE `timestamp` < $clean_date" );
  160. ##### syslog ######
  161. $clean_time = time()-$history_syslog_day*60*60*24;
  162. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  163. $month++;
  164. $year += 1900;
  165. $clean_str="$year-$month-$day";
  166. $clean_date=$dbh->quote($clean_str);
  167. db_log_verbose($dbh,"Clean syslog older that ".$clean_str);
  168. do_sql($dbh,"DELETE FROM remote_syslog WHERE `date` < $clean_date" );
  169. ##### Traffic stats ######
  170. $clean_time = time()-$history_trafstat_day*60*60*24;
  171. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  172. $month++;
  173. $year += 1900;
  174. $clean_str="$year-$month-$day";
  175. $clean_date=$dbh->quote($clean_str);
  176. db_log_verbose($dbh,"Clean traffic statistics older that ".$clean_str);
  177. do_sql($dbh,"DELETE FROM User_stats WHERE `timestamp` < $clean_date" );
  178. ##### Traffic stats full ######
  179. my $iptraf_history = $config_ref{traffic_ipstat_history} || 30;
  180. $clean_time = time()-$iptraf_history*60*60*24;
  181. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_time);
  182. $month++;
  183. $year += 1900;
  184. $clean_str="$year-$month-$day";
  185. $clean_date=$dbh->quote($clean_str);
  186. db_log_verbose($dbh,"Clean traffic full statistics older that ".$clean_str);
  187. do_sql($dbh,"DELETE FROM User_stats_full WHERE `timestamp` < $clean_date" );
  188. #### clean unknown user ip
  189. do_sql($dbh,"DELETE FROM User_auth WHERE (mac is NULL or mac='') and deleted=1");
  190. db_log_info($dbh,'Garbage stopped.');
  191. $dbh->disconnect;
  192. exit 0;