我有一个网页,有时会出现状态为 200 的空白页,我想在这种情况发生时自动重启服务。为此,我想使用 Monit 和 nagios 插件,允许在页面中搜索文本,而不是仅检查状态,但我的问题是 monit 需要很长时间才能检测到页面何时关闭,并且即使再次正常运行,它也会多次重启服务。
我的 Monit 配置文件:
check program nginx with path "/etc/monit/files/check_nginx.sh"
every 1 cycle
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if status != 0 then restart
由于 Monit 不允许传递参数,因此我在 bash 中使用了这个简单的脚本:
#!/bin/bash
/etc/monit/files/check_http -H 127.0.0.1 -w 5,0 -c 10,0 -p 88
exit $?
当 nagios 运行时,Monit 可以毫无问题地检测到它,但是当我停止守护进程时,它需要大约 2 个周期才能检测到它已关闭(这是可以接受的,因为是 2 分钟)。检测到问题后,Monit 可以毫无问题地运行重启命令:
CEST Sep 26 18:31:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:31:47] info : 'nginx' trying to restart
[CEST Sep 26 18:31:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:31:47] info : 'nginx' start: /etc/init.d/nginx
这很好,但问题是无法检测何时再次启动,并且它会在每个周期不断重新启动服务,直到我禁用 Monit 服务:
[CEST Sep 26 18:31:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:31:47] info : 'nginx' trying to restart
[CEST Sep 26 18:31:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:31:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:32:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:32:47] info : 'nginx' trying to restart
[CEST Sep 26 18:32:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:32:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:33:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:33:47] info : 'nginx' trying to restart
[CEST Sep 26 18:33:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:33:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:34:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:34:47] info : 'nginx' trying to restart
[CEST Sep 26 18:34:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:34:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:35:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:35:47] info : 'nginx' trying to restart
[CEST Sep 26 18:35:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:35:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:36:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:36:47] info : 'nginx' trying to restart
[CEST Sep 26 18:36:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:36:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:37:47] error : 'nginx' connect to address localhost and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:37:47] info : 'nginx' trying to restart
[CEST Sep 26 18:37:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:37:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:38:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:38:47] info : 'nginx' trying to restart
[CEST Sep 26 18:38:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:38:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:39:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:39:47] info : 'nginx' trying to restart
[CEST Sep 26 18:39:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:39:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:40:47] info : 'nginx' status succeeded
[CEST Sep 26 18:44:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:44:47] info : 'nginx' trying to restart
[CEST Sep 26 18:44:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:44:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:45:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:45:47] info : 'nginx' trying to restart
[CEST Sep 26 18:45:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:45:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:46:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:46:47] info : 'nginx' trying to restart
[CEST Sep 26 18:46:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:46:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:47:47] error : 'nginx' connect to address 127.0.0.1 and port 88: Connection refused
HTTP CRITICAL - Unable to open TCP socket
[CEST Sep 26 18:47:47] info : 'nginx' trying to restart
[CEST Sep 26 18:47:47] info : 'nginx' stop: /etc/init.d/nginx
[CEST Sep 26 18:47:47] info : 'nginx' start: /etc/init.d/nginx
[CEST Sep 26 18:48:26] info : Shutting down monit HTTP server
[CEST Sep 26 18:48:26] info : monit HTTP server stopped
[CEST Sep 26 18:48:26] info : monit daemon with pid [4187] killed
我已经测试了 bash 脚本以查看是否检测服务何时再次启动并正常运行,因此看起来 Monit 没有更新状态或没有尝试再次运行该命令。
有人知道会发生什么?因为我不知道该怎么办......
谢谢!!
答案1
已修复。这似乎是 Ubuntu 版本的一个错误,因为我已经下载了最新版本 (5.19.0),现在即使有参数也可以正常工作。
问候!!