脚本使用前的注意事项
重要事项:大家需要根据自己的理解来调整脚本中的数值。完美的默认值设定并不存在,因为不同的服务器环境 在实际运行中所应遵循的标准也不一样。举例来说,拥有十六个CPU核心的服务器在一分钟平均负载方面肯定要 高于只拥有一个CPU核心的服务器。
注意:大家需要将自己的电子邮箱地址添加到EMAIL变量当中,如下所示:
代码:
- EMAIL=you@example.com
以下五项也需要根据实际情况加以调整:
- MAX_LOAD
- MAX_SWAP_USED
- MAX_MEM_USED
- MAX_PPS_OUT
- MAX_PPS_IN
代码:
- #!/bin/sh
- export PATH=/bin:/usr/bin
- ##########################################################################
- # #
- # Copyright Jeff Petersen, 2009 - 2013 #
- # #
- # This program is free software: you can redistribute it and/or modify #
- # it under the terms of the GNU General Public License as published by #
- # the Free Software Foundation, either version 3 of the License, or #
- # (at your option) any later version. #
- # #
- # This program is distributed in the hope that it will be useful, #
- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
- # GNU General Public License for more details. #
- # #
- # You should have received a copy of the GNU General Public License #
- # along with this program. If not, see <http://www.gnu.org/licenses/>. #
- # #
- ##########################################################################
- ###############################################################################
- # START USER CONFIGURABLE VARIABLES
- ###############################################################################
- EMAIL="you@example.com"
- # 1 minute load avg
- MAX_LOAD=3
- # kB
- MAX_SWAP_USED=1000
- # kB
- MAX_MEM_USED=500000
- # packets per second inbound
- MAX_PPS_IN=2000
- # packets per second outbound
- MAX_PPS_OUT=2000
- # max processes in the process list
- MAX_PROCS=400
- ###############################################################################
- # END USER CONFIGURABLE VARIABLES
- ###############################################################################
- IFACE=`grep ETHDEV /etc/wwwacct.conf | awk '{print $2}'`
- if [[ "$IFACE" =~ "venet" ]] ; then
- IFACE=venet0
- fi
- IFACE=${IFACE}:
- ###############################################################################
- # 1 min load avg
- ###############################################################################
- ONE_MIN_LOADAVG=`cut -d . -f 1 /proc/loadavg`
- echo "1 minute load avg: $ONE_MIN_LOADAVG"
- ###############################################################################
- # swap used
- ###############################################################################
- SWAP_TOTAL=`grep ^SwapTotal: /proc/meminfo | awk '{print $2}'`
- SWAP_FREE=`grep ^SwapFree: /proc/meminfo | awk '{print $2}'`
- let "SWAP_USED = (SWAP_TOTAL - SWAP_FREE)"
- echo "Swap used: $SWAP_USED kB"
- ###############################################################################
- # mem used
- ###############################################################################
- MEM_TOTAL=`grep ^MemTotal: /proc/meminfo | awk '{print $2}'`
- MEM_FREE=`grep ^MemFree: /proc/meminfo | awk '{print $2}'`
- let "MEM_USED = (MEM_TOTAL - MEM_FREE)"
- echo "Mem used: $MEM_USED kB"
- ###############################################################################
- # packets received
- ###############################################################################
- PACKETS_RX_1=`grep $IFACE /proc/net/dev | awk '{print $2}'`
- sleep 2;
- PACKETS_RX_2=`grep $IFACE /proc/net/dev | awk '{print $2}'`
- let "PACKETS_RX = (PACKETS_RX_2 - PACKETS_RX_1) / 2"
- echo "packets received (2 secs): $PACKETS_RX"
- ###############################################################################
- # packets sent
- ###############################################################################
- PACKETS_TX_1=`grep $IFACE /proc/net/dev | awk '{print $10}'`
- sleep 2;
- PACKETS_TX_2=`grep $IFACE /proc/net/dev | awk '{print $10}'`
- let "PACKETS_TX = (PACKETS_TX_2 - PACKETS_TX_1) / 2"
- echo "packets sent (2 secs): $PACKETS_TX"
- let "SWAP_USED = SWAP_TOTAL - SWAP_FREE"
- if [ ! "$SWAP_USED" == 0 ] ; then
- PERCENTAGE_SWAP_USED=`echo $SWAP_USED / $SWAP_TOTAL | bc -l`
- TOTAL_PERCENTAGE=`echo ${PERCENTAGE_SWAP_USED:1:2}%`
- else
- TOTAL_PERCENTAGE='0%'
- fi
- ###############################################################################
- # number of processes
- ###############################################################################
- MAX_PROCS_CHECK=`ps ax | wc -l`
- send_alert()
- {
- SUBJECTLINE="`hostname` [L: $ONE_MIN_LOADAVG] [P: $MAX_PROCS_CHECK]
- [Swap Use: $TOTAL_PERCENTAGE ] [pps in: $PACKETS_RX pps out: $PACKETS_TX]"
- ps auxwwwf | mail -s "$SUBJECTLINE" $EMAIL
- exit
- }
- if [ $ONE_MIN_LOADAVG -gt $MAX_LOAD ] ; then send_alert
- elif [ $SWAP_USED -gt $MAX_SWAP_USED ] ; then send_alert
- elif [ $MEM_USED -gt $MAX_MEM_USED ] ; then send_alert
- elif [ $PACKETS_RX -gt $MAX_PPS_IN ] ; then send_alert
- elif [ $PACKETS_TX -gt $MAX_PPS_OUT ] ; then send_alert
- elif [ $MAX_PROCS_CHECK -gt $MAX_PROCS ] ; then send_alert
- 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 |