首页 > 解决方案 > Monit:在警报中访问测试阈值

问题描述

我已经设置Monit来监控进程的内存使用情况,exec当阈值被破坏时,检查将是一个 Python 脚本。Python 脚本将发出 Slack 通知。

要求是,我需要在超出阈值时打印内存使用情况。

监控配置文件如下:

 check process testprocess with pidfile /mnt/codebase/userserver_4444.pid
    start program = "/bin/"
    as uid ubuntu and gid admin
    if memory usage > 50% for 5 cycles then exec "/usr/bin/python /opt/scripts/slacker.py <channel_name> <User> <Level>  <Message>"

我收到以下松弛消息:

Alert : Host-test
 172.39.11.115 USER-MEM High-memory-for-1-cycles

但我希望信息是:

Alert : tpg-prod-user-16-115
172.31.16.115 USER-MEM 55% High-memory-for-1-cycles

55%是超出阈值时消耗的内存。我需要知道,是否有办法访问使用值,并将其作为参数传递给 Python 脚本以打印消息。

标签: monit

解决方案


您可以使用两个选项:

  1. Monit为包含$MONIT_DESCRIPTION类似mem usage of 1.0% matches resource limit [mem usage>0.5%].

  2. Monit 提供了一个 ENV var 用于绝对内存使用$MONIT_PROCESS_MEMORY其中包含一个整数,如2844.

有关可用 ENV 变量的完整列表,请参阅Monit 文档

这些 ENV 变量仅对子线程可见,因此您必须包装您的调用:

# will create a file "/tmp/$MONIT_PROCESS_MEMORY"
[...] then exec "/usr/bin/touch /tmp/$MONIT_PROCESS_MEMORY"

# will create a file "/tmp/2844"
[...] then exec "/bin/bash -c '/usr/bin/touch /tmp/$MONIT_PROCESS_MEMORY'"

# will create a file "/tmp/py_2844"
[...] then exec "/usr/bin/python /tmp/monit1.py"

/tmp/monit1.py在哪里

import os

with open("/tmp/py_%s" % (os.environ['MONIT_PROCESS_MEMORY']), 'w') as f:
    f.close()

推荐阅读