1
0

stat-sync.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_record_sql($hdb,"SELECT COUNT(*) as c_count from User_auth WHERE changed=1");
  79. if ($changed->{"c_count"}>0) {
  80. log_info("Found changed records: ".$changed->{'c_count'});
  81. my %result=do_exec_ref($HOME_DIR."/sync_mikrotik.pl");
  82. if ($result{status} ne 0) { log_error("Error sync status at gateways"); }
  83. }
  84. }
  85. sleep(60);
  86. };
  87. if ($@) { log_error("Exception found: $@"); sleep(300); }
  88. }
  89. } else {
  90. print "Already Running with pid $pid\n";
  91. }
  92. }
  93. sub usage {
  94. print "usage: stat-sync.pl (start|stop|restart)\n";
  95. exit(0);
  96. }
  97. sub reload {
  98. print "reload process not implemented.\n";
  99. }
  100. sub restart {
  101. stop;
  102. run;
  103. }