几个月前开始使用VPS,每月限制300GB流量,流量方便基本够用了,但是有时候由于受到一些恶意访问,导致CPU、Memory等资源消耗较大,导致VPS上的博客网站响应时间太慢甚至有时根本无法打开网页。所以,我简单做了个脚本来进行监控和发邮件报警。
由于不是做很专业的运维,暂时不想上很专业的监控工具,如:Nagios、Puppet、Cacti、Zabbix等,所以自己手动了个Shell脚本来监控我关心的CPU、Load、Memory、网络传输、博客网站能否打开等内容。将该监控脚本分享如下:
https://github.com/smilejay/shell/blob/master/sh2013/vps_monitor.sh
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#!/bin/bash #set -x # the script to monitor my VPS # It will alert when memory, load, CPU%, networking, httpd/mysqld or home-page # is in an abnormal state. # author: Jay # date: 2013-10-16 EMAIL="smile665@gmail.com" WARN_MSG="" # alert when free memory is less than 50 MB function mem_free() { threshold=50 # 50 MB free memory free_mem=$(free -m | grep "buffers/cache" | awk '{print $4}') if [ $free_mem -lt $threshold ]; then WARN_MSG=$WARN_MSG"Free memeory is less than $threshold MB.\n" return 1 fi return 0 } # alert when load (5min) is larger than 4 function load_avg() { threshold=4 # load is 4 load_5min=$(cat /proc/loadavg | awk '{print $2}') if [ $(echo "$load_5min > $threshold" | bc) -eq 1 ]; then WARN_MSG=$WARN_MSG"5min average load is larger than $threshold.\n" return 1 fi return 0 } # alert when CPU idle% is less than 20% function cpu_idle() { threshold=20 # CPU idle% 20% cpu_idle=$(sar 1 5 | grep -i 'Average' | awk '{print $NF}') if [ $(echo "$cpu_idle < $threshold" | bc) -eq 1 ]; then # in printf cmd, %% represents a single % WARN_MSG=$WARN_MSG"CPU idle%% is less than $threshold%%.\n" return 1 fi return 0 } # alert when networking tx speed is larger than 80 kB/s function network_tx() { threshold=80 # TX speed 80 kB/s interface=eth0 tx_speed=$(sar -n DEV 10 5 | grep "Average" | grep "$interface" | awk '{print $6}') if [ $(echo "$tx_speed > $threshold" | bc) -eq 1 ]; then WARN_MSG=$WARN_MSG"networking TX speed is larger than $threshold kB/s.\n" return 1 fi return 0 } # alert when httpd or mysqld process doesn't exist function httpd_mysqld() { ps -ef | grep 'httpd' | grep -v 'grep' if [ $? -ne 0 ]; then WARN_MSG=$WARN_MSG"httpd process doesn't exist.\n" return 1 else ps -ef | grep 'mysqld' | grep -v 'grep' if [ $? -ne 0 ]; then WARN_MSG=$WARN_MSG"mysqld process doesn't exist.\n" return 1 fi fi return 0 } # alert when 'Stay hungry' doesn't exist in the home page http://smilejay.cn/ function home_page() { url=http://smilejay.cn/ keywords="Stay hungry" curl -sL $url | grep -i "$keywords" if [ $? -ne 0 ]; then WARN_MSG=$WARN_MSG"keywords '$keywords' doesn't exist on link $url.\n" return 1 fi return 0 } # use an array to store the return value of each monitoring function mem_free chk[1]=$? load_avg chk[2]=$? cpu_idle chk[3]=$? network_tx chk[4]=$? httpd_mysqld chk[5]=$? home_page chk[6]=$? # send warning email to the web master if any of the check fails. for i in $(seq 1 6) do if [ ${chk[i]} -ne 0 ]; then printf "$WARN_MSG" | mailx -s "Warning from smilejay.cn" $EMAIL exit 1 fi done |
写注释的程序员是个好程序员
请问博主,我是各站长但是,不懂技术,最近也碰到相关问题,我想知道,这个脚本放在vps【lnmp】,那个文件夹,怎么使用啊?
这个。。。 还是需要懂一点技术的~~ 不然就不好告诉你怎么用了~~ 我也没能力和时间告诉你很多计算机的最基础的东西。