我目前正在使用Monit监视Apache,并在内存使用率过高时重新启动它。 但是,我也希望能够监视所产生的各个apache2subprocess,并在几分钟内终止内存使用率过高的任何subprocess。 我怎样才能做到这一点?
Monit的文档build议您可以本地监控Apahce及其subprocess使用的总内存,而不是任何单个subprocess。
但是,您可以使用check programtesting来check program脚本的返回状态:
http://mmonit.com/monit/documentation/monit.html#program_status_testing
所以,你可以像这样做一个检查脚本:
#/bin/bash threshold=10000 # 10MB for childmem in $(ps h orss p $(pgrep -P $(cat /var/run/httpd.pid))) do if [ $childmem -gt $threshold ]; then exit 1 fi done exit 0
如果该脚本是/usr/local/bin/check_apache_children.sh ,则可以执行如下操作:
check program myscript with path "/usr/local/bin/check_apache_children.sh" if status != 0 then exec "/usr/local/bin/kill_apache_children.sh"
杀死脚本大概看起来像检查脚本,但在PID而不是退出。
这些脚本当然是说明性的,应该根据您的环境进行修改。
我上面接受了cjc的回答,但是想要发表我如何使用他的build议来解决这个问题。 请注意,您至less需要使用Monit 5.3才能使用Monit的“检查程序”。 我正在运行Debian。
在/ usr / local / bin目录/ monit_check_apache2_children:
#!/usr/bin/env bash log_file=/path/to/monit_check_apache2_children.log mem_limit=6 kill_after_minutes=5 exit_code=0 date_nice=$(date +'%Y-%m-%d %H:%M:%S') date_seconds=$(date +'%s') apache_children=$(ps h -o pid,%mem p $(pgrep -P $(cat /var/run/apache2.pid)) | sed 's/^ *//' | tr ' ' ',' | sed 's/,,/,/g') for apache_child in $apache_children; do pid=`echo $apache_child | awk -F, '{ print $1 }'` mem=`echo $apache_child | awk -F, '{ print $2 }'` mem_rounded=`echo $apache_child | awk -F, '{ printf("%d\n", $2 + 0.5) }'` if [ $mem_rounded -ge $mem_limit ]; then log_entry_count=$(cat $log_file | grep -v 'KILLED' | grep " $pid; " | wc -l) log_entry_time=$(cat $log_file | grep -v 'KILLED' | grep " $pid; " | tail -$kill_after_minutes | head -1 | awk '{ print $3 }') if [ "$1" != "kill" ]; then echo "$date_nice $date_seconds Process: $pid; Memory Usage: $mem" >> $log_file fi if [ $((date_seconds - log_entry_time)) -le $(((kill_after_minutes * 60) + 30)) ] && [ $log_entry_count -ge $kill_after_minutes ]; then if [ "$1" = "kill" ]; then kill -9 $pid echo "$date_nice $date_seconds ***** KILLED APACHE2 PROCESS: $pid; MEMORY USAGE: $mem" >> $log_file else exit_code=1 fi fi fi done exit $exit_code
的/ etc / monitrc:
... check program apache2_children with path "/usr/local/bin/monit_check_apache2_children" if status != 0 then exec "/usr/local/bin/monit_check_apache2_children kill" ...