linux 重启服务器脚本_使用简单脚本诊断Linux服务器负载问题

linux 重启服务器脚本

linux 重启服务器脚本

If you have been an admin for any length of time, you have certainly discovered situations where a server spikes in CPU use or memory utilization and/or load levels. Running `top` won’t always give you the answer, either. So how do you find those sneaky processes that are chewing up your system resources to be able to kill ’em?

如果您曾经担任管理员一段时间,则肯定会发现服务器CPU使用率或内存利用率和/或负载水平激增的情况。 运行“ top”也不会总是给您答案。 那么,如何找到那些消耗系统资源以杀死它们的偷偷摸摸的进程呢?

The following script might be able to help. It was written for a web server, so has some parts of it that are specifically looking for httpd processes and some parts that deal with MySQL. Depending on your server deployment, simply comment/delete those sections and add others. It should be used for a starting point.

以下脚本可能会有所帮助。 它是为Web服务器编写的,因此其中的某些部分专门寻找httpd进程,而某些部分则处理MySQL。 根据您的服务器部署,只需注释/删除这些部分并添加其他部分。 它应该用作起点。

Prerequisites for this version of the script is some freeware released under the GNU General Public License called mytop (available at http://jeremy.zawodny.com/mysql/mytop/) which is a fantastic tool for checking how MySQL is performing. It is getting old, but still works great for our purposes here. Additionally, I use mutt as the mailer – you may want to change the script to simply use the linux built in `mail` utility. I run it via cron every hour; adjust as you see fit. Oh – and this script needs to run as root since it does read from some protected areas of the server.

此脚本版本的前提条件是一些免费软件在GNU通用公共许可证下发行,称为mytop(可从http://jeremy.zawodny.com/mysql/mytop/获得 ),这是一种用于检查MySQL的性能的出色工具。 它已经变老了,但对于我们这里的目的仍然很有效。 另外,我使用mutt作为邮件程序–您可能希望更改脚本以仅使用内置于`mail`实用程序的linux。 我每小时通过cron运行一次; 视需要调整。 哦-该脚本需要以root用户身份运行,因为它确实从服务器的某些受保护区域读取。

So let’s get started, shall we?

那么,让我们开始吧?

First, set your script variables:

首先,设置脚本变量:

#!/bin/bash
#
# Script to check system load average levels to try to determine
# what processes are taking it overly high...
#
# 07Jul2010 tjones
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10

#!/bin/bash
#
# Script to check system load average levels to try to determine
# what processes are taking it overly high...
#
# 07Jul2010 tjones
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10

Next, check your load level to see if the script should continue:

接下来,检查您的负载级别以查看脚本是否应该继续:

# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

#如果负载水平大于所需水平,请启动脚本过程。 否则,退出0

if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo "**************************************" >>$tmpfile echo "Date: $dt " >>$tmpfile echo "Check System Load & Processes " >>$tmpfile echo "**************************************" >>$tmpfile

如果[$ loadLevel -gt $ levelToCheck]; 然后echo“”> $ tmpfile echo“ **************************************” >> $ tmpfile echo“ Date:$ dt” >> $ tmpfile echo“ Check System Load&Processes” >> $ tmpfile echo“ *********************** ***************“ >> $ tmpfile

And continue through the checks, writing the results to the temporary file. Add or delete items from here where applicable to your situation:

并继续进行检查,将结果写入临时文件。 在此处根据您的情况添加或删除项目:

# Get more variables from system:
httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Get more variables from system:
httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo "Load Level Is: $loadLevel" >>$tmpfile echo "*************************************************" >>$tmpfile

#显示当前负载水平:echo“ Load Level Is:$ loadLevel” >> $ tmpfile echo“ ********************************* ********************“ >> $ tmpfile

# Show number of httpd processes now running (not including children): echo "Number of httpd processes now: $httpdProcesses" >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示正在运行的httpd进程数(不包括子进程):echo“现在httpd进程数:$ httpdProcesses” >> $ tmpfile echo“ ******************* **********************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show process list: echo "Processes now running:" >>$tmpfile ps f -ef >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示进程列表:echo“正在运行的进程:” >> $ tmpfile ps f -ef >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Show current MySQL info: echo "Results from mytop:" >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前MySQL信息:echo“ mytop的结果:” >> $ tmpfile / usr / bin / mytop -u $ dbusr -p $ dbpw -b -d $ db >> $ tmpfile echo“ ******* **********************************************“ >> $ tmpfile echo”“ >> $ tmpfile

Notice with the top command, we are writing to two temp files. One is for the much smaller message to cell phone. If you don’t want the urgency of cell phone alerts at three in the morning, you can take this out (and take out the second mailing routine later in the script).

注意,使用top命令,我们正在写入两个临时文件。 一种是发送给手机的小得多的消息。 如果您不希望在凌晨三点收到手机警报,可以将其删除(并在脚本的后面删除第二个邮寄例程)。


# Show current top:
echo "top now shows:" >>$tmpfile
echo "top now shows:" >>$topfile
/usr/bin/top -b -n1 >>$tmpfile
/usr/bin/top -b -n1 >>$topfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile


# Show current top:
echo "top now shows:" >>$tmpfile
echo "top now shows:" >>$topfile
/usr/bin/top -b -n1 >>$tmpfile
/usr/bin/top -b -n1 >>$topfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile

More checks:

更多检查:


# Show current connections:
echo "netstat now shows:" >>$tmpfile
/bin/netstat -p >>$tmpfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile


# Show current connections:
echo "netstat now shows:" >>$tmpfile
/bin/netstat -p >>$tmpfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile

# Check disk space echo "disk space:" >>$tmpfile /bin/df -k >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#检查磁盘空间echo“ disk space:” >> $ tmpfile / bin / df -k >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

Then write the temporary file contents to a more permanent log file and email the results to the appropriate parties. The second mailing is the pared down results consisting simply of the standard out of `top`:

然后将临时文件的内容写到一个更永久的日志文件中,并将结果通过电子邮件发送给相应的参与者。 第二封邮件是精简后的结果,仅包含`top`中的标准:

# Send results to log file:
/bin/cat $tmpfile >>$logfile

# Send results to log file:
/bin/cat $tmpfile >>$logfile

# And email results to sysadmin: /usr/bin/mutt -s "$machine has a high load level! - $dt" -a $mysqlLog -a $msgLog $mailstop >$logfile

#然后将结果通过电子邮件发送给sysadmin:/ usr / bin / mutt -s“ $ machine的负载很高!-$ dt” -a $ mysqlLog -a $ msgLog $ mailstop> $ logfile

And then some housekeeping and exit:

然后做一些客房整理并退出:

# And then remove the temp file:
rm $tmpfile
rm $topfile
fi

# And then remove the temp file:
rm $tmpfile
rm $topfile
fi

# exit 0

#退出0

Hopefully this helps someone out there. Fully assembled script is:

希望这可以帮助某人。 完全组装的脚本是:

#!/bin/bash
#
# Script to check system load average levels to try to determine what processes are
# taking it overly high...
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10
# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

#!/bin/bash
#
# Script to check system load average levels to try to determine what processes are
# taking it overly high...
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10
# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

#如果负载水平大于所需水平,请启动脚本过程。 否则,退出0

if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo "**************************************" >>$tmpfile echo "Date: $dt " >>$tmpfile echo "Check System Load & Processes " >>$tmpfile echo "**************************************" >>$tmpfile

如果[$ loadLevel -gt $ levelToCheck]; 然后echo“”> $ tmpfile echo“ **************************************” >> $ tmpfile echo“ Date:$ dt” >> $ tmpfile echo“ Check System Load&Processes” >> $ tmpfile echo“ *********************** ***************“ >> $ tmpfile

# Get more variables from system: httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

#从系统获取更多变量:httpdProcesses =`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo "Load Level Is: $loadLevel" >>$tmpfile echo "*************************************************" >>$tmpfile

#显示当前负载水平:echo“ Load Level Is:$ loadLevel” >> $ tmpfile echo“ ********************************* ********************“ >> $ tmpfile

# Show number of httpd processes now running (not including children): echo "Number of httpd processes now: $httpdProcesses" >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示正在运行的httpd进程数(不包括子进程):echo“现在httpd进程数:$ httpdProcesses” >> $ tmpfile echo“ ******************* **********************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show process list: echo "Processes now running:" >>$tmpfile ps f -ef >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示进程列表:echo“正在运行的进程:” >> $ tmpfile ps f -ef >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Show current MySQL info: echo "Results from mytop:" >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前MySQL信息:echo“ mytop的结果:” >> $ tmpfile / usr / bin / mytop -u $ dbusr -p $ dbpw -b -d $ db >> $ tmpfile echo“ ******* **********************************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show current top: echo "top now shows:" >>$tmpfile echo "top now shows:" >>$topfile /usr/bin/top -b -n1 >>$tmpfile /usr/bin/top -b -n1 >>$topfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前顶部:echo“顶部现在显示:” >> $ tmpfile echo“顶部现在显示:” >> $ topfile / usr / bin / top -b -n1 >> $ tmpfile / usr / bin / top -b- n1 >> $ topfile echo“ *********************************************** ******“ >> $ tmpfile echo”“ >> $ tmpfile

# Show current connections: echo "netstat now shows:" >>$tmpfile /bin/netstat -p >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前连接:echo“ netstat现在显示:” >> $ tmpfile / bin / netstat -p >> $ tmpfile echo“ ********************** *******************************“” >> $ tmpfile echo“” >> $ tmpfile

# Check disk space echo "disk space:" >>$tmpfile /bin/df -k >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#检查磁盘空间echo“ disk space:” >> $ tmpfile / bin / df -k >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Send results to log file: /bin/cat $tmpfile >>$logfile

#将结果发送到日志文件:/ bin / cat $ tmpfile >> $ logfile

# And email results to sysadmin: /usr/bin/mutt -s "$machine has a high load level! - $dt" -a $mysqlLog -a $msgLog $mailstop >$logfile

#然后将结果通过电子邮件发送给sysadmin:/ usr / bin / mutt -s“ $ machine的负载很高!-$ dt” -a $ mysqlLog -a $ msgLog $ mailstop> $ logfile

# And then remove the temp file: rm $tmpfile rm $topfile fi

#然后删除临时文件:rm $ tmpfile rm $ topfile fi

# exit 0

#退出0

翻译自: https://www.howtogeek.com/50934/diagnose-linux-server-load-problems-with-a-simple-script/

linux 重启服务器脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值