当前位置:首页 > linux教程 > 列表

分享一个VPS系统监控资源的脚本

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-21 09:33:35 浏览: 评论:0 

以下是本人自己民写的一个Shell脚本,可以监控使用的VPS的CPU、Load、Memory、网络传输、网站能否打开等情况,感觉挺有用的,现在分享出来给需要的朋友.

几个月前开始使用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. #!/bin/bash 
  2. #set -x 
  3. # the script to monitor my VPS 
  4. # It will alert when memory, load, CPU%, networking, httpd/mysqld or home-page 
  5. # is in an abnormal state. 
  6. # author: Jay 
  7. # date: 2013-10-16 
  8.  
  9. EMAIL="smile665@gmail.com" 
  10. WARN_MSG="" 
  11.  
  12. # alert when free memory is less than 50 MB 
  13. function mem_free() 
  14. { 
  15.         threshold=50 # 50 MB free memory 
  16.         free_mem=$(free -m | grep "buffers/cache" | awk '{print $4}') 
  17.         if [ $free_mem -lt $threshold ]; then 
  18.                 WARN_MSG=$WARN_MSG"Free memeory is less than $threshold MB.n" 
  19.                 return 1 
  20.         fi 
  21.         return 0 
  22. } 
  23.  
  24. # alert when load (5min) is larger than 4 
  25. function load_avg() 
  26. { 
  27.         threshold=4 # load is 4 
  28.         load_5min=$(cat /proc/loadavg | awk '{print $2}') 
  29.         if [ $(echo "$load_5min > $threshold" | bc) -eq 1 ]; then 
  30.         WARN_MSG=$WARN_MSG"5min average load is larger than $threshold.n" 
  31.                 return 1 
  32.         fi 
  33.         return 0 
  34. } 
  35.  
  36. # alert when CPU idle% is less than 20% 
  37. function cpu_idle() 
  38. { 
  39.         threshold=20 # CPU idle% 20% 
  40.         cpu_idle=$(sar 1 5 | grep -i 'Average' | awk '{print $NF}') 
  41.         if [ $(echo "$cpu_idle < $threshold" | bc) -eq 1 ]; then 
  42.                 # in printf cmd, %% represents a single % 
  43.                 WARN_MSG=$WARN_MSG"CPU idle%% is less than $threshold%%.n" 
  44.                 return 1 
  45.         fi 
  46.         return 0 
  47. } 
  48.  
  49. # alert when networking tx speed is larger than 80 kB/s 
  50. function network_tx() 
  51. { 
  52.         threshold=80 # TX speed 80 kB/s 
  53.         interface=eth0 
  54.         tx_speed=$(sar -n DEV 10 5 | grep "Average" | grep "$interface" | awk '{print $6}') 
  55.         if [ $(echo "$tx_speed > $threshold" | bc) -eq 1 ]; then 
  56.                 WARN_MSG=$WARN_MSG"networking TX speed is larger than $threshold kB/s.n" 
  57.                 return 1 
  58.         fi 
  59.         return 0 
  60. } 
  61.  
  62. # alert when httpd or mysqld process doesn't exist 
  63. function httpd_mysqld() 
  64. { 
  65.         ps -ef | grep 'httpd' | grep -v 'grep' 
  66.         if [ $? -ne 0 ]; then 
  67.                 WARN_MSG=$WARN_MSG"httpd process doesn't exist.n" 
  68.                 return 1 
  69.         else 
  70.                 ps -ef | grep 'mysqld' | grep -v 'grep' 
  71.                 if [ $? -ne 0 ]; then 
  72.                         WARN_MSG=$WARN_MSG"mysqld process doesn't exist.n" 
  73.                         return 1 
  74.                 fi 
  75.         fi 
  76.         return 0 
  77. } 
  78.  
  79. # alert when 'Stay hungry' doesn't exist in the home page http://smilejay.com/ 
  80. function home_page() 
  81. { 
  82.         url=http://smilejay.com/ 
  83.         keywords="Stay hungry" 
  84.         curl -sL $url | grep -i "$keywords" 
  85.         if [ $? -ne 0 ]; then 
  86.                 WARN_MSG=$WARN_MSG"keywords '$keywords' doesn't exist on link $url.n"  --phpfensi.com 
  87.                 return 1 
  88.         fi 
  89.         return 0 
  90. } 
  91.  
  92. # use an array to store the return value of each monitoring function 
  93. mem_free 
  94. chk[1]=$? 
  95. load_avg 
  96. chk[2]=$? 
  97. cpu_idle 
  98. chk[3]=$? 
  99. network_tx 
  100. chk[4]=$? 
  101. httpd_mysqld 
  102. chk[5]=$? 
  103. home_page 
  104. chk[6]=$? 
  105.  
  106. # send warning email to the web master if any of the check fails. 
  107. for i in $(seq 1 6) 
  108. do 
  109.         if [ ${chk[i]} -ne 0 ]; then 
  110.                 printf "$WARN_MSG" | mailx -s "Warning from smilejay.com" $EMAIL 
  111.                 exit 1 
  112.         fi 
  113. done

Tags: VPS系统监控 VPS监控脚本

分享到: