如何在几秒钟后配置monit以杀死高CPU进程?

时间:2015-11-01 22:24:11

标签: monit ubuntu-15.04

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

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

我的.monitrc:

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

我启动monit(我检查了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:

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使用情况。

有什么想法吗?

1 个答案:

答案 0 :(得分: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使用率不相关,只需使用我上面提供的confg。确定配置有效后,请调整要在生产环境中监控的流程的资源限制。

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

希望它有所帮助。

此致