stat-sync.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use Data::Dumper;
  8. use Rstat::config;
  9. use Rstat::main;
  10. use Rstat::mysql;
  11. use Rstat::net_utils;
  12. use strict;
  13. use warnings;
  14. use Getopt::Long;
  15. use Proc::Daemon;
  16. use Cwd;
  17. use Net::Netmask;
  18. my $pf = '/var/run/stat-sync.pid';
  19. my $daemon = Proc::Daemon->new(
  20. pid_file => $pf,
  21. work_dir => $HOME_DIR
  22. );
  23. # are you running? Returns 0 if not.
  24. my $pid = $daemon->Status($pf);
  25. my $daemonize = 1;
  26. GetOptions(
  27. 'daemon!' => \$daemonize,
  28. "help" => \&usage,
  29. "reload" => \&reload,
  30. "restart" => \&restart,
  31. "start" => \&run,
  32. "status" => \&status,
  33. "stop" => \&stop
  34. ) or &usage;
  35. exit(0);
  36. sub stop {
  37. if ($pid) {
  38. print "Stopping pid $pid...";
  39. if ($daemon->Kill_Daemon($pf)) {
  40. print "Successfully stopped.\n";
  41. } else {
  42. print "Could not find $pid. Was it running?\n";
  43. }
  44. } else {
  45. print "Not running, nothing to stop.\n";
  46. }
  47. }
  48. sub status {
  49. if ($pid) {
  50. print "Running with pid $pid.\n";
  51. } else {
  52. print "Not running.\n";
  53. }
  54. }
  55. sub run {
  56. if (!$pid) {
  57. print "Starting...";
  58. if ($daemonize) {
  59. # when Init happens, everything under it runs in the child process.
  60. # this is important when dealing with file handles, due to the fact
  61. # Proc::Daemon shuts down all open file handles when Init happens.
  62. # Keep this in mind when laying out your program, particularly if
  63. # you use filehandles.
  64. $daemon->Init;
  65. }
  66. setpriority(0,0,19);
  67. while (1) {
  68. eval {
  69. # Create new database handle. If we can't connect, die()
  70. my $hdb = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  71. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  72. if ( !defined $hdb ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  73. $urgent_sync=get_option($hdb,50);
  74. if ($urgent_sync) {
  75. my @changed = get_records_sql($hdb,"SELECT * from User_auth WHERE changed=1 and (user_id<>".$default_user_id." and user_id<>".$hotspot_user_id.")");
  76. if (@changed and scalar @changed>0) {
  77. log_info("Found changed records: ".Dumper(\@changed));
  78. my %result=do_exec_ref($HOME_DIR."/sync_mikrotik.pl");
  79. if ($result{status} eq 0) {
  80. foreach my $auth (@changed) {
  81. next if (!$auth);
  82. my $update_record;
  83. $update_record->{changed}=0;
  84. update_record($hdb,'User_auth',$update_record,"id=$auth->{id}");
  85. }
  86. }
  87. }
  88. }
  89. sleep(60);
  90. };
  91. if ($@) { log_error("Exception found: $@"); sleep(300); }
  92. }
  93. } else {
  94. print "Already Running with pid $pid\n";
  95. }
  96. }
  97. sub usage {
  98. print "usage: stat-sync.pl (start|stop|restart)\n";
  99. exit(0);
  100. }
  101. sub reload {
  102. print "reload process not implemented.\n";
  103. }
  104. sub restart {
  105. stop;
  106. run;
  107. }