stat-sync.pl 7.6 KB

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