Home  >  Article  >  Database  >  服务器服务监测脚本

服务器服务监测脚本

WBOY
WBOYOriginal
2016-06-07 16:32:02888browse

由于最近一直在弄服务器,所以一直没有带来Arch Linux的东西,这篇文章给出的脚本依旧是为服务器准备的。写shell多了,C都快忘了怎么写了,这样不好

由于最近一直在弄服务器,所以一直没有带来Arch Linux的东西,这篇文章给出的脚本依旧是为服务器准备的。写shell多了,C都快忘了怎么写了,这样不好啊……

公司的服务器经常莫名其妙就挂,ssh上去又是好的,看看进程发现服务根本没打开,为什么呢?服务器被重启了!我的前任留下一个check.sh脚本,用来监测apache等进程,脚本在监测到进程过多时就重启服务器,结果有些服务器没有设置好开机启动那些apache之类的,就悲剧了。我就重新写了一个脚本,代替了crontab中原来的check.sh,在进程过多时不是重启服务器而是重启服务,这下服务器正常多了,网站那边负责的朝我喊“XXXX网站挂了,什么问题?”少了,哈哈。

废话多了点,脚本如下,使用前请一定看清代码并且修改其中一些路径之类的东西,以确保不会在你的服务器上造成什么不必要的伤害。

[bash]
#!/bin/bash
# service –> name to test
# apache –> apache|apache2|http|httpd
# nginx –> nginx
# mysql –> mysql|mysqld
# php-cgi –> php-cgi
# vsftpd –> vsftp|vsftpd
# pure-ftpd –> pure-ftp|pure-ftpd
# apache-tomcat –> java
# Set the service name you want to test and its max number of processes first.
# Don’t forget to check the bin path for each service.
# By Chen Zhidong
# http://sillydong.com

# This is a special one for the apache path is /usr/sbin

SERVICE=”apache2 mysqld vsftpd”
SERVICE_MAX=(35 5 5)

LOGPATH=”/root”
#APACHE_PATH=”/usr/sbin”
#MYSQL_PATH=”/usr/local/mysql”
#NGINX_PATH=”/usr/local/nginx”
#PHP_PATH=”/usr/local/php”
#TOMCAT_PATH=”/usr/local/apache-tomcat-6.0.26″
#PURE_PATH=”/usr/local/pureftpd”
i=0

if [ -e $LOGPATH/restart.log ]
then
tail -n 10 $LOGPATH/restart.log > $LOGPATH/restart.tmp
rm -rf $LOGPATH/restart.log
mv $LOGPATH/restat.tmp $LOGPATH/restart.log
fi

for serv in $SERVICE
do
#echo “$serv”
#echo “$i”
#echo “${SERVICE_MAX[i]}”

counter=0
counter=`ps -A | grep “$serv” | wc -l`
#echo “$counter”

if [ $counter -eq 0 ];then
echo “$serv is off at $(date), starting…” >> $LOGPATH/restart.log
case $serv in
apache*|http*)
#echo “apache start”
$APACHE_PATH/bin/apachectl restart >> $LOGPATH/restart.log
;;
nginx)
#echo “nginx start”
$NGINX_PATH/sbin/nginx -s reopen >> $LOGPATH/restart.log
;;
mysql*)
#echo “mysql start”
if [ -e /etc/init.d/mysqld ];
then
/etc/init.d/mysqld restart >> $LOGPATH/restart.log
else
/etc/init.d/mysql restart >> $LOGPATH/restart.log
fi
;;
php-cgi)
#echo “php-cgi start”
$PHP_PATH/sbin/php-fpm restart >> $LOGPATH/restart.log
;;
vsftp*)
#echo “vsftpd start”
/etc/init.d/vsftpd restart >> $LOGPATH/restart.log
;;
pure-ftp*)
#echo “pureftpd start”
$PURE_PATH/sbin/pure-ftpd -B >> $LOGPATH/restart.log
;;
java*) #apache-tomcat
#echo “tomcat start”
$TOMCAT_PATH/bin/startup.sh >> $LOGPATH/restart.log
;;
*)
echo “Wrong service name while starting…” >> $LOGPATH/restart.log
;;
esac
else
echo “$serv is on at $(date), next service…” >> $LOGPATH/restart.log
fi

if [ $counter -gt ${SERVICE_MAX[i]} ];then
echo “$(date), too many $serv, needs to restart…” >> $LOGPATH/restart.log
case $serv in
apache*|http*)
#echo “apache restart”
$APACHE_PATH/bin/apachectl restart >> $LOGPATH/restart.log
;;
nginx)
#echo “nginx restart”
$NGINX_PATH/sbin/nginx -s reload >> $LOGPATH/restart.log
;;
mysql*)
#echo “mysql restart”
if [ -e /etc/init.d/mysqld ];
then
/etc/init.d/mysqld restart >> $LOGPATH/restart.log
else
/etc/init.d/mysql restart >> $LOGPATH/restart.log
fi
;;
php-cgi)
#echo “php-cgi restart”
$PHP_PATH/sbin/php-fpm restart >> $LOGPATH/restart.log
;;
vsftp*)
#echo “vsftpd restart”
/etc/init.d/vsftp* restart >> $LOGPATH/restart.log
;;
pure-ftp*)
#echo “pureftpd restart”
$PURE_PATH/sbin/pure-ftpd -B >> $LOGPATH/restart.log
;;
java*) #apache-tomcat
#echo “tomcat restart”
$TOMCAT_PATH/bin/startup.sh >> $LOGPATH/restart.log
;;
*)
echo “Warning: Wrong service name while restartiing…” >> $LOGPATH/restart.log
;;
esac
fi

let “i++”
done

exit 0

#End
[/bash]

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:自编译MySQL指南 2.0Next article:MySQL主从原理(转)