Linux Server System load monitoring-shell script

Source: Internet
Author: User

One, monitoring server system load situation:

1, use the uptime command to view the current load situation (1 minutes, 5 minutes, 15 minutes average load situation)
# uptime
15:43:59 up 186 days, 20:04, 1 user, load average:0.01, 0.02, 0.00

Rule of thumb for system load: (detail reference: http://blog.csdn.net/skyline_loafer/article/details/26940539)
(1) The main observation "15 minutes system load", it as the normal operation of the computer indicators.
(2) If within 15 minutes (the system load divided by the number of CPU cores) The average load is greater than 1.0, indicating that the problem persists, not a temporary phenomenon.
(3) When the system load continues to be greater than 0.7, you must start investigating where the problem is and prevent the situation from deteriorating.
(4) When the system load continues to be greater than 1.0, you must find a solution to this value down.
(5) When the system load reaches 5.0, it indicates that your system has a serious problem, long time no response, or close to the freezing.

2. View the total number of cores of the server CPU
# grep-c ' model name '/proc/cpuinfo

3, intercept the server 1 minutes, 5 minutes, 15 minutes of the load situation
# Uptime | awk ' {print $8,$9,$10,$11,$12} ' (Note: Here are 1 symbols on the left!!!) )
Load average:0.01, 0.02, 0.00

4. View the average load of 15 minutes
# Uptime | awk ' {print $} '

(using ' {print $} ' is not accurate enough, and if you use awk to take the 12th field, the result may be empty.) and use the $NF table to output the last paragraph of the content)
# Uptime | awk ' {print $NF} '

5, write the system load monitoring script file:
# vim/scripts/load-check.sh

    1. #!/bin/bash
    2. #使用uptime命令监控linux系统负载变化
    3. #取系统当前时间 (write files in append >>)
    4. Date >>/scripts/datetime-load.txt
    5. #提取服务器1分钟, 5-minute, 15-minute load conditions
    6. Uptime | awk ' {print $8,$9,$10,$11,$12} ' >>/scripts/load.txt
    7. #逐行连接上面的时间和负载相关行数据 (re-write file > each time)
    8. Paste/scripts/datetime-load.txt/scripts/load.txt >/scripts/load_day.txt

# chmod a+x/scripts/load-check.sh

6, write the system load result file mail Send script:
# vim/scripts/sendmail-load.sh

    1. #!/bin/bash
    2. #把系统负载监控生成的load_day. txt files are sent to the user by mail
    3. #提取本服务器的IP地址信息
    4. ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
    5. #提取当前日期
    6. today= ' date-d "0 Day" +%y year%m month%d "
    7. #发送系统负载监控结果邮件
    8. echo "This is the system load monitoring report for the $IP server $today, please download the attachment. " | Mutt-s "System load monitoring report for the $IP server $today"-a/scripts/load_day.txt [email protected]

# chmod a+x/scripts/sendmail-load.sh

7, write the system load monitoring script file:
# vim/scripts/load-warning.sh

  1. #!/bin/bash
  2. #使用uptime命令监控linux系统负载变化
  3. #提取本服务器的IP地址信息
  4. ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
  5. #抓取cpu的总核数
  6. cpu_num= ' grep-c ' model name '/proc/cpuinfo '
  7. #抓取当前系统15分钟的平均负载值
  8. load_15= ' Uptime | awk ' {print $NF} '
  9. #计算当前系统单个核心15分钟的平均负载值, the result is less than 1.0 when the front single digit is 0.
  10. Average_load= ' echo 'scale=2;  A= $load _15/$cpu _num;if (Length (a) ==scale (a)) print 0;print a "| BC '

  11. #取上面平均负载值的个位整数
  12. Average_int= ' echo $average _load | Cut-f 1-d "." `
  13. #设置系统单个核心15分钟的平均负载的告警值为0.70 (i.e. alarm when using more than 70%).
  14. load_warn=0.70
  15. #当单个核心15分钟的平均负载值大于等于1.0 (that is, single-digit integer greater than 0), direct email alarm, if less than 1.0 two times comparison
  16. if (($average _int > 0)); Then
  17. echo "$IP Server 15-minute system average load of $average_load, exceeding the alert value of 1.0, please immediately handle!!! " | Mutt-s "$IP Server system load critical alarm!!! "[Email protected]
  18. Else
  19. #当前系统15分钟平均负载值与告警值进行比较 (1 is returned when the alarm value is greater than 0.70, and 0 is returned if it is less than)
  20. load_now= ' expr $average _load \> $load _warn '
  21. #如果系统单个核心15分钟的平均负载值大于告警值0.70 (return value is 1), send an email to the administrator
  22. if (($Load_now = = 1)); Then
  23. echo "$IP server 15 minutes of system average load reached $average _load, exceeding the alert value of 0.70, please timely processing. " | Mutt-s "$IP Server system load Alarm" [email protected]
  24. Fi
  25. Fi

# chmod a+x/scripts/load-warning.sh

8, join the task plan: The system load is detected every 10 minutes, there is an alarm immediately send mail (10 minutes to detect), every morning 8 points to send a system load Check report

# CRONTAB-E

    1. */10 * * * */scripts/load-check.sh
    2. */10 * * * */scripts/load-warning.sh
    3. 0 8 * * */scripts/sendmail-load.sh


(Original digest from: http://huangrs.blog.51cto.com/2677571/788379/)

Linux Server System load monitoring-shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.