【问题标题】:How can I configure monit to kill a high CPU process after a few seconds?如何配置 monit 在几秒钟后杀死高 CPU 进程?
【发布时间】:2015-11-01 22:24:11
【问题描述】:

我想使用 monit 杀死一个使用超过 X% CPU 超过 N 秒的进程。

我正在使用stress 生成负载来尝试一个简单的示例。

我的 .monitrc:

check process stress
    matching "stress.*"
    if cpu usage > 95% for 2 cycles then stop

我开始监视(我检查了 monit -t .monitrc 的语法):

monit -c .monitrc -d 5

我发起压力:

stress --cpu 1 --timeout 60

压力在top 中显示为使用 100 %CPU。

我预计 monit 会在大约 10 秒内消除压力,但压力会成功完成。我做错了什么?

我还尝试了monit procmatch "stress.*",它出于某种原因显示了两个匹配项。也许这很相关?

List of processes matching pattern "stress.*":
stress --cpu 1 --timeout 60
stress --cpu 1 --timeout 60
Total matches: 2
WARNING: multiple processes matched the pattern. The check is FIRST-MATCH based, please refine the pattern

编辑:尝试了 e.lopez 的方法

我不得不从 .monitrc 中删除 start 语句,因为它导致了 monit 错误('stress' failed to start (exit status -1) -- Program /usr/bin/stress timed out 然后是僵尸进程)。

所以手动启动压力:

stress -c 1
stress: info: [8504] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd

.monitrc:

set daemon 5
check process stress
    matching "stress.*"
    stop program = "/usr/bin/pkill stress"
    if cpu > 5% for 2 cycles then stop

启动监控:

monit -Iv -c .monitrc
Starting Monit 5.11 daemon
'xps13' Monit started
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check skipped (initializing)
'stress' 
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check succeeded [current cpu usage=0.0%]
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check succeeded [current cpu usage=0.0%]
'stress' process is not running
'stress' trying to restart
'stress' start skipped -- method not defined

Monit 看到正确的进程(pid 匹配),但看到 0% 的使用率(压力是每个顶部 100% 使用 1 个 cpu)。我手动消除了压力,这是当 monit 说进程没有运行时(最后,上面)。因此,monit 可以正常监控进程,但没有看到正确的 cpu 使用情况。

有什么想法吗?

【问题讨论】:

    标签: monit ubuntu-15.04


    【解决方案1】:

    请注意,如果您的系统有许多内核,那么您只对其中一个(cpu 1)施加压力这一事实不会对整个系统造成压力。在我使用 i7 处理器进行的测试中,将 CPU 1 压力提高到 95% 只会将整个系统压力提高到 12.5%。

    根据内核数量,您可能需要相应地使用:

    monit -c X
    

    其中 X 是您想要强调的核心数量。

    但这不是您的主要问题。您的问题是您没有为 monit 提供压力程序的停止指令。看看这个:

    check process stress
    matching "stress.*"
    start program = "/usr/bin/stress -c 1" with timeout 10 seconds
    stop program = "/usr/bin/pkill stress"
    if cpu > 5% for 2 cycles then stop
    

    您至少缺少“停止”行,您可以在其中定义 monit 将用于实际停止进程的命令。由于压力不是服务,您可能需要使用 pkill 指令来终止进程。

    我成功测试了上面的配置。 monit.log 的输出:

    [CET Nov  5 09:03:02] info     : 'stress' start action done
    [CET Nov  5 09:03:02] info     : 'Overlord' start action done
    [CET Nov  5 09:03:12] info     : Awakened by User defined signal 1
    [CET Nov  5 09:03:22] error    : 'stress' cpu usage of 12.5% matches resource limit [cpu usage<5.0%]
    [CET Nov  5 09:03:32] error    : 'stress' cpu usage of 12.4% matches resource limit [cpu usage<5.0%]
    [CET Nov  5 09:03:32] info     : 'stress' stop: /usr/bin/pkill
    

    所以:假设您只是愿意测试,因此 CPU-Usage 无关紧要,只需使用我在上面提供的配置即可。一旦您确定您的配置有效,请调整您希望在生产环境中监控的进程的资源限制。

    随时有:https://mmonit.com/monit/documentation/

    希望对你有帮助。

    问候

    【讨论】:

    • 非常感谢您的详细回复。不幸的是,仍然遇到问题。请参阅编辑。如果这比它的价值更麻烦,我会完全理解。开始认为自己编写一些简单的实用程序会比调试监视器更容易。
    • 提供的配置在您的环境中不起作用根本没有意义......我假设您通过执行“sudo monit -t && sudo service monit restart && sudo monit”刷新了monit的配置更新配置文件后全部启动”?在这种情况下,我建议重新安装 monit。
    • -monit -c X +stress -c X
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    相关资源
    最近更新 更多