【问题标题】:alarm on existence of file in Monit在Monit中存在文件的警报
【发布时间】:2013-04-12 03:23:52
【问题描述】:

我使用 monit 已经有一段时间了,但我想在文件存在时发出警报。这是与the main documentation 相反的用例。

文档是这样说的:

IF [DOES] NOT EXIST [[<X>] <Y> CYCLES] THEN action [ELSE IF SUCCEEDED [[<X>] <Y> CYCLES] THEN action]
action is a choice of "ALERT", "RESTART", "START", "STOP", "EXEC" or "UNMONITOR".

这给了我“如果文件丢失就吓坏了”的秘诀。但我想“如果文件在那里就吓坏了”。行动的选择意味着没有“什么都不做”的行动。我可以选择无操作,但对于“什么都不做”的标准情况来说,这真的很愚蠢。

我猜到了一些基本情况:

IF EXISTS THEN alarm
IF EXIST THEN ALARM

那么,IF IT DOES EXIST 有标准的方法吗?

【问题讨论】:

    标签: monit


    【解决方案1】:

    我最近一直在寻找与您相同的解决方案,不幸的是,我无法在 monit 中找到执行此操作的方法。

    我的情况与您的情况略有不同,所以如果文件不存在,我最终会感到震惊,如果存在,我会执行一个 shell 脚本。和你一样,我不想仅仅因为文件不存在而生成一个 shell,在 /var/log/messages 中显示“文件不存在”对我来说没什么大不了的。

    我知道您说过您可以选择无操作,因此您可能不需要以下内容,但我正在为可能遇到相同问题但不知道如何操作的人添加它。

    check file testfile with path /path/to/file
        if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alarm
    

    请注意,您必须执行 /bin/bash 才能将 echo 的输出写入 /dev/null,否则 monit 将直接回显“dne > /dev/null”

    编辑:由于灾难避免引起了我的注意,新版本的 Monit 使用警报而不是警报,所以检查看起来像这样:

    check file testfile with path /path/to/file
        if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alert
    

    【讨论】:

    • 看起来 sn-p 被复制并忘记更改。它应该是 alert 在第二个代码 sn-p 的末尾 :) 我无法更改它,因为它小于 6 个字符。
    • 现在应该修复
    【解决方案2】:

    从 monit 5.21.0 开始,直接支持更改存在:

    check file testfile with path /path/to/file
        if exist then alert
    

    查看更新日志https://mmonit.com/monit/changes/#5.21.0

    【讨论】:

      【解决方案3】:

      请检查:

      check program not_exist_file_root_test with path "/bin/ls /root/test"
      
      if status = 0 then alert
      

      check program not_exist_file_root_test with path /bin/sh -c "test -f /root/test"
      
      if status = 0 then alert
      

      我的 2 美分

      【讨论】:

        【解决方案4】:

        renab,至少在我的版本 (5.2.5) 中,您的检查应该以“然后警报”而不是“然后警报”结尾。

        testfile with path /path/to/file
          if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alert
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多