1
0

stat-sync.pl 6.3 KB

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