hmonitor.pl 6.4 KB

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