hmonitor.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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::mysql;
  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 = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  75. if ( !defined $hdb ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  76. #parse log
  77. 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!";
  78. #truncate current log file
  79. truncate $config_ref{nagios_event_socket}, 0;
  80. while (my $logline=$nagios_log->read) {
  81. next unless defined $logline;
  82. chomp($logline);
  83. log_debug("GET:".$logline);
  84. my ($date,$hoststate,$hoststatetype,$hostname,$hostip,$hostid,$hosttype,$svc_control)= split (/\|/, $logline);
  85. next if (!$hostip);
  86. if (time()-$last_refresh_config>=60) { init_option($hdb); }
  87. if (!$svc_control) { $svc_control=0; }
  88. if ($hoststate=~/UNREACHABLE/i) { $hoststate='DOWN'; }
  89. my $old_state = 'HARDDOWN';
  90. my $device;
  91. my $auth;
  92. my $login;
  93. my $nagios_handler;
  94. if (!$hostid or $hostid !~ /^[0-9]/) {
  95. $auth = get_record_sql($hdb,'SELECT * FROM User_auth WHERE deleted=0 AND ip="'.$hostip.'"');
  96. next if (!$auth);
  97. $hostid = $auth->{id};
  98. $login = get_record_sql($hdb,'SELECT * FROM User_list WHERE id='.$auth->{user_id});
  99. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE user_id='.$auth->{user_id});
  100. if ($auth->{nagios_status}) { $old_state = $auth->{nagios_status}; }
  101. db_log_verbose($hdb,"Manual host: $hostname [$hostip] => $hoststate, old: $old_state");
  102. } else {
  103. if ($hosttype=~/device/i) {
  104. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE id='.$hostid);
  105. $login = get_record_sql($hdb,'SELECT * FROM User_list WHERE id='.$device->{user_id});
  106. $auth = get_record_sql($hdb,'SELECT * FROM User_auth WHERE user_id='.$device->{user_id}.' AND deleted=0 AND ip="'.$hostip.'"');
  107. if ($device->{nagios_status}) { $old_state = $device->{nagios_status}; }
  108. } else {
  109. $auth = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$hostid);
  110. $login = get_record_sql($hdb,'SELECT * FROM User_list WHERE id='.$auth->{user_id});
  111. $device = get_record_sql($hdb,'SELECT * FROM devices WHERE user_id='.$auth->{user_id});
  112. if ($auth->{nagios_status}) { $old_state = $auth->{nagios_status}; }
  113. }
  114. }
  115. if ($auth and $auth->{nagios_handler}) { $nagios_handler=$auth->{nagios_handler}; }
  116. db_log_debug($hdb,"Get old for $hostname [$hostip] id: $hostid type: $hosttype => state: $old_state");
  117. if ($hoststate eq "DOWN") { $hoststate=$hoststatetype.$hoststate; }
  118. db_log_debug($hdb,"Now for $hostname [$hostip] id: $hostid type: $hosttype => state: $hoststate");
  119. if ($hoststate ne $old_state) {
  120. #disable child
  121. my $full_action = ($svc_control eq 2);
  122. #Change device state
  123. db_log_verbose($hdb,"Host changed! $hostname [$hostip] => $hoststate, old: $old_state");
  124. my $ip_aton=StrToIp($hostip);
  125. if ($device->{id}) { do_sql($hdb,'UPDATE devices SET nagios_status="'.$hoststate.'" WHERE id='.$device->{id}); }
  126. if ($auth->{id}) { do_sql($hdb,'UPDATE User_auth SET nagios_status="'.$hoststate.'" WHERE id='.$auth->{id}); }
  127. if ($hoststate=~/UP/i) {
  128. nagios_host_svc_enable($hostname,1);
  129. db_log_debug($hdb,"Enable notifications for host $hostname [$hostip] id: $hostid services");
  130. }
  131. if ($hoststate=~/SOFTDOWN/i) {
  132. if ($svc_control) {
  133. nagios_host_svc_disable($hostname,$full_action);
  134. db_log_debug($hdb,"Disable notifications for host $hostname [$hostip] id: $hostid services");
  135. }
  136. }
  137. if ($hoststate=~/HARDDOWN/i) {
  138. if ($svc_control) {
  139. nagios_host_svc_disable($hostname,$full_action);
  140. db_log_debug($hdb,"Disable notifications for host $hostname [$hostip] id: $hostid services");
  141. }
  142. if ($nagios_handler) {
  143. db_log_info($hdb,"Event handler $nagios_handler for $hostname [$hostip] => $hoststate found!");
  144. if ($nagios_handler=~/restart-port/i) {
  145. my $run_cmd = $HOME_DIR."/restart_port_snmp.pl $hostip & ";
  146. db_log_info($hdb,"Nagios eventhandler restart-port started for $hostname [".$hostip."]");
  147. db_log_info($hdb,"Run handler: $run_cmd");
  148. system($run_cmd);
  149. }
  150. } else {
  151. db_log_debug($hdb,"Event handler for $hostname [$hostip] => $hoststate not found.");
  152. }
  153. }
  154. }
  155. }
  156. };
  157. if ($@) { log_error("Exception found: $@"); sleep(60); }
  158. }
  159. } else {
  160. log_error("Already Running with pid $pid");
  161. }
  162. }
  163. sub usage {
  164. print "usage: hmonitor.pl (start|stop|status|restart)\n";
  165. exit(0);
  166. }
  167. sub reload {
  168. print "reload process not implemented.\n";
  169. }
  170. sub restart {
  171. stop;
  172. run;
  173. }