设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

Linux服务器中高负载现象故障排查指南

2013-7-11 12:28| 发布者: 红黑魂| 查看: 2294| 评论: 0|来自: 51CTO

摘要: 技术支持分析师们常常接到用户对服务器高负载的控诉。事实上cPanel软件及其安装的应用很少引发服务器高负载情况。服务器拥有者、系统管理员或者服务器供应商应当对高负载状况进行初步调查,并在确认情况复杂后再向分 ...

脚本使用前的注意事项

重要事项:大家需要根据自己的理解来调整脚本中的数值。完美的默认值设定并不存在,因为不同的服务器环境

在实际运行中所应遵循的标准也不一样。举例来说,拥有十六个CPU核心的服务器在一分钟平均负载方面肯定要

高于只拥有一个CPU核心的服务器。

注意:大家需要将自己的电子邮箱地址添加到EMAIL变量当中,如下所示:

代码:

  1. EMAIL=you@example.com 

以下五项也需要根据实际情况加以调整:

  • MAX_LOAD
  • MAX_SWAP_USED
  • MAX_MEM_USED
  • MAX_PPS_OUT
  • MAX_PPS_IN

代码:

  1. #!/bin/sh 
  2. export PATH=/bin:/usr/bin 
  3. ########################################################################## 
  4. #                                                                        # 
  5. #  Copyright Jeff Petersen, 2009 - 2013                                  # 
  6. #                                                                        # 
  7. #  This program is free software: you can redistribute it and/or modify  # 
  8. #  it under the terms of the GNU General Public License as published by  # 
  9. #  the Free Software Foundation, either version 3 of the License, or     # 
  10. #  (at your option) any later version.                                   # 
  11. #                                                                        # 
  12. #  This program is distributed in the hope that it will be useful,       # 
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of        # 
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         # 
  15. #  GNU General Public License for more details.                          # 
  16. #                                                                        # 
  17. #  You should have received a copy of the GNU General Public License     # 
  18. #  along with this program.  If not, see <http://www.gnu.org/licenses/>. # 
  19. #                                                                        # 
  20. ########################################################################## 
  21. ############################################################################### 
  22. # START USER CONFIGURABLE VARIABLES 
  23. ############################################################################### 
  24. EMAIL="you@example.com" 
  25. # 1 minute load avg 
  26. MAX_LOAD=3 
  27. # kB 
  28. MAX_SWAP_USED=1000 
  29. # kB 
  30. MAX_MEM_USED=500000 
  31. # packets per second inbound 
  32. MAX_PPS_IN=2000 
  33. # packets per second outbound 
  34. MAX_PPS_OUT=2000 
  35. # max processes in the process list 
  36. MAX_PROCS=400 
  37. ############################################################################### 
  38. # END USER CONFIGURABLE VARIABLES 
  39. ############################################################################### 
  40. IFACE=`grep ETHDEV /etc/wwwacct.conf | awk '{print $2}'` 
  41. if [[ "$IFACE" =~ "venet" ]] ; then 
  42. IFACE=venet0 
  43. fi 
  44. IFACE=${IFACE}: 
  45. ############################################################################### 
  46. # 1 min load avg 
  47. ############################################################################### 
  48. ONE_MIN_LOADAVG=`cut -d . -f 1 /proc/loadavg` 
  49. echo "1 minute load avg: $ONE_MIN_LOADAVG" 
  50. ############################################################################### 
  51. # swap used 
  52. ############################################################################### 
  53. SWAP_TOTAL=`grep ^SwapTotal: /proc/meminfo | awk '{print $2}'` 
  54. SWAP_FREE=`grep ^SwapFree: /proc/meminfo | awk '{print $2}'` 
  55. let "SWAP_USED = (SWAP_TOTAL - SWAP_FREE)" 
  56. echo "Swap used: $SWAP_USED kB" 
  57. ############################################################################### 
  58. # mem used 
  59. ############################################################################### 
  60. MEM_TOTAL=`grep ^MemTotal: /proc/meminfo | awk '{print $2}'` 
  61. MEM_FREE=`grep ^MemFree: /proc/meminfo | awk '{print $2}'` 
  62. let "MEM_USED = (MEM_TOTAL - MEM_FREE)" 
  63. echo "Mem used: $MEM_USED kB" 
  64. ############################################################################### 
  65. # packets received 
  66. ############################################################################### 
  67. PACKETS_RX_1=`grep $IFACE /proc/net/dev | awk '{print $2}'` 
  68. sleep 2; 
  69. PACKETS_RX_2=`grep $IFACE /proc/net/dev | awk '{print $2}'` 
  70. let "PACKETS_RX = (PACKETS_RX_2 - PACKETS_RX_1) / 2" 
  71. echo "packets received (2 secs): $PACKETS_RX" 
  72. ############################################################################### 
  73. # packets sent 
  74. ############################################################################### 
  75. PACKETS_TX_1=`grep $IFACE /proc/net/dev | awk '{print $10}'` 
  76. sleep 2; 
  77. PACKETS_TX_2=`grep $IFACE /proc/net/dev | awk '{print $10}'` 
  78. let "PACKETS_TX = (PACKETS_TX_2 - PACKETS_TX_1) / 2" 
  79. echo "packets sent (2 secs): $PACKETS_TX" 
  80. let "SWAP_USED = SWAP_TOTAL - SWAP_FREE" 
  81. if [ ! "$SWAP_USED" == 0 ] ; then 
  82. PERCENTAGE_SWAP_USED=`echo $SWAP_USED / $SWAP_TOTAL | bc -l` 
  83. TOTAL_PERCENTAGE=`echo ${PERCENTAGE_SWAP_USED:1:2}%` 
  84. else 
  85. TOTAL_PERCENTAGE='0%' 
  86. fi 
  87. ############################################################################### 
  88. # number of processes 
  89. ############################################################################### 
  90. MAX_PROCS_CHECK=`ps ax | wc -l` 
  91. send_alert() 
  92. SUBJECTLINE="`hostname` [L: $ONE_MIN_LOADAVG] [P: $MAX_PROCS_CHECK] 
  93. [Swap Use: $TOTAL_PERCENTAGE ] [pps in: $PACKETS_RX  pps out: $PACKETS_TX]" 
  94. ps auxwwwf | mail -s "$SUBJECTLINE" $EMAIL 
  95. exit 
  96. if   [ $ONE_MIN_LOADAVG -gt $MAX_LOAD      ] ; then send_alert 
  97. elif [ $SWAP_USED       -gt $MAX_SWAP_USED ] ; then send_alert 
  98. elif [ $MEM_USED        -gt $MAX_MEM_USED  ] ; then send_alert 
  99. elif [ $PACKETS_RX      -gt $MAX_PPS_IN    ] ; then send_alert 
  100. elif [ $PACKETS_TX      -gt $MAX_PPS_OUT   ] ; then send_alert 
  101. elif [ $MAX_PROCS_CHECK -gt $MAX_PROCS ] ; then send_alert 
  102. fi 

