监控进程死亡时,Monit EXEC无法正常工作

时间:2015-12-10 08:59:11

标签: monit

在FreeBSD 10.2上使用Monit 5.15:

set daemon  5
set logfile syslog
set pidfile /var/run/monit.pid
set idfile /var/.monit.id
set statefile /var/.monit.state
set alert x@y.z
set mailserver localhost
set httpd port 2812 and
     use address 192.168.40.72
     allow 192.168.20.0/24
     allow admin:monit

check process haproxy with pidfile /var/run/haproxy.pid
     if failed host 192.168.40.72 port 9090 type tcp
       then exec "/bin/sh -c '/bin/echo `/bin/date` >> /tmp/monit.test'"

当我使用-vI运行monit并且我杀死haproxy时,我有以下输出:

Adding net allow '192.168.20.0/24'
Adding credentials for user 'admin'
Runtime constants:
 Control file       = /usr/local/etc/monitrc
 Log file           = syslog
 Pid file           = /var/run/monit.pid
 Id file            = /var/.monit.id
 State file         = /var/.monit.state
 Debug              = True
 Log                = True
 Use syslog         = True
 Is Daemon          = True
 Use process engine = True
 Poll time          = 5 seconds with start delay 0 seconds
 Expect buffer      = 256 bytes
 Mail server(s)     = localhost:25 with timeout 30 seconds
 Mail from          = (not defined)
 Mail subject       = (not defined)
 Mail message       = (not defined)
 Start monit httpd  = True
 httpd bind address = 192.168.40.72
 httpd portnumber   = 2812
 httpd ssl          = Disabled
 httpd signature    = Enabled
 httpd auth. style  = Basic Authentication and Host/Net allow list
 Alert mail to      = root@localhost
   Alert on         = All events

The service list contains the following entries:

Process Name          = haproxy
 Pid file             = /var/run/haproxy.pid
 Monitoring mode      = active
 Existence            = if does not exist then restart
 Port                 = if failed [192.168.40.72]:9090 type TCP/IP protocol DEFAULT with timeout 5 seconds then exec '/bin/sh -c /bin/echo `/bin/date` >> /tmp/monit.test'

System Name           = appsrv01
 Monitoring mode      = active

-------------------------------------------------------------------------------
pidfile '/var/run/monit.pid' does not exist
Starting Monit 5.15 daemon with http interface at [192.168.40.72]:2812
Starting Monit HTTP server at [192.168.40.72]:2812
Monit HTTP server started
'appsrv01' Monit 5.15 started
Sending Monit instance changed notification to root@localhost
'haproxy' process is running with pid 42999
'haproxy' zombie check succeeded
'haproxy' succeeded testing protocol [DEFAULT] at [192.168.40.72]:9090 [TCP/IP]
'haproxy' connection succeeded to [192.168.40.72]:9090 [TCP/IP]
'haproxy' process is running with pid 42999
'haproxy' zombie check succeeded
'haproxy' succeeded testing protocol [DEFAULT] at [192.168.40.72]:9090 [TCP/IP]
'haproxy' connection succeeded to [192.168.40.72]:9090 [TCP/IP]
'haproxy' process is running with pid 42999
'haproxy' zombie check succeeded
'haproxy' succeeded testing protocol [DEFAULT] at [192.168.40.72]:9090 [TCP/IP]
'haproxy' connection succeeded to [192.168.40.72]:9090 [TCP/IP]
'haproxy' process test failed [pid=42999] -- No such process
'haproxy' process is not running
Sending Does not exist notification to root@localhost
'haproxy' trying to restart
'haproxy' stop skipped -- method not defined
'haproxy' start method not defined
'haproxy' monitoring enabled
'haproxy' process test failed [pid=42999] -- No such process
'haproxy' process is not running
'haproxy' trying to restart
'haproxy' stop skipped -- method not defined
'haproxy' start method not defined
'haproxy' monitoring enabled
^CShutting down Monit HTTP server
Monit HTTP server stopped
Monit daemon with pid [48685] stopped
'appsrv01' Monit 5.15 stopped
Sending Monit instance changed notification to root@localhost

EXEC Line永远不会执行,我在/tmp/monit.test中看不到任何新行

如果我将已检查的端口从9090更改为某个无效端口,请说9190并启动monit(haproxy正在运行!),我看到:

Starting Monit 5.15 daemon with http interface at [192.168.40.72]:2812
Starting Monit HTTP server at [192.168.40.72]:2812
Monit HTTP server started
'appsrv01' Monit 5.15 started
Sending Monit instance changed notification to root@localhost
'haproxy' process is running with pid 50703
'haproxy' zombie check succeeded
Socket test failed for [192.168.40.72]:9190 -- Connection refused
'haproxy' failed protocol test [DEFAULT] at [192.168.40.72]:9190 [TCP/IP] -- Connection refused
Sending Connection failed notification to root@localhost
'haproxy' exec: /bin/sh
'haproxy' process is running with pid 50703
'haproxy' zombie check succeeded
Socket test failed for [192.168.40.72]:9190 -- Connection refused
'haproxy' failed protocol test [DEFAULT] at [192.168.40.72]:9190 [TCP/IP] -- Connection refused
'haproxy' exec: /bin/sh

为什么EXEC Line在这里工作但不是在我杀死-9 haproxy的时候? 我试图做的是在haproxy失败的情况下让monit运行exec。然后,exec行将包含一个命令,用于将CARP IP切换到另一个主机。 haproxy本身是使用zabbix监控的,因此NOC可以稍后调查失败的原因。

1 个答案:

答案 0 :(得分:1)

当你kill -9 haproxy你正在杀死守护进程时。因此,当monit执行此“检查进程”块时,它会检测到进程不存在并重新启动进程。它不会对该端口执行检查,因为它看不到该进程。

当您为其提供无效端口时,它会起作用,因为该进程仍处于活动状态。当它执行端口检查时,它将失败并运行脚本。

您应该在此检查块中添加一行

check process haproxy with pidfile /var/run/haproxy.pid
     if failed host 192.168.40.72 port 9090 type tcp 
         then exec "/bin/sh -c '/bin/echo `/bin/date` >> /tmp/monit.test'"
     if restarted then exec "/bin/sh -c '/bin/echo `/bin/date` >>/tmp/monit.test'"

这应该在重启和失败的主机上运行shell命令。