Selaa lähdekoodia

added: clean empty user records

root 2 vuotta sitten
vanhempi
sitoutus
4dfc2b0434

+ 2 - 4
html/admin/customers/control-options.php

@@ -83,8 +83,7 @@ print_control_submenu($page_url);
 
             <?php
             $descr_field = "description." . HTML_LANG;
-            $t_config = mysqli_query($db_link, "SELECT `config`.`id`,`option_id`,`option_name`,`value`,`type`,`" . $descr_field . "`,`min_value`,`max_value` FROM `config`,`config_options` WHERE
- `config`.`option_id`=`config_options`.`id` ORDER BY `option_name`");
+            $t_config = mysqli_query($db_link, "SELECT `config`.`id`,`option_id`,`option_name`,`value`,`type`,`" . $descr_field . "`,`min_value`,`max_value` FROM `config`,`config_options` WHERE `config`.`option_id`=`config_options`.`id` ORDER BY `option_name`");
             while ($row = mysqli_fetch_array($t_config)) {
                 print "<tr align=center>\n";
                 print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_id[" . $row["option_id"] . "] value='" . $row['id'] . "'></td>\n";
@@ -122,8 +121,7 @@ print_control_submenu($page_url);
             }
             ?>
             <tr>
-                <td colspan=4 class="data"><?php print WEB_btn_add . " " . mb_strtolower(WEB_config_option) . ":&nbsp";
-                                            print_option_select($db_link, "f_new_option"); ?></td>
+                <td colspan=4 class="data"><?php print WEB_btn_add . " " . mb_strtolower(WEB_config_option) . ":&nbsp"; print_option_select($db_link, "f_new_option"); ?></td>
                 <td class="up" align="center"><input type="submit" name="create" value="<?php echo WEB_btn_add; ?>"></td>
                 <td colspan=2 class="data"></td>
             </tr>

+ 9 - 8
html/inc/common.php

@@ -867,7 +867,7 @@ function get_queue($db, $queue_value)
     return $queue_name;
 }
 
-function print_qa_l3int_select($qa_name, $qa_value)
+function print_qa_l3int_select($qa_name, $qa_value = 0)
 {
     print "<select name=\"$qa_name\">\n";
     print_select_item(WEB_select_item_lan, 0, $qa_value);
@@ -875,7 +875,7 @@ function print_qa_l3int_select($qa_name, $qa_value)
     print "</select>\n";
 }
 
-function print_qa_rule_select($qa_name, $qa_value)
+function print_qa_rule_select($qa_name, $qa_value = 1)
 {
     print "<select name=\"$qa_name\">\n";
     print_select_item('Subnet', 1, $qa_value);
@@ -884,15 +884,16 @@ function print_qa_rule_select($qa_name, $qa_value)
     print "</select>\n";
 }
 
-function print_qa_select($qa_name, $qa_value)
+function print_qa_select($qa_name, $qa_value = 0)
 {
     print "<select name=\"$qa_name\">\n";
+    if (empty($qa_value)) { $qa_value = 0; } else { $qa_value = $qa_value*1; }
     print_select_item(WEB_select_item_yes, 1, $qa_value);
     print_select_item(WEB_select_item_no, 0, $qa_value);
     print "</select>\n";
 }
 
