hmonitor.pl 6.4 KB

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