hmonitor.pl 6.3 KB

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