当某些事情发生时,这两个进程(通常是他们的线程)通常存在并需要重新启动.我对当前问题的原因并不是很感兴趣,但是想要重启这两个进程.为此,我创建了两个bash脚本/etc/monit/webserver.start.sh和/etc/monit/webserver.stop.sh.
这是我的monit配置文件(在conf.d中):
check process webserver with pidfile /var/run/Nginx.pid start program = "/etc/monit/webserver.start.sh" stop program = "/etc/monit/webserver.stop.sh" if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds) then alert if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds) for 2 cycles then restart if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds) for 4 cycles then exec "/sbin/reboot"
这并非完全错误,但仍存在一些问题:
>实际上,我不想在这里监控Nginx进程,而是监视端口/ URL.我可以使用任何其他检查而不是检查过程吗?
>要在1次失败后执行不同的操作,2次失败,4次失败,我需要三次失败条件,导致三次服务器请求.有没有办法在每个周期运行一个请求并在不同次数的失败后执行不同的活动?
我试图从官方的monit参考中找到答案,但很明显,我不明白该来源所描述的可能性.因此,我会非常感激地提出一些建议.
更新
在monit手册页上花了一些时间(在我看来,它的结构比在线手册好得多),我发现了这个优化:
CHECK HOST webserver WITH ADDRESS 127.0.0.1 START PROGRAM = "/etc/monit/webserver.start.sh" STOP PROGRAM = "/etc/monit/webserver.stop.sh" IF NOT EXIST THEN ALERT IF Failed (url https://www.mydomain.tld/example/ and content == 'test content' and timeout 20 seconds) FOR 2 CYCLES THEN RESTART IF 2 RESTARTS WITHIN 5 CYCLES THEN EXEC "/sbin/reboot"
此修改不包括第一个URL失败时的警报(解决方法是使用虚拟启动/停止命令,此处),但可以在2个失败并且重启异常4失败后重新启动 – 只有一个服务器请求.
它仍然不完美.如果有人知道如何做得更好,建议仍然赞赏:)谢谢!
更新
经过一些测试后,我不建议使用monit的超时功能(IF 2 REsTARTS WITHIN …)进行二阶动作.似乎在证书情况下重新启动后重新运行超时操作.在我的情况下,这导致多次重启:
[CET Dec 28 05:59:50] error : skipping queued event /var/monit/id - unknown data format [CET Dec 28 05:59:50] error : skipping queued event /var/monit/state - unknown data format [CET Dec 30 03:10:52] error : 'webserver' Failed protocol test [HTTP] at INET[www.myserver.com/example/] via TCPSSL -- HTTP: Error receiving data -- Resource temporarily unavailable [CET Jan 1 03:08:10] error : 'webserver' Failed protocol test [HTTP] at INET[www.myserver.com/example/] via TCPSSL -- HTTP: Error receiving data -- Resource temporarily unavailable [CET Jan 1 03:09:30] error : 'webserver' Failed protocol test [HTTP] at INET[www.myserver.com/example/] via TCPSSL -- HTTP: Error receiving data -- Resource temporarily unavailable [CET Jan 1 03:09:31] info : 'webserver' trying to restart [CET Jan 1 03:09:31] info : 'webserver' stop: /etc/monit/webserver.stop.sh [CET Jan 1 03:09:31] info : 'webserver' start: /etc/monit/webserver.start.sh [CET Jan 1 03:10:31] error : 'webserver' Failed,cannot open a connection to INET[www.myserver.com/example/] via TCPSSL [CET Jan 1 03:10:31] info : 'webserver' trying to restart [CET Jan 1 03:10:31] info : 'webserver' stop: /etc/monit/webserver.stop.sh [CET Jan 1 03:10:31] info : 'webserver' start: /etc/monit/webserver.start.sh [CET Jan 1 03:10:31] error : 'PHP-fpm' process is not running [CET Jan 1 03:10:31] info : 'PHP-fpm' trying to restart [CET Jan 1 03:10:31] info : 'PHP-fpm' start: /usr/sbin/service [CET Jan 1 03:10:31] error : 'Nginx' process is not running [CET Jan 1 03:10:31] info : 'Nginx' trying to restart [CET Jan 1 03:10:31] info : 'Nginx' start: /usr/sbin/service [CET Jan 1 03:11:32] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec [CET Jan 1 03:11:32] info : 'webserver' exec: /sbin/reboot [CET Jan 1 03:12:24] info : Starting monit daemon with http interface at [0.0.0.0:2812] [CET Jan 1 03:12:24] info : Monit start delay set -- pause for 240s [CET Jan 1 03:16:24] info : Starting monit HTTP server at [0.0.0.0:2812] [CET Jan 1 03:16:24] info : monit HTTP server started [CET Jan 1 03:16:24] info : 'Memory' Monit started [CET Jan 1 03:16:24] error : skipping queued event /var/monit/id - unknown data format [CET Jan 1 03:16:24] error : skipping queued event /var/monit/state - unknown data format [CET Jan 1 03:16:24] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec [CET Jan 1 03:16:24] info : 'webserver' exec: /sbin/reboot [CET Jan 1 03:17:04] info : Starting monit daemon with http interface at [0.0.0.0:2812] [CET Jan 1 03:17:04] info : Monit start delay set -- pause for 240s [CET Jan 1 03:21:04] info : Starting monit HTTP server at [0.0.0.0:2812] [CET Jan 1 03:21:04] info : monit HTTP server started [CET Jan 1 03:21:04] info : 'Memory' Monit started [CET Jan 1 03:21:04] error : skipping queued event /var/monit/id - unknown data format [CET Jan 1 03:21:04] error : skipping queued event /var/monit/state - unknown data format [CET Jan 1 03:21:04] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec [CET Jan 1 03:21:04] info : 'webserver' exec: /sbin/reboot [CET Jan 1 03:21:44] info : Starting monit daemon with http interface at [0.0.0.0:2812] [CET Jan 1 03:21:44] info : Monit start delay set -- pause for 240s [CET Jan 1 03:25:44] info : Starting monit HTTP server at [0.0.0.0:2812] [CET Jan 1 03:25:44] info : monit HTTP server started [CET Jan 1 03:25:44] info : 'Memory' Monit started [CET Jan 1 03:25:44] error : skipping queued event /var/monit/id - unknown data format [CET Jan 1 03:25:44] error : skipping queued event /var/monit/state - unknown data format [CET Jan 1 03:25:44] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec [CET Jan 1 03:25:44] info : 'webserver' exec: /sbin/reboot
除非有人有好主意,否则我将切换回多个请求.最后,它们不是那么耗时……
BurninLeo
解决方法
你可以使用主机检查,这是monit网站的一个例子:
check host mmonit.com with address mmonit.com if Failed port 80 protocol http with http headers [Host: mmonit.com,Cache-Control: no-cache,Cookie: csrftoken=nj1bI3CnMCaiNv4beqo8ZaCfAQQvpgLH] and request /monit/ with content = "Monit [0-9.]+" then alert
要在1次失败后执行不同的操作,导致三次服务器请求.有没有办法在每个周期运行一个请求并在不同次数的失败后执行不同的活动?
EXEC可用于执行任意程序并发送警报.如果选择此操作,则必须声明要执行的程序,如果程序需要参数,则必须将程序及其参数括在带引号的字符串中.您可以选择指定执行程序在启动时应切换到的uid和gid.例如:
exec "/usr/local/tomcat/bin/startup.sh" as uid nobody and gid nobody