让 Monit 先发出警报再重新启动。

19

我想在monit中处理一种链式行动。

  • 检查进程并立即发出警报。
  • 在若干周期后重新启动进程。

我的尝试(到目前为止):

check process myprocess with pidfile /run/my.pid
  start program = "/path/to/binary start" with timeout 60 seconds
  stop program = "/path/to/binary stop" with timeout 60 seconds
  if not exist for 3 cycles then restart
  if not exist then alert
  if 3 restarts within 3 cycles then timeout

即使PID失败也不会发出警报并保持“running”状态,但在3个周期后将重新启动。

check process myprocess with pidfile /run/my.pid
  start program = "/path/to/binary start" with timeout 60 seconds
  stop program = "/path/to/binary stop" with timeout 60 seconds
  if not exist for 3 cycles then restart
  if children < 1 for 1 cycles then alert
  if 3 restarts within 3 cycles then timeout

没有1岁以下儿童的警报,但在5秒后重新启动。 monit.log
[CEST Aug  1 15:09:30] error    : 'myprocess' process is not running

Monit 概述

Process 'myprocess'            Running

这里是monit -v的部分:

Existence      = if does not exist 3 times within 3 cycle(s) then restart else 
                 if succeeded 1 times within 1 cycle(s) then alert
Pid            = if changed 1 times within 1 cycle(s) then alert
Ppid           = if changed 1 times within 1 cycle(s) then alert
Children       = if less than 1 1 times within 1 cycle(s) then alert else if 
                 succeeded 1 times within 1 cycle(s) then alert
Timeout        = If restarted 3 times within 3 cycle(s) then unmonitor

问题是:是否可以在1个周期内发送警报并更改状态为“未运行”,然后在3个周期后重新启动?


当您说“不会警报”时,您是指已为Monit设置了全局/本地电子邮件警报,但它未按照要求发送它们吗? - Eduardo López
Monit已正确设置。所有警报和电子邮件都正常。正如我所写的,Monit根本没有发出警报,并且在PID失败时保持“运行”状态,但在3个周期后重新启动。 - questioner
1个回答

17

编辑(重要):请查看下方评论,获取更新版本的 Monit(截至2019年2月),该版本已经改进了此行为。


以下是需要翻译的内容:

if does not exist for 3 cycles then restart

意思是:

在检查三次服务不存在之前,不要执行任何操作,然后重新启动它。 这种行为在monit文档中被描述为故障容忍性:

故障容忍性

默认情况下,如果操作与设置了错误状态的服务匹配,则将执行该操作。 但是,在触发错误事件并将服务状态更改为失败之前,您可以要求多次测试失败。这对于避免因网络测试而发生的虚假错误尤其有用。

语法:

FOR CYCLES ... 或者:

[TIMES WITHIN] CYCLES ...

因此,Monit不会更改服务的状态,直到它在接下来的X个周期内失败。为了确认此声明,请仅删除此服务的故障容忍性并仅使用:

if does not exist then alert

停止手动服务并确认命令

monit status

一旦停止,它会显示当前状态为“不存在”。

那么,回到你的问题:

  1. 是的,可以在1个周期内发送警报(通过电子邮件)。为此,您需要为该服务定义选项“如果不存在则警报”,并正确设置电子邮件警报。假设您想要使用外部电子邮件服务器,则需要定义至少两行(使用gmail的配置示例):

SMTP服务器配置

set mailserver smtp.gmail.com PORT 587 USERNAME "xxxxxxx@xxxxx.xxx" PASSWORD "xxxxx" using TLSV1 with timeout 30 seconds

请注意,在gmail中,您必须激活“不安全应用程序”的访问权限,以允许monit使用stmp服务。

电子邮件收件人

set alert xxxxxxx@xxxxx.xxx

两者都在文件/etc/monit/monitrc中。有关这两行的更多信息,请参阅官方文档。

  1. 根据文档,如果定义了容错(在X个周期后执行操作),则无法立即更新服务的状态。但是,您仍然可以定义要立即发送的警报并在所需的周期内重新启动服务。

参考:

Monit的文档:https://mmonit.com/monit/documentation/monit.html

希望对您有所帮助!

谢谢!


2
有人能解释一下为什么有人对这个答案点踩吗?o_O - Eduardo López
请查看第一个配置。我尝试了“如果不存在则警报”,但没有效果。 - questioner
我刚刚进行了一些测试,使用了似乎是最新版本的monit(5.25.2),看起来他们摆脱了故障容错。就我所测试的而言,进程状态现在在第一个周期后立即更改,您可以定义“如果连续3个周期不存在,则重新启动”和“如果不存在,则警报”,并且它确实像预期的那样工作(从第一个周期开始警报,第三个周期重新启动)。我建议更新到最新版本,并查看您的配置是否可行。如果是的话,我会更新答案。 - Eduardo López
不,他们没有取消失败容错。现在它被称为“故障容忍”并且看起来和以前差不多,但无论如何行为现在是不同的,在第一个错误周期之后进程的状态确实会发生改变,而不是在指定周期结束时发生。所以它应该按照您的预期工作。再次,请尝试更新并分享 :-) - Eduardo López

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接