hmonitor.pl 6.4 KB

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