首页 文章

让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时不报警并保持状态“运行”,但在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个周期后重新启动?

1 回答

  • 14

    EDIT (IMPORTANT) :请参阅下面的评论,了解Monit的更新版本(根据2019年2月),此行为已得到改进 .


    这一行:

    if does not exist for 3 cycles then restart
    

    意味着以下内容:

    在检查3次服务不存在之前,请不要执行任何操作,然后重新启动它 . 在monit的文档中将此行为描述为Failure Tolerance:

    FAILURE TOLERANCE默认情况下,如果操作匹配且服务设置为错误状态,则执行操作 . 但是,在触发错误事件并且服务状态更改为失败之前,您可以要求测试多次失败 . 这有助于避免发生可能发生的虚假错误警报,尤其是在网络测试时 . 语法:FOR CYCLES ...或:[TIMES WITHIN] CYCLES ...

    因此,Monit不会改变服务的状态,直到它在接下来的X个周期内失败 . 为了确认此声明,只需删除此服务的容错,并仅使用:

    if does not exist then alert
    

    手动停止服务并确认该命令

    monit status
    

    一旦停止,现在显示状态“不存在” .

    那么,回到你的问题:

    • 是的,可以在1个周期内发送警报(每个电子邮件) . 为此,您需要为该服务定义选项"if does not exist then alert"并正确设置电子邮件警报 . 假设您想使用外部eMail-Server,则需要至少定义两行(使用gmail配置示例):

    SMTP SERVER CONFIGURATION

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

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

    EMAIL RECIPIENT

    set alert xxxxxxx@xxxxx.xxx
    

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

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

    参考文献:

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

    希望能帮助到你!

    问候

相关问题