|
|
@@ -22,38 +22,12 @@ use eyelib::common;
|
|
|
use eyelib::logconfig;
|
|
|
use eyelib::net_utils;
|
|
|
use strict;
|
|
|
-use Getopt::Long;
|
|
|
-use Proc::Daemon;
|
|
|
use Cwd;
|
|
|
use Net::Netmask;
|
|
|
use DateTime;
|
|
|
|
|
|
my $mute_time=300;
|
|
|
|
|
|
-my $pf = '/run/eye-sync/stat-sync.pid';
|
|
|
-
|
|
|
-my $daemon = Proc::Daemon->new(
|
|
|
- pid_file => $pf,
|
|
|
- work_dir => $HOME_DIR
|
|
|
-);
|
|
|
-
|
|
|
-# are you running? Returns 0 if not.
|
|
|
-my $pid = $daemon->Status($pf);
|
|
|
-
|
|
|
-my $daemonize = 1;
|
|
|
-
|
|
|
-GetOptions(
|
|
|
- 'daemon!' => \$daemonize,
|
|
|
- "help" => \&usage,
|
|
|
- "reload" => \&reload,
|
|
|
- "restart" => \&restart,
|
|
|
- "start" => \&run,
|
|
|
- "status" => \&status,
|
|
|
- "stop" => \&stop
|
|
|
-) or &usage;
|
|
|
-
|
|
|
-exit(0);
|
|
|
-
|
|
|
### Analyze DHCP requests
|
|
|
|
|
|
# === 1. Получение событий из очереди DHCP ===
|
|
|
@@ -107,7 +81,7 @@ sub _process_single_dhcp_event {
|
|
|
log_debug("Deleted DHCP event ID $event->{id} from queue (affected rows: $rows)");
|
|
|
# Проверяем, что запись создана/обновлена
|
|
|
if (!$dhcp_record or !$dhcp_record->{auth_id} ){
|
|
|
- log_error("User ip auth record not created by DHCP request for ip: $ip mac: $event->{mac}!");
|
|
|
+ wrlog($W_ERROR,"User ip auth record not created by DHCP request for ip: $ip mac: $event->{mac}!");
|
|
|
return;
|
|
|
}
|
|
|
# Обновляем кэш последних событий
|
|
|
@@ -121,18 +95,18 @@ sub process_dhcp_queue {
|
|
|
# Получаем все события
|
|
|
my @dhcp_events = _fetch_dhcp_queue($hdb);
|
|
|
return unless @dhcp_events;
|
|
|
- log_info("Processing " . scalar(@dhcp_events) . " DHCP event(s) from queue");
|
|
|
+ wrlog($W_INFO,"Processing " . scalar(@dhcp_events) . " DHCP event(s) from queue");
|
|
|
# Обрабатываем каждое событие
|
|
|
foreach my $dhcp (@dhcp_events) {
|
|
|
eval {
|
|
|
_process_single_dhcp_event($hdb, $dhcp, $leases_ref, $mute_time);
|
|
|
};
|
|
|
if ($@) {
|
|
|
- log_error("Failed to process DHCP event ID $dhcp->{id}: $@");
|
|
|
+ wrlog($W_ERROR,"Failed to process DHCP event ID $dhcp->{id}: $@");
|
|
|
# Не прерываем остальные события
|
|
|
}
|
|
|
}
|
|
|
- log_info("DHCP queue processing completed");
|
|
|
+ wrlog($W_INFO,"DHCP queue processing completed");
|
|
|
}
|
|
|
|
|
|
### UPDATE user state
|
|
|
@@ -162,7 +136,7 @@ sub _clear_changed_flags_for_non_office_ips {
|
|
|
$cleared += $rows;
|
|
|
}
|
|
|
if ($cleared) {
|
|
|
- log_info("Cleared 'changed' flags for $cleared records with non-office IPs");
|
|
|
+ wrlog($W_INFO,"Cleared 'changed' flags for $cleared records with non-office IPs");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -172,7 +146,7 @@ sub _process_dhcp_changes {
|
|
|
my $changed = get_record_sql($dbh, "SELECT COUNT(*) AS c_count FROM user_auth WHERE dhcp_changed = 1");
|
|
|
my $count = $changed ? ($changed->{c_count} // 0) : 0;
|
|
|
return if $count == 0;
|
|
|
- log_info("Found $count record(s) with dhcp_changed=1");
|
|
|
+ wrlog($W_INFO,"Found $count record(s) with dhcp_changed=1");
|
|
|
# Сбрасываем флаги
|
|
|
do_sql($dbh, "UPDATE user_auth SET dhcp_changed = 0");
|
|
|
# Запускаем внешний скрипт
|
|
|
@@ -183,9 +157,9 @@ sub _process_dhcp_changes {
|
|
|
}
|
|
|
my %result = do_exec_ref("/usr/bin/sudo $dhcp_exec");
|
|
|
if ($result{status} != 0) {
|
|
|
- log_error("DHCP config sync failed: " . ($result{stderr} // 'no error output'));
|
|
|
+ wrlog($W_ERROR,"DHCP config sync failed: " . ($result{stderr} // 'no error output'));
|
|
|
} else {
|
|
|
- log_info("DHCP config synced successfully");
|
|
|
+ wrlog($W_INFO,"DHCP config synced successfully");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -195,7 +169,7 @@ sub _process_acl_changes {
|
|
|
my $changed = get_record_sql($dbh, "SELECT COUNT(*) AS c_count FROM user_auth WHERE changed = 1");
|
|
|
my $count = $changed ? ($changed->{c_count} // 0) : 0;
|
|
|
return if $count == 0;
|
|
|
- log_info("Found $count record(s) with changed=1 (ACL/DHCP)");
|
|
|
+ wrlog($W_INFO,"Found $count record(s) with changed=1 (ACL/DHCP)");
|
|
|
my $acl_exec = get_option($dbh, 37);
|
|
|
if (!$acl_exec) {
|
|
|
log_warning("ACL sync script (opt 37) not configured");
|
|
|
@@ -203,9 +177,9 @@ sub _process_acl_changes {
|
|
|
}
|
|
|
my %result = do_exec_ref("$acl_exec --changes-only");
|
|
|
if ($result{status} != 0) {
|
|
|
- log_error("Gateway ACL sync failed: " . ($result{stderr} // 'no error output'));
|
|
|
+ wrlog($W_ERROR,"Gateway ACL sync failed: " . ($result{stderr} // 'no error output'));
|
|
|
} else {
|
|
|
- log_info("Gateway ACL synced successfully");
|
|
|
+ wrlog($W_INFO,"Gateway ACL synced successfully");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -214,16 +188,16 @@ sub _process_dns_queue {
|
|
|
my ($dbh) = @_;
|
|
|
my @dns_changed = get_records_sql($dbh, "SELECT DISTINCT auth_id FROM dns_queue");
|
|
|
return unless @dns_changed;
|
|
|
- log_info("Processing DNS queue for " . scalar(@dns_changed) . " auth_id(s)");
|
|
|
+ wrlog($W_INFO,"Processing DNS queue for " . scalar(@dns_changed) . " auth_id(s)");
|
|
|
for my $auth (@dns_changed) {
|
|
|
my $auth_id = $auth->{auth_id};
|
|
|
eval {
|
|
|
update_dns_record($dbh, $auth_id);
|
|
|
do_sql($dbh, "DELETE FROM dns_queue WHERE auth_id = ?", $auth_id);
|
|
|
- log_info("DNS processed and cleared for auth_id: $auth_id");
|
|
|
+ wrlog($W_INFO,"DNS processed and cleared for auth_id: $auth_id");
|
|
|
};
|
|
|
if ($@) {
|
|
|
- log_error("Failed to process DNS for auth_id=$auth_id: $@");
|
|
|
+ wrlog($W_ERROR,"Failed to process DNS for auth_id=$auth_id: $@");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -238,20 +212,20 @@ sub _cleanup_expired_dynamic_users {
|
|
|
$now_str
|
|
|
);
|
|
|
return unless @users_auth;
|
|
|
- log_info("Cleaning up " . scalar(@users_auth) . " expired dynamic user_auth records");
|
|
|
+ wrlog($W_INFO,"Cleaning up " . scalar(@users_auth) . " expired dynamic user_auth records");
|
|
|
for my $row (@users_auth) {
|
|
|
eval {
|
|
|
delete_user_auth($dbh, $row->{id});
|
|
|
- db_log_info($dbh, "Removed dynamic user auth record for auth_id: $row->{id} by end_life time: $row->{end_life}", $row->{id});
|
|
|
+ db_wrlog($W_INFO,$dbh, "Removed dynamic user auth record for auth_id: $row->{id} by end_life time: $row->{end_life}", $row->{id});
|
|
|
# Удаляем пользователя, если больше нет активных auth-записей
|
|
|
my $u_count = get_count_records($dbh, 'user_auth', 'deleted = 0 AND user_id = ?', $row->{user_id});
|
|
|
if ($u_count == 0) {
|
|
|
delete_user($dbh, $row->{user_id});
|
|
|
- log_info("Deleted orphaned user_id: $row->{user_id}");
|
|
|
+ wrlog($W_INFO,"Deleted orphaned user_id: $row->{user_id}");
|
|
|
}
|
|
|
};
|
|
|
if ($@) {
|
|
|
- log_error("Error cleaning up auth_id $row->{id}: $@");
|
|
|
+ wrlog($W_ERROR,"Error cleaning up auth_id $row->{id}: $@");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -265,7 +239,7 @@ sub refresh_config_if_needed {
|
|
|
init_option($hdb);
|
|
|
my $urgent_sync = get_option($hdb, 50);
|
|
|
if ($urgent_sync) {
|
|
|
- log_info("Urgent sync triggered (option 50)");
|
|
|
+ wrlog($W_INFO,"Urgent sync triggered (option 50)");
|
|
|
_reset_changed_flags_for_default_ous($hdb, $default_user_ou_id, $default_hotspot_ou_id);
|
|
|
_clear_changed_flags_for_non_office_ips($hdb, $office_networks);
|
|
|
_process_dhcp_changes($hdb);
|
|
|
@@ -277,54 +251,19 @@ sub refresh_config_if_needed {
|
|
|
log_debug("Config refresh cycle completed");
|
|
|
}
|
|
|
|
|
|
-sub stop {
|
|
|
- if ($pid) {
|
|
|
- print "Stopping pid $pid...";
|
|
|
- if ($daemon->Kill_Daemon($pf)) {
|
|
|
- print "Successfully stopped.\n";
|
|
|
- } else {
|
|
|
- print "Could not find $pid. Was it running?\n";
|
|
|
- }
|
|
|
- } else {
|
|
|
- print "Not running, nothing to stop.\n";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-sub status {
|
|
|
- if ($pid) {
|
|
|
- print "Running with pid $pid.\n";
|
|
|
- } else {
|
|
|
- print "Not running.\n";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-sub run {
|
|
|
-if (!$pid) {
|
|
|
- print "Starting...";
|
|
|
- if ($daemonize) {
|
|
|
- # when Init happens, everything under it runs in the child process.
|
|
|
- # this is important when dealing with file handles, due to the fact
|
|
|
- # Proc::Daemon shuts down all open file handles when Init happens.
|
|
|
- # Keep this in mind when laying out your program, particularly if
|
|
|
- # you use filehandles.
|
|
|
- $daemon->Init;
|
|
|
- }
|
|
|
+wrlog($W_INFO,"Starting...");
|
|
|
|
|
|
- setpriority(0,0,19);
|
|
|
+setpriority(0,0,19);
|
|
|
|
|
|
- my %leases;
|
|
|
+my %leases;
|
|
|
|
|
|
- while (1) {
|
|
|
+while (1) {
|
|
|
|
|
|
eval {
|
|
|
-
|
|
|
# Create new database handle. If we can't connect, die()
|
|
|
my $hdb = init_db();
|
|
|
-
|
|
|
# Process DHCP queue every 10 seconds
|
|
|
process_dhcp_queue($hdb, \%leases, $mute_time);
|
|
|
-
|
|
|
-
|
|
|
# Update state every 60 seconds
|
|
|
refresh_config_if_needed(
|
|
|
$hdb,
|
|
|
@@ -334,25 +273,10 @@ if (!$pid) {
|
|
|
$office_networks
|
|
|
);
|
|
|
sleep(10);
|
|
|
-
|
|
|
};
|
|
|
- if ($@) { log_error("Exception found: $@"); sleep(300); }
|
|
|
- }
|
|
|
- } else {
|
|
|
- print "Already Running with pid $pid\n";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-sub usage {
|
|
|
- print "usage: stat-sync.pl (start|stop|restart)\n";
|
|
|
- exit(0);
|
|
|
+ if ($@) { wrlog($W_ERROR,"Exception found: $@"); sleep(300); }
|
|
|
}
|
|
|
|
|
|
-sub reload {
|
|
|
- print "reload process not implemented.\n";
|
|
|
-}
|
|
|
+wrlog($W_INFO,"Process stopped.");
|
|
|
|
|
|
-sub restart {
|
|
|
- stop;
|
|
|
- run;
|
|
|
-}
|
|
|
+exit 0;
|