淫荡的一天又开始了!!!

一个让银蛋疼的博客



VPS自动监控Shell脚本 分享道

 该脚本用于监控VPS服务器负载,Web程序内存及CPU使用。当服务器系统负载或内存使用达到预设值,则重启该程序,或者某个php-cgi进程占用CPU过大,则直接kill掉该进程。目的在于缓解服务器资源耗尽导致意外宕机等情况。

嗯,没错。该脚本是此前 v1 的更新版本,考虑今后可能还会更新,故移到 github gist 进行简单的版本控制。

一、使用方法:

1
2
3
4
git clone git://gist.github.com/1216837.git gist-1216837
vim gist-1216837/sys-mon.sh //修改内存、CPU等预设阀值
mkdir /var/script
mv gist-1216837/sys-mon.sh /var/script

设置每分钟执行一次

1
2
crontab -e
* * * * * /bin/bash  /var/script/sys-mon.sh

二、Shell脚本内容

建议打开下面网址查看最新版本。

https://gist.github.com/1216837
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /bin/bash
#====================================================================
# sys-mon.sh
#
# Copyright (c) 2011, WangYan <webmaster@wangyan.org>
# All rights reserved.
# Distributed under the GNU General Public License, version 3.0.
#
# Monitor system mem and load, if too high, restart some service.
#
# See: https://wangyan.org/blog/sys-mon-shell-script.html
#
# V 2, since 2011-09-14
#====================================================================
 
# Need to monitor the service name
NAME_LIST="php-fpm mysql nginx"
 
# Single process to allow the maximum CPU (%)
PID_CPU_MAX="20"
 
# The maximum allowed memory (%)
SYS_MEM_MAX="90"
 
# The maximum allowed system load
SYS_LOAD_MAX="5"
 
# Log path settings
LOG_PATH="/var/log/autoreboot.log"
 
# Date time format setting
DATA_TIME=$(date +"%y-%m-%d %H:%M:%S")
 
# Your email address
EMAIL="wangyan@188.com"
 
#====================================================================
 
for NAME in $NAME_LIST
do
    SYS_CPU_SUM="0";SYS_MEM_SUM="0"
    PID_LIST=`ps aux | grep $NAME | grep -v root`
 
    IFS_TMP="$IFS";IFS=$'\n'
    for PID in $PID_LIST
    do
        PID_NUM=`echo $PID | awk '{print $2}'`
        PID_CPU=`echo $PID | awk '{print $3}'`
        PID_MEM=`echo $PID | awk '{print $4}'`
#       echo $NAME: $PID_NUM $PID_CPU $PID_MEM
 
        SYS_CPU_SUM=`echo $SYS_CPU_SUM + $PID_CPU | bc`
        SYS_MEM_SUM=`echo $SYS_MEM_SUM + $PID_MEM | bc`
 
        if [[ "$NAME" = "php-fpm" && "$PID_CPU" > "$PID_CPU_MAX" ]];then
            echo "$DATA_TIME kill $PID_NUM successful (CPU:$PID_CPU)" | tee -a $LOG_PATH
            kill $PID_NUM
        fi
    done
    IFS="$IFS_TMP"
 
    SYS_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`
    MEM_COMPARE=`awk 'BEGIN{print('$SYS_MEM_SUM'>'$SYS_MEM_MAX')}'`
    LOAD_COMPARE=`awk 'BEGIN{print('$SYS_LOAD'>'$SYS_LOAD_MAX')}'`
#   echo -e "$NAME: CPU_SUM:$SYS_CPU_SUM MEM_SUM:$SYS_MEM_SUM SYS_LOAD:$SYS_LOAD\n"
 
    if [[ "$MEM_COMPARE" = "1" || "$LOAD_COMPARE" = "1" ]];then
        /etc/init.d/$NAME stop
        if [ "$?" = "0" ];then
            echo "$DATA_TIME Stop $NAME successful (MEM:$SYS_MEM_SUM CPU:$SYS_CPU_SUM LOAD:$SYS_LOAD)" | tee -a $LOG_PATH
        else
            echo "$DATA_TIME Stop $NAME [failed] (MEM:$SYS_MEM_SUM CPU:$SYS_CPU_SUM LOAD:$SYS_LOAD)" | tee -a $LOG_PATH
            sleep 3
            pkill $NAME
        fi
        /etc/init.d/$NAME start
        if [ "$?" = "0" ];then
            echo "$DATA_TIME Start $NAME successful" | tee -a $LOG_PATH
        else
            echo "$DATA_TIME Start $NAME [failed]" | tee -a $LOG_PATH
            echo "$DATA_TIME Start $NAME failed" | mail -s "Start $NAME failed" $EMAIL
        fi
    fi
 
done

脚本内容不难理解,原理解释可参考《Linux 进程自动监控shell脚本》


  2011, September 15, 11:59 PM [0] 评论 (5341 views)

上一篇 | 下一篇
发表评论

评论内容 (必填):


浏览本博请不要用淫荡的眼神和思想,谢谢合作
Browse this blog, please do not use the lustful eyes and thought, thank you

  


搜索文章

高级搜索

实用链接

站长工具

规则转换

在线翻译 云输入法 IP归属在线查询 --在线偷窥-- 云网盘