web-server – Monit:如何以最佳方式监控URL

前端之家收集整理的这篇文章主要介绍了web-server – Monit:如何以最佳方式监控URL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的网络服务器使用PHP5-fpm运行Nginx.如果发生一些麻烦,通常PHP5-fpm挂断,导致“坏网关”服务器错误.当然,我永远不会知道,如果Nginx可能会崩溃,有一天.

当某些事情发生时,这两个进程(通常是他们的线程)通常存在并需要重新启动.我对当前问题的原因并不是很感兴趣,但是想要重启这两个进程.为此,我创建了两个bash脚本/etc/monit/webserver.start.sh和/etc/monit/webserver.stop.sh.

这是我的monit配置文件(在conf.d中):

  1. check process webserver with pidfile /var/run/Nginx.pid
  2. start program = "/etc/monit/webserver.start.sh"
  3. stop program = "/etc/monit/webserver.stop.sh"
  4. if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds)
  5. then alert
  6. if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds)
  7. for 2 cycles
  8. then restart
  9. if Failed (url https://www.myserver.com/example/ and content == 'test string' and timeout 20 seconds)
  10. for 4 cycles
  11. then exec "/sbin/reboot"

这并非完全错误,但仍存在一些问题:

>实际上,我不想在这里监控Nginx进程,而是监视端口/ URL.我可以使用任何其他检查而不是检查过程吗?
>要在1次失败后执行不同的操作,2次失败,4次失败,我需要三次失败条件,导致三次服务器请求.有没有办法在每个周期运行一个请求并在不同次数的失败后执行不同的活动?

我试图从官方的monit参考中找到答案,但很明显,我不明白该来源所描述的可能性.因此,我会非常感激地提出一些建议.

更新

在monit手册页上花了一些时间(在我看来,它的结构比在线手册好得多),我发现了这个优化:

  1. CHECK HOST webserver WITH ADDRESS 127.0.0.1
  2. START PROGRAM = "/etc/monit/webserver.start.sh"
  3. STOP PROGRAM = "/etc/monit/webserver.stop.sh"
  4. IF NOT EXIST THEN ALERT
  5. IF Failed (url https://www.mydomain.tld/example/ and content == 'test content' and timeout 20 seconds)
  6. FOR 2 CYCLES
  7. THEN RESTART
  8. IF 2 RESTARTS WITHIN 5 CYCLES
  9. THEN EXEC "/sbin/reboot"

修改包括第一个URL失败时的警报(解决方法是使用虚拟启动/停止命令,此处),但可以在2个失败并且重启异常4失败后重新启动 – 只有一个服务器请求.

它仍然不完美.如果有人知道如何做得更好,建议仍然赞赏:)谢谢!

更新

经过一些测试后,我不建议使用monit的超时功能(IF 2 REsTARTS WITHIN …)进行二阶动作.似乎在证书情况下重新启动后重新运行超时操作.在我的情况下,这导致多次重启:

  1. [CET Dec 28 05:59:50] error : skipping queued event /var/monit/id - unknown data format
  2. [CET Dec 28 05:59:50] error : skipping queued event /var/monit/state - unknown data format
  3. [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
  4. [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
  5. [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
  6. [CET Jan 1 03:09:31] info : 'webserver' trying to restart
  7. [CET Jan 1 03:09:31] info : 'webserver' stop: /etc/monit/webserver.stop.sh
  8. [CET Jan 1 03:09:31] info : 'webserver' start: /etc/monit/webserver.start.sh
  9. [CET Jan 1 03:10:31] error : 'webserver' Failed,cannot open a connection to INET[www.myserver.com/example/] via TCPSSL
  10. [CET Jan 1 03:10:31] info : 'webserver' trying to restart
  11. [CET Jan 1 03:10:31] info : 'webserver' stop: /etc/monit/webserver.stop.sh
  12. [CET Jan 1 03:10:31] info : 'webserver' start: /etc/monit/webserver.start.sh
  13. [CET Jan 1 03:10:31] error : 'PHP-fpm' process is not running
  14. [CET Jan 1 03:10:31] info : 'PHP-fpm' trying to restart
  15. [CET Jan 1 03:10:31] info : 'PHP-fpm' start: /usr/sbin/service
  16. [CET Jan 1 03:10:31] error : 'Nginx' process is not running
  17. [CET Jan 1 03:10:31] info : 'Nginx' trying to restart
  18. [CET Jan 1 03:10:31] info : 'Nginx' start: /usr/sbin/service
  19. [CET Jan 1 03:11:32] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec
  20. [CET Jan 1 03:11:32] info : 'webserver' exec: /sbin/reboot
  21. [CET Jan 1 03:12:24] info : Starting monit daemon with http interface at [0.0.0.0:2812]
  22. [CET Jan 1 03:12:24] info : Monit start delay set -- pause for 240s
  23. [CET Jan 1 03:16:24] info : Starting monit HTTP server at [0.0.0.0:2812]
  24. [CET Jan 1 03:16:24] info : monit HTTP server started
  25. [CET Jan 1 03:16:24] info : 'Memory' Monit started
  26. [CET Jan 1 03:16:24] error : skipping queued event /var/monit/id - unknown data format
  27. [CET Jan 1 03:16:24] error : skipping queued event /var/monit/state - unknown data format
  28. [CET Jan 1 03:16:24] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec
  29. [CET Jan 1 03:16:24] info : 'webserver' exec: /sbin/reboot
  30. [CET Jan 1 03:17:04] info : Starting monit daemon with http interface at [0.0.0.0:2812]
  31. [CET Jan 1 03:17:04] info : Monit start delay set -- pause for 240s
  32. [CET Jan 1 03:21:04] info : Starting monit HTTP server at [0.0.0.0:2812]
  33. [CET Jan 1 03:21:04] info : monit HTTP server started
  34. [CET Jan 1 03:21:04] info : 'Memory' Monit started
  35. [CET Jan 1 03:21:04] error : skipping queued event /var/monit/id - unknown data format
  36. [CET Jan 1 03:21:04] error : skipping queued event /var/monit/state - unknown data format
  37. [CET Jan 1 03:21:04] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec
  38. [CET Jan 1 03:21:04] info : 'webserver' exec: /sbin/reboot
  39. [CET Jan 1 03:21:44] info : Starting monit daemon with http interface at [0.0.0.0:2812]
  40. [CET Jan 1 03:21:44] info : Monit start delay set -- pause for 240s
  41. [CET Jan 1 03:25:44] info : Starting monit HTTP server at [0.0.0.0:2812]
  42. [CET Jan 1 03:25:44] info : monit HTTP server started
  43. [CET Jan 1 03:25:44] info : 'Memory' Monit started
  44. [CET Jan 1 03:25:44] error : skipping queued event /var/monit/id - unknown data format
  45. [CET Jan 1 03:25:44] error : skipping queued event /var/monit/state - unknown data format
  46. [CET Jan 1 03:25:44] error : 'webserver' service restarted 2 times within 2 cycles(s) - exec
  47. [CET Jan 1 03:25:44] info : 'webserver' exec: /sbin/reboot

除非有人有好主意,否则我将切换回多个请求.最后,它们不是那么耗时……

BurninLeo

解决方法

我不想在这里监视Nginx进程,而是监视端口/ URL.我可以使用任何其他检查而不是检查过程吗?

你可以使用主机检查,这是monit网站的一个例子:

  1. check host mmonit.com with address mmonit.com
  2. if Failed
  3. port 80 protocol http
  4. with http headers [Host: mmonit.com,Cache-Control: no-cache,Cookie: csrftoken=nj1bI3CnMCaiNv4beqo8ZaCfAQQvpgLH]
  5. and request /monit/ with content = "Monit [0-9.]+"
  6. then alert

要在1次失败后执行不同的操作,导致三次服务器请求.有没有办法在每个周期运行一个请求并在不同次数的失败后执行不同的活动?

EXEC可用于执行任意程序并发送警报.如果选择此操作,则必须声明要执行的程序,如果程序需要参数,则必须将程序及其参数括在带引号的字符串中.您可以选择指定执行程序在启动时应切换到的uid和gid.例如:

  1. exec "/usr/local/tomcat/bin/startup.sh"
  2. as uid nobody and gid nobody

猜你在找的HTML相关文章