-function print_qa_select_ext($qa_name, $qa_value, $readonly)
+function print_qa_select_ext($qa_name, $qa_value = 0, $readonly = 1)
 {
     $state = '';
     if ($readonly) {
@@ -904,7 +905,7 @@ function print_qa_select_ext($qa_name, $qa_value, $readonly)
     print "</select>\n";
 }
 
-function print_td_yes_no($qa_value)
+function print_td_yes_no($qa_value = 0)
 {
     $cl = 'down';
     if ($qa_value==1) { $cl='up'; }
@@ -913,7 +914,7 @@ function print_td_yes_no($qa_value)
     print "</td>\n";
 }
 
-function print_control_proto_select($qa_name, $qa_value)
+function print_control_proto_select($qa_name, $qa_value = -1)
 {
     print "<select name=\"$qa_name\">\n";
     print_select_item('Disabled', -1, $qa_value);
@@ -923,7 +924,7 @@ function print_control_proto_select($qa_name, $qa_value)
     print "</select>\n";
 }
 
-function print_snmp_select($qa_name, $qa_value)
+function print_snmp_select($qa_name, $qa_value = 0)
 {
     print "<select name=\"$qa_name\">\n";
     print_select_item('Disabled', 0, $qa_value);
@@ -933,7 +934,7 @@ function print_snmp_select($qa_name, $qa_value)
     print "</select>\n";
 }
 
-function print_dhcp_select($qa_name, $qa_value)
+function print_dhcp_select($qa_name, $qa_value = 0)
 {
     print "<select name=\"$qa_name\">\n";
     if (!isset($qa_value) or strlen($qa_value) == 0) {

+ 2 - 0
scripts/eyelib/mysql.pm

@@ -1144,6 +1144,8 @@ $config_ref{wiki_path} = get_option($db,61);
 $config_ref{auto_mac_rule} = get_option($db,64);
 #network configuration mode
 $config_ref{config_mode}=get_option($db,68);
+#auto clean old user record
+$config_ref{clean_empty_user}=get_option($db,69);
 
 #$save_detail = 1; id=23
 $save_detail=get_option($db,23);

+ 45 - 13
scripts/garbage.pl

@@ -70,11 +70,14 @@ $dhcp_conf{$subnet_name}->{last_ip}=$subnet->{dhcp_stop};
 }
 
 if ($day==1) {
+    db_log_info($dbh,'Monthly amnesty started');
+    db_log_verbose($dbh,"Amnistuyemo all blocked user by traffic for a month");
     do_sql($dbh,"Update User_list set blocked=0");
     do_sql($dbh,"Update User_auth set blocked=0, changed=1  WHERE blocked=1 and deleted=0");
-    db_log_verbose($dbh,"Amnistuyemo all blocked user by traffic for a month");
+    db_log_info($dbh,'Monthly amnesty stopped');
     } else {
     #month stat
+    db_log_info($dbh,'Daily statistics started');
     my $month_sql="SELECT User_list.id, User_list.login, SUM( traf_all ) AS traf_sum, User_list.month_quota as uquota
     FROM ( SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats 
     WHERE User_stats.`timestamp`>=$month_start AND User_stats.`timestamp`< $month_stop  
@@ -88,8 +91,10 @@ if ($day==1) {
         do_sql($dbh,"UPDATE User_list set blocked=0 WHERE id=$row->{id}");
         do_sql($dbh,"UPDATE User_auth set blocked=0, changed=1 WHERE user_id=$row->{id}");
         }
+    db_log_info($dbh,'Daily statistics stopped');
     }
 
+db_log_info($dbh,'Clean dhcp leases for dynamic hosts with overdue lease time');
 #clean temporary dhcp leases & connections only for dhcp pool ip
 my $users_sql = "SELECT * FROM User_auth WHERE deleted=0 AND (`ou_id`=".$default_user_ou_id." OR `ou_id`=".$default_hotspot_ou_id.")";
 my @users_auth = get_records_sql($dbh,$users_sql);
@@ -105,11 +110,12 @@ if ($dhcp_networks->match_string($row->{ip})) {
         my $u_count=get_count_records($dbh,'User_auth','deleted=0 and user_id='.$row->{user_id});
         if (!$u_count) {
 		delete_record($dbh,"User_list","id=".$row->{'user_id'});
-		db_log_verbose($dbh,"Remove dynamic user id: $row->{'user_id'} by dhcp lease timeout");
+		db_log_info($dbh,"Remove dynamic user id: $row->{'user_id'} by dhcp lease timeout");
 	        }
         }
     }
 }
+db_log_info($dbh,'Finished');
 
 $now = DateTime->now(time_zone=>'local');
 my $day_dur = DateTime::Duration->new( days => $history_dhcp );
@@ -117,10 +123,12 @@ my $clean_date = $now - $day_dur;
 my $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
 #clean dhcp log
+db_log_info($dbh,'Clearing outdated records dhcp log');
 do_sql($dbh,"DELETE FROM dhcp_log WHERE `timestamp` < $clean_str" );
 db_log_verbose($dbh,"Clean dhcp leases for all older that ".$clean_str);
 
 ##### clean old connections ########
+db_log_info($dbh,'Clearing outdated connection records');
 $day_dur = DateTime::Duration->new( days => $connections_history );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
@@ -129,11 +137,12 @@ $users_sql = "SELECT id FROM User_auth WHERE `last_found` < $clean_str and last_
 db_log_debug($dbh,$users_sql) if ($debug);
 @users_auth=get_records_sql($dbh,$users_sql);
 foreach my $row (@users_auth) {
-db_log_debug($dbh,"Clear old connection for user_auth ".$row->{id}) if ($debug);
+db_log_debug($dbh,"Clear old connection for user_auth ".$row->{id});
 do_sql($dbh,"DELETE FROM connections WHERE auth_id='".$row->{id}."'");
 }
 
 ##### clean dup connections ########
+db_log_info($dbh,'Clearing duplicated connection records');
 my $conn_sql = "SELECT id,port_id,auth_id FROM connections order by port_id";
 my @conn_ref = get_records_sql($dbh,$conn_sql);
 my $old_port_id=0;
@@ -148,19 +157,20 @@ if ($old_port_id ==0 or $old_auth_id==0) { $old_port_id=$c_port_id; $old_auth_id
 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; }
 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; }
 do_sql($dbh,"DELETE FROM connections WHERE id='".$c_id."'");
