服务器服务监测脚本(更新)
上一回发布了服务器监控脚本,这是该脚本的新版。此次更新内容包括:
1、添加了PATH处理,防止crontab执行脚本失败
2、精简了部分变量
3、添加了NRPE进程的监控
有参考上一版本脚本的朋友如有需要,可参照更新一下。
脚本内容如下:
#!/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 # nrpe --> nrpe # 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 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH SERVICE="httpd mysqld vsftpd nrpe" SERVICE_MAX=(70 5 5 1) LOGPATH="/root" APACHE_PATH="/usr/local/apache2" NGINX_PATH="/usr/local/nginx" PHP_PATH="/usr/local/php" TOMCAT_PATH="/usr/local/apache-tomcat-6.0.26" PURE_PATH="/usr/local/pureftpd" NRPE_PATH="/usr/local/nagios" i=0 if [ -e $LOGPATH/restart.log ] then tail -n 40 $LOGPATH/restart.log > $LOGPATH/restart.tmp rm -rf $LOGPATH/restart.log mv $LOGPATH/restart.tmp $LOGPATH/restart.log fi for serv in $SERVICE do counter=0 counter=`ps -A | grep "$serv" | wc -l` 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 ;; nrpe) #echo "nrpe start" $NRPE/bin/nrpe -c $NRPE/etc/nrpe.cfg ;; *) 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 ;; nrpe) #echo "nrpe start" killall nrpe $NRPE/bin/nrpe -c $NRPE/etc/nrpe.cfg -d ;; *) echo "Warning: Wrong service name while restartiing..." >> $LOGPATH/restart.log ;; esac fi let "i++" done exit 0 #End
上一篇: 我的iptables总结下一篇: 我的Nagios安装配置笔记
您好,我想问下这个脚本怎么用呢?不好意思我是菜鸟
@無名指:这个放到一个目录之后用chmod +x xxx.sh给予执行权限,然后在crontab中添加任务,就可以了
@無名指:把代码先复制,保存成xxx.sh的文件,用UTF-8格式,不然在linux服务器上执行会出错
我好奇的是你怎么做到的代码高亮?呵呵!
@Jomin:这是用的wordpress的代码高亮插件,叫Syntax Highlighter for WordPress,总的来说在高亮插件中还是不错的