需要注意的是,进程列表的输出内容中包含一些有用的数列,涉及各个进程的CPU与内存使用情况:

  • %CPU
  • %MEM
  • VSZ
  • RSS
  • TIME (显示一个进程的存在时间)

我们可以通过多种方式剖析服务器负载高企的原因。下面我们列出几项常用方案--仅供参考,并不全面:

  • 利用mysqladmin processlist (或者简写为'mysqladmin pr')检查MySQL进程列表
  • 利用mytop检查MySQL进程列表
  • 查阅日志文件。了解服务器自身的反馈意见也很重要。您的服务器是否遭遇暴力破解?
  • 运行dmesg以检查可能存在的硬件故障
  • 利用netstat查看服务器连接

下面则是值得关注的日志文件及其保存路径:

  • 系统日志: /var/log/messages, /var/log/secure
  • SMTP日志: /var/log/exim_mainlog, /var/log/exim_rejectlog, /var/log/exim_paniclog
  • POP3/IMAP日志: /var/log/maillog
  • Apache日志: /usr/local/apache/logs/access_log, /usr/local/apache/logs/error_log, /usr/local/apache/logs/suexec_log, /usr/local/apache/logs/suphp_log
  • 网站日志: /usr/local/apache/domlogs/ (use this to find sites with traffic in the last 60 
  • seconds: find -maxdepth 1 -type f -mmin -1 | egrep -v 'offset|_log$')
  • Cron日志: /var/log/cron

大家也可以在评论栏中反馈您在工作中遇到的问题、对本篇文章的评论及其它任何希望与朋友们分享的信息。

作为一篇独立的指导性文章,我们不可避免会存在遗漏或者疏忽,期待您提出宝贵意见、也希望大家能从中受到一点启发。

原文链接:

http://forums.cpanel.net/f34/troubleshooting-high-server-loads-linux-servers-319352.html


酷毙
2

雷人

鲜花

鸡蛋

漂亮

刚表态过的朋友 (2 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部