hmonitor.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use English;
  11. use FindBin '$Bin';
  12. use lib "/opt/Eye/scripts";
  13. use strict;
  14. use Time::Local;
  15. use FileHandle;
  16. use eyelib::config;
  17. use eyelib::database;
  18. use eyelib::common;
  19. use eyelib::main;
  20. use eyelib::nagios;
  21. use Data::Dumper;
  22. use Time::Local;
  23. use Date::Parse;
  24. use Getopt::Long;
  25. use Proc::Daemon;
  26. use Cwd;
  27. use File::Tail;
  28. my $pf = '/run/hmonitor/hmonitor.pid';
  29. my $daemon = Proc::Daemon->new(
  30. pid_file => $pf,
  31. work_dir => $HOME_DIR
  32. );
  33. # are you running? Returns 0 if not.
  34. my $pid = $daemon->Status($pf);
  35. my $daemonize = 1;
  36. GetOptions(
  37. 'daemon!' => \$daemonize,
  38. "help" => \&usage,
  39. "reload" => \&reload,
  40. "restart" => \&restart,
  41. "start" => \&run,
  42. "status" => \&status,
  43. "stop" => \&stop
  44. ) or &usage;
  45. exit(0);
  46. sub stop {
  47. if ($pid) {
  48. print "Stopping pid $pid...";
  49. if ($daemon->Kill_Daemon($pf)) {
  50. print "Successfully stopped.\n";
  51. } else {
  52. print "Could not find $pid. Was it running?\n";
  53. }
  54. } else {
  55. print "Not running, nothing to stop.\n";
  56. }
  57. }
  58. sub status {
  59. if ($pid) {
  60. print "Running with pid $pid.\n";
  61. } else {
  62. print "Not running.\n";
  63. }
  64. }
  65. sub run {
  66. if (!$pid) {
  67. print "Starting...";
  68. if ($daemonize) {
  69. # when Init happens, everything under it runs in the child process.
  70. # this is important when dealing with file handles, due to the fact
  71. # Proc::Daemon shuts down all open file handles when Init happens.
  72. # Keep this in mind when laying out your program, particularly if
  73. # you use filehandles.
  74. $daemon->Init;
  75. }
  76. setpriority(0,0,19);
  77. while(1) {
  78. eval {
  79. my $hdb = init_db();
  80. #parse log
  81. my $nagios_log=File::Tail->new(name=>$config_ref{nagios_event_socket},maxinterval=>5,interval=>1,ignore_nonexistant=>1) || die "$config_ref{nagios_event_socket} not found!";
  82. #truncate current log file
  83. truncate $config_ref{nagios_event_socket}, 0;
  84. while (my $logline=$nagios_log->read) {
  85. next unless defined $logline;
  86. chomp($logline);
  87. log_debug("GET:".$logline);
  88. my ($date,$hoststate,$hoststatetype,$hostname,$hostip,$hostid,$hosttype,$svc_control)= split (/\|/, $logline);
  89. next if (!$hostip);
  90. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  91. if (!$svc_control) { $svc_control=0; }
  92. if ($hoststate=~/UNREACHABLE/i) { $hoststate='DOWN'; }
  93. my $old_state = 'HARDDOWN';
  94. my $device;
  95. my $auth;
  96. my $login;
  97. my $nagios_handler;
  98. if (!$hostid or $hostid !~ /^[0-9]/) {
  99. $auth = get_record_sql($hdb,"SELECT * FROM user_auth WHERE deleted=0 AND ip=?",$hostip);
  100. next if (!$auth);
  101. $hostid = $auth->{id};
  102. $login = get_record_sql($hdb,'SELECT * FROM user_list WHERE id=?',$auth->{user_id});
  103. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE user_id=?',$auth->{user_id});
  104. if ($auth->{nagios_status}) { $old_state = $auth->{nagios_status}; }
  105. db_log_verbose($hdb,"Manual host: $hostname [$hostip] => $hoststate, old: $old_state");
  106. } else {
  107. if ($hosttype=~/device/i) {
  108. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE id=?',$hostid);
  109. $login = get_record_sql($hdb,'SELECT * FROM user_list WHERE id=?',$device->{user_id});
  110. $auth = get_record_sql($hdb,'SELECT * FROM user_auth WHERE user_id=? AND deleted=0 AND ip=?',$device->{user_id},$hostip);
  111. if ($device->{nagios_status}) { $old_state = $device->{nagios_status}; }
  112. } else {
  113. $auth = get_record_sql($hdb,'SELECT * FROM user_auth WHERE id=?',$hostid);
  114. $login = get_record_sql($hdb,'SELECT * FROM user_list WHERE id=?',$auth->{user_id});
  115. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE user_id=?',$auth->{user_id});
  116. if ($auth->{nagios_status}) { $old_state = $auth->{nagios_status}; }
  117. }
  118. }
  119. if ($auth and $auth->{nagios_handler}) { $nagios_handler=$auth->{nagios_handler}; }
  120. db_log_debug($hdb,"Get old for $hostname [$hostip] id: $hostid type: $hosttype => state: $old_state");
  121. if ($hoststate eq "DOWN") { $hoststate=$hoststatetype.$hoststate; }
  122. db_log_debug($hdb,"Now for $hostname [$hostip] id: $hostid type: $hosttype => state: $hoststate");
  123. if ($hoststate ne $old_state) {
  124. #disable child
  125. my $full_action = ($svc_control eq 2);
  126. #Change device state
  127. db_log_verbose($hdb,"Host changed! $hostname [$hostip] => $hoststate, old: $old_state");
  128. my $ip_aton=StrToIp($hostip);
  129. if ($device->{id}) { do_sql($hdb,"UPDATE devices SET nagios_status=? WHERE id=?", $hoststate, $device->{id}); }
  130. if ($auth->{id}) { do_sql($hdb,"UPDATE user_auth SET nagios_status=? WHERE id=?",$hoststate, $auth->{id}); }
  131. if ($hoststate=~/UP/i) {
  132. nagios_host_svc_enable($hostname,1);
  133. db_log_debug($hdb,"Enable notifications for host $hostname [$hostip] id: $hostid services");
  134. }
  135. if ($hoststate=~/SOFTDOWN/i) {
  136. if ($svc_control) {
  137. nagios_host_svc_disable($hostname,$full_action);
  138. db_log_debug($hdb,"Disable notifications for host $hostname [$hostip] id: $hostid services");
  139. }
  140. }
  141. if ($hoststate=~/HARDDOWN/i) {
  142. if ($svc_control) {
  143. nagios_host_svc_disable($hostname,$full_action);
  144. db_log_debug($hdb,"Disable notifications for host $hostname [$hostip] id: $hostid services");
  145. }
  146. if ($nagios_handler) {
  147. db_log_info($hdb,"Event handler $nagios_handler for $hostname [$hostip] => $hoststate found!");
  148. if ($nagios_handler=~/restart-port/i) {
  149. my $run_cmd = $HOME_DIR."/restart_port_snmp.pl $hostip & ";
  150. db_log_info($hdb,"Nagios eventhandler restart-port started for $hostname [".$hostip."]");
  151. db_log_info($hdb,"Run handler: $run_cmd");
  152. system($run_cmd);
  153. }
  154. } else {
  155. db_log_debug($hdb,"Event handler for $hostname [$hostip] => $hoststate not found.");
  156. }
  157. }
  158. }
  159. }
  160. };
  161. if ($@) { log_error("Exception found: $@"); sleep(60); }
  162. }
  163. } else {
  164. log_error("Already Running with pid $pid");
  165. }
  166. }
  167. sub usage {
  168. print "usage: hmonitor.pl (start|stop|status|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. }