stat-sync.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use English;
  8. use base;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use Data::Dumper;
  12. use eyelib::config;
  13. use eyelib::main;
  14. use eyelib::database;
  15. use eyelib::net_utils;
  16. use strict;
  17. use warnings;
  18. use Getopt::Long;
  19. use Proc::Daemon;
  20. use Cwd;
  21. use Net::Netmask;
  22. use DateTime;
  23. my $pf = '/run/stat-sync.pid';
  24. my $daemon = Proc::Daemon->new(
  25. pid_file => $pf,
  26. work_dir => $HOME_DIR
  27. );
  28. # are you running? Returns 0 if not.
  29. my $pid = $daemon->Status($pf);
  30. my $daemonize = 1;
  31. GetOptions(
  32. 'daemon!' => \$daemonize,
  33. "help" => \&usage,
  34. "reload" => \&reload,
  35. "restart" => \&restart,
  36. "start" => \&run,
  37. "status" => \&status,
  38. "stop" => \&stop
  39. ) or &usage;
  40. exit(0);
  41. sub stop {
  42. if ($pid) {
  43. print "Stopping pid $pid...";
  44. if ($daemon->Kill_Daemon($pf)) {
  45. print "Successfully stopped.\n";
  46. } else {
  47. print "Could not find $pid. Was it running?\n";
  48. }
  49. } else {
  50. print "Not running, nothing to stop.\n";
  51. }
  52. }
  53. sub status {
  54. if ($pid) {
  55. print "Running with pid $pid.\n";
  56. } else {
  57. print "Not running.\n";
  58. }
  59. }
  60. sub run {
  61. if (!$pid) {
  62. print "Starting...";
  63. if ($daemonize) {
  64. # when Init happens, everything under it runs in the child process.
  65. # this is important when dealing with file handles, due to the fact
  66. # Proc::Daemon shuts down all open file handles when Init happens.
  67. # Keep this in mind when laying out your program, particularly if
  68. # you use filehandles.
  69. $daemon->Init;
  70. }
  71. setpriority(0,0,19);
  72. while (1) {
  73. eval {
  74. # Create new database handle. If we can't connect, die()
  75. my $hdb = init_db();
  76. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  77. $urgent_sync=get_option($hdb,50);
  78. if ($urgent_sync) {
  79. #clean changed for dynamic clients or hotspot
  80. do_sql($hdb,"UPDATE User_auth SET changed=0 WHERE ou_id=".$default_user_ou_id." OR ou_id=".$default_hotspot_ou_id);
  81. do_sql($hdb,"UPDATE User_auth SET dhcp_changed=0 WHERE ou_id=".$default_user_ou_id." OR ou_id=".$default_hotspot_ou_id);
  82. #clean unmanagment ip changed
  83. my @all_changed = get_records_sql($hdb,"SELECT id, ip FROM User_auth WHERE changed = 1 OR dhcp_changed = 1");
  84. foreach my $row(@all_changed) {
  85. next if ($office_networks->match_string($row->{ip}));
  86. do_sql($hdb,"UPDATE User_auth SET changed = 0, dhcp_changed = 0 WHERE id=".$row->{id});
  87. }
  88. #dhcp changed records
  89. my $changed = get_record_sql($hdb,"SELECT COUNT(*) as c_count from User_auth WHERE dhcp_changed=1");
  90. if ($changed->{"c_count"}>0) {
  91. do_sql($hdb,"UPDATE User_auth SET dhcp_changed=0");
  92. log_info("Found changed dhcp variables in records: ".$changed->{'c_count'});
  93. my $dhcp_exec=get_option($hdb,38);
  94. my %result=do_exec_ref($dhcp_exec);
  95. if ($result{status} ne 0) { log_error("Error sync dhcp config"); }
  96. }
  97. #acl & dhcp changed records
  98. $changed = get_record_sql($hdb,"SELECT COUNT(*) as c_count from User_auth WHERE changed=1");
  99. if ($changed->{"c_count"}>0) {
  100. log_info("Found changed records: ".$changed->{'c_count'});
  101. my $acl_exec=get_option($hdb,37);
  102. my %result=do_exec_ref($acl_exec);
  103. if ($result{status} ne 0) { log_error("Error sync status at gateways"); }
  104. }
  105. }
  106. #dns changed records
  107. my @dns_changed = get_records_sql($hdb,"SELECT auth_id FROM `dns_queue` GROUP BY auth_id");
  108. if (@dns_changed and scalar @dns_changed) {
  109. foreach my $auth (@dns_changed) {
  110. update_dns_record($hdb,$auth->{auth_id});
  111. log_info("Clear changed dns for auth id: ".$auth->{auth_id});
  112. do_sql($hdb,"DELETE FROM `dns_queue` WHERE auth_id=".$auth->{auth_id});
  113. }
  114. }
  115. #clear temporary user auth records
  116. my $now = DateTime->now(time_zone=>'local');
  117. my $clear_time =$dbh->quote($now->strftime('%Y-%m-%d %H:%M:%S'));
  118. my $users_sql = "SELECT * FROM User_auth WHERE deleted=0 AND dynamic=1 AND `eof`<=".$clear_time;
  119. my @users_auth = get_records_sql($hdb,$users_sql);
  120. if (@users_auth and scalar @users_auth) {
  121. foreach my $row (@users_auth) {
  122. delete_record($hdb,"User_auth","id='".$row->{id}."'");
  123. db_log_info($hdb,"Removed dynamic user auth record for auth_id: $row->{'id'} by eof time: $row->{'eof'}",$row->{'id'});
  124. my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$row->{user_id});
  125. if (!$u_count) {
  126. delete_record($hdb,"User_list","id=".$row->{'user_id'});
  127. db_log_info($hdb,"Removed dynamic user id: $row->{'user_id'} by eof time");
  128. #delete binded device
  129. my $user_device = get_record_sql($hdb,"SELECT * FROM devices WHERE user_id=".$row->{id});
  130. if ($user_device) {
  131. db_log_info($hdb,"Remove corresponded device id: $user_device->{id} name: $user_device->{device_name}");
  132. unbind_ports($hdb, $user_device->{id});
  133. do_sql($hdb, "DELETE FROM connections WHERE device_id=".$user_device->{id});
  134. do_sql($hdb, "DELETE FROM device_l3_interfaces WHERE device_id=".$user_device->{id});
  135. do_sql($hdb, "DELETE FROM device_ports WHERE device_id=".$user_device->{id});
  136. delete_record($hdb, "devices", "id=".$user_device->{id});
  137. }
  138. }
  139. }
  140. }
  141. sleep(60);
  142. };
  143. if ($@) { log_error("Exception found: $@"); sleep(300); }
  144. }
  145. } else {
  146. print "Already Running with pid $pid\n";
  147. }
  148. }
  149. sub usage {
  150. print "usage: stat-sync.pl (start|stop|restart)\n";
  151. exit(0);
  152. }
  153. sub reload {
  154. print "reload process not implemented.\n";
  155. }
  156. sub restart {
  157. stop;
  158. run;
  159. }