如何将monit配置为在几秒钟后终止高CPU进程



我想使用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看到了正确的进程(pids匹配),但看到了0%的使用率(压力是每个顶部使用100%的1个cpu)。我手动消除了压力,也就是当monit说流程没有运行时(在最后,如上)。因此,monit可以很好地监控进程,但没有看到正确的cpu使用情况。

有什么想法吗?

请注意,如果您的系统有许多核心,那么只强调其中一个核心(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指令来终止进程。

我成功地测试了上述配置。监控日志的输出:

[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/

希望能有所帮助。

问候

我认为您看到0%cpu的原因是因为stress -c 1创建了两个进程-一个"工人;将创建负载的进程和第二个大部分空闲的后台进程(打开htop并过滤压力以查看第二个进程)。

如果正则表达式匹配多个进程,monit将选择运行时间最长的进程(查看monit文档)-对我来说,后台进程的运行时间总是比";工人;过程

你可以通过使用压力ng来缓解这种情况。这里的";工人;进程有一个不同的名称,所以在匹配时并没有歧义。

stress-ng -c 1

使用以下.monitrc文件

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

最新更新