1
0

garbage.pl 8.4 KB

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