-db_log_verbose($dbh,"Remove dup connection $c_id: $c_port_id $c_auth_id");
+db_log_info($dbh,"Remove dup connection $c_id: $c_port_id $c_auth_id");
 }
 
-##### clean empty user account and corresponded devices ################
+##### clean empty user account and corresponded devices for dynamic users and hotspot ################
+db_log_info($dbh,'Clearing empty user account and corresponded devices for dynamic users and hotspot');
 my $u_sql = "SELECT * FROM User_list as U WHERE (U.ou_id=".$default_hotspot_ou_id." OR U.ou_id=".$default_user_ou_id.") AND (SELECT COUNT(*) FROM User_auth WHERE User_auth.deleted=0 AND User_auth.user_id = U.id)=0";
 my @u_ref = get_records_sql($dbh,$u_sql);
 foreach my $row (@u_ref) {
 do_sql($dbh,"DELETE FROM User_list WHERE id='".$row->{id}."'");
-db_log_verbose($dbh,"Remove empty user id: $row->{id} login: $row->{login}");
+db_log_info($dbh,"Remove empty dynamic user with id: $row->{id} login: $row->{login}");
 #delete binded device
 my $user_device = get_record_sql($dbh,"SELECT * FROM devices WHERE user_id=".$row->{id});
 if ($user_device) {
-    db_log_verbose($dbh,"Remove corresponded device id: $user_device->{id} name: $user_device->{device_name}");
+    db_log_info($dbh,"Remove corresponded device id: $user_device->{id} name: $user_device->{device_name}");
     unbind_ports($dbh, $user_device->{id});
     do_sql($dbh, "DELETE FROM connections WHERE device_id=".$user_device->{id});
     do_sql($dbh, "DELETE FROM device_l3_interfaces WHERE device_id=".$user_device->{id});
@@ -169,7 +179,29 @@ if ($user_device) {
     }
 }
 
+##### clean empty user account and corresponded devices if there are no rules for automatic linking ################
+if ($config_ref{clean_empty_user}) {
+    db_log_info($dbh,'Clearing empty user account and corresponded devices if there are no rules for automatic linking');
+    my $u_sql = "SELECT * FROM User_list as U WHERE (SELECT COUNT(*) FROM User_auth WHERE User_auth.deleted=0 AND User_auth.user_id = U.id)=0 AND (SELECT COUNT(*) FROM auth_rules WHERE auth_rules.user_id = U.id)=0";
+    my @u_ref = get_records_sql($dbh,$u_sql);
+    foreach my $row (@u_ref) {
+        do_sql($dbh,"DELETE FROM User_list WHERE id='".$row->{id}."'");
+        db_log_info($dbh,"Remove empty user with id: $row->{id} login: $row->{login}");
+        #delete binded device
+        my $user_device = get_record_sql($dbh,"SELECT * FROM devices WHERE user_id=".$row->{id});
+        if ($user_device) {
+            db_log_info($dbh,"Remove corresponded device id: $user_device->{id} name: $user_device->{device_name}");
+            unbind_ports($dbh, $user_device->{id});
+            do_sql($dbh, "DELETE FROM connections WHERE device_id=".$user_device->{id});
+            do_sql($dbh, "DELETE FROM device_l3_interfaces WHERE device_id=".$user_device->{id});
+            do_sql($dbh, "DELETE FROM device_ports WHERE device_id=".$user_device->{id});
+            delete_record($dbh, "devices", "id=".$user_device->{id});
+            }
+        }
+    }
+
 ##### unknown mac clean ############
+db_log_info($dbh,'Clearing unknown mac if it found in current User_auth table');
 $users_sql = "SELECT mac FROM User_auth WHERE deleted=0";
 @users_auth = get_records_sql($dbh,$users_sql);
 foreach my $row (@users_auth) {
@@ -183,7 +215,7 @@ $day_dur = DateTime::Duration->new( days => $history );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean traffic detail older that ".$clean_str);
+db_log_info($dbh,"Clean traffic detail older that ".$clean_str);
 #clean old traffic detail
 do_sql($dbh,"DELETE FROM Traffic_detail WHERE `timestamp` < $clean_str" );
 
@@ -193,7 +225,7 @@ $day_dur = DateTime::Duration->new( days => $history_log_day );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean worklog older that ".$clean_str);
+db_log_info($dbh,"Clean worklog older that ".$clean_str);
 do_sql($dbh,"DELETE FROM syslog WHERE `timestamp` < $clean_str" );
 
 #clean debug logs older than 2 days
@@ -201,7 +233,7 @@ $day_dur = DateTime::Duration->new( days => 2 );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean debug worklog older that ".$clean_str);
+db_log_info($dbh,"Clean debug worklog older that ".$clean_str);
 do_sql($dbh,"DELETE FROM syslog WHERE level>3 AND `timestamp` < $clean_str" );
 
 ##### remote syslog  ######
@@ -210,7 +242,7 @@ $day_dur = DateTime::Duration->new( days => $history_syslog_day );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean syslog older that ".$clean_str);
+db_log_info($dbh,"Clean syslog older that ".$clean_str);
 do_sql($dbh,"DELETE FROM remote_syslog WHERE `date` < $clean_str" );
 
 ##### Traffic stats  ######
@@ -219,7 +251,7 @@ $day_dur = DateTime::Duration->new( days => $history_trafstat_day );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean traffic statistics older that ".$clean_str);
+db_log_info($dbh,"Clean traffic statistics older that ".$clean_str);
 do_sql($dbh,"DELETE FROM User_stats WHERE `timestamp` < $clean_str" );
 
 ##### Traffic stats full ######
@@ -230,7 +262,7 @@ $day_dur = DateTime::Duration->new( days => $iptraf_history );
 $clean_date = $now - $day_dur;
 $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
 
-db_log_verbose($dbh,"Clean traffic full statistics older that ".$clean_str);
+db_log_info($dbh,"Clean traffic full statistics older that ".$clean_str);
 do_sql($dbh,"DELETE FROM User_stats_full WHERE `timestamp` < $clean_str" );
 
 #### clean unknown user ip

+ 1 - 0
updates/2-4-14/device_lock.sql

@@ -1 +1,2 @@
 ALTER TABLE `devices` ADD `discovery_locked` BOOLEAN NOT NULL DEFAULT FALSE AFTER `deleted`, ADD `locked_timestamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP AFTER `discovery_locked`;
+

+ 4 - 0
updates/2-4-14/options.sql

@@ -0,0 +1,4 @@
+INSERT INTO `config_options` (`id`, `option_name`, `description.russian`, `description.english`, `uniq`, `type`, `default_value`, `min_value`, `max_value`) VALUES ('69', 'clean_empty_user', 'Автоматически удалять записи пользователей, не содержащие ip-адресов или автоматических привязок', 'Automatically delete user records that do not contain IP addresses or automatic bindings', '1', 'bool', '0', '0', '1');
+UPDATE `config_options` SET `min_value` = '0' WHERE `config_options`.`min_value`<>0 AND `config_options`.`type`='bool';
+UPDATE `config_options` SET `max_value` = '1' WHERE `config_options`.`max_value`<>1 AND `config_options`.`type`='bool';
+