hoststate-monitor.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin";
  7. use strict;
  8. use Time::Local;
  9. use FileHandle;
  10. use Rstat::config;
  11. use Rstat::mysql;
  12. use Rstat::main;
  13. use Rstat::nagios;
  14. use Data::Dumper;
  15. use Time::Local;
  16. use Date::Parse;
  17. use Getopt::Long;
  18. use Proc::Daemon;
  19. use Cwd;
  20. my $pf = '/var/run/nagios/hoststate-monitor.pid';
  21. my $log = '/var/log/nagios/logs/hoststate.log';
  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. if (!-e $log) { sleep(30); }
  73. open(hoststate, "tail -n 0 -F $log |") || die "$log not found!";
  74. my $hdb = init_db();
  75. while (my $logline = <hoststate>) {
  76. next unless defined $logline;
  77. chomp($logline);
  78. #db_log_debug($hdb,"Host event RAW: $logline");
  79. my ($date,$hoststate,$hoststatetype,$hostname,$hostip,$hostid,$hosttype,$svc_control)= split (/\|/, $logline);
  80. #db_log_debug($hdb,"Host event: $date,$hoststate,$hoststatetype,$hostname,$hostip,$hostid,$hosttype,$svc_control");
  81. next if (!$hostid);
  82. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  83. if (!$svc_control) { $svc_control=0; }
  84. if ($hoststate=~/UNREACHABLE/i) { $hoststate='DOWN'; }
  85. my $old_state = 'HARDDOWN';
  86. my $device = get_custom_record($hdb,'SELECT nagios_status,nagios_handler FROM User_auth WHERE id='.$hostid);
  87. if ($hosttype=~/device/i) { $device = get_custom_record($hdb,'SELECT nagios_status FROM devices WHERE id='.$hostid); }
  88. if ($device->{nagios_status}) { $old_state = $device->{nagios_status}; }
  89. db_log_debug($hdb,"Get old for $hostname [$hostip] id: $hostid type: $hosttype => state: $old_state");
  90. if ($hoststate eq "DOWN") { $hoststate=$hoststatetype.$hoststate; }
  91. db_log_debug($hdb,"Now for $hostname [$hostip] id: $hostid type: $hosttype => state: $hoststate");
  92. if ($hoststate !~ /$old_state/i) {
  93. #disable child
  94. my $full_action = ($svc_control eq 2);
  95. #Change device state
  96. db_log_debug($hdb,"Host changed! $hostname [$hostip] => $hoststate, old: $old_state");
  97. my $ip_aton=StrToIp($hostip);
  98. if ($hosttype=~/device/i) {
  99. do_sql($hdb,'UPDATE devices SET nagios_status="'.$hoststate.'" WHERE id='.$hostid);
  100. do_sql($hdb,'UPDATE User_auth SET nagios_status="'.$hoststate.'" WHERE deleted=0 AND ip_int='.$ip_aton);
  101. } else {
  102. do_sql($hdb,'UPDATE User_auth SET nagios_status="'.$hoststate.'" WHERE id='.$hostid);
  103. }
  104. if ($hoststate=~/UP/i) { nagios_host_svc_enable($hostname,1); }
  105. if ($hoststate=~/SOFTDOWN/i) { if ($svc_control) { nagios_host_svc_disable($hostname,$full_action); } }
  106. if ($hoststate=~/HARDDOWN/i) {
  107. if ($svc_control) { nagios_host_svc_disable($hostname,$full_action); }
  108. if ($device->{nagios_handler}) {
  109. db_log_info($hdb,"Event handler $device->{nagios_handler} for $hostname [$hostip] => $hoststate found!");
  110. if ($device->{nagios_handler}=~/restart-port/i) {
  111. my $run_cmd = "/usr/local/scripts/restart_port_snmp.pl $hostip & ";
  112. db_log_info($hdb,"Nagios eventhandler restart-port started for ip: $hostip");
  113. db_log_info($hdb,"Run handler: $run_cmd");
  114. system($run_cmd);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. close(hoststate);
  121. };
  122. if ($@) { log_error("Exception found: $@"); }
  123. sleep(30);
  124. }
  125. } else {
  126. log_error("Already Running with pid $pid");
  127. }
  128. }
  129. sub usage {
  130. print "usage: hoststate-monitor.pl (start|stop|status|restart)\n";
  131. exit(0);
  132. }
  133. sub reload {
  134. print "reload process not implemented.\n";
  135. }
  136. sub restart {
  137. stop;
  138. run;
  139. }