monit - 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为包含
$MONIT_DESCRIPTION
类似mem usage of 1.0% matches resource limit [mem usage>0.5%]
.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()
推荐阅读
- twilio - 如何获得 Twilio 函数以继续通话并返回收集信息
- docker - nginx处理器后面的docker上的Nifi配置未打开
- caching - 咖啡因缓存:如何在不更改过期时间的情况下更新缓存值
- css - Angular - 具有多项选择的活动和非活动 CSS
- c# - 如何通过更改源处理来自 Cosmos DB 中不同容器的更改
- json - 如何使用颤振将数据发送到互联网 HTTP Post 请求(已修复)
- bash - head 命令删除部分行
- python - 将 Pandas DataFrame 传递给函数的最佳实践
- monaco-editor - 现有语言的新标记
- indexing - PostgreSQL:多列索引(jsonb,整数)部分与@>和=条件一起使用