0

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

有任何想法吗?

4

1 回答 1

2

请注意,如果您的系统有许多内核,那么您只强调其中一个(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/

希望能帮助到你。

问候

于 2015-11-05T08:14:18.807 回答