stat-sync.pl 3.1 KB

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