linux-shell脚本,监控nginx,tomcat,redis,php-cli,磁盘状态

邮件配置:

[root@mycentos sh]# which mail
/bin/mail
You have new mail in /var/spool/mail/root
[root@mycentos sh]#
从上面的命令看,系统已经安装了mail,对些我们还需要设置一下mail,让它使用外面的邮箱进行发邮件。设置文件是 /etc/mail.rc
我使用的是公司代理的邮箱,已经开启了smtp服务的了,如其他邮箱自行开启就行了
只需要在 /etc/mail.rc 最后添加两行即可,如下
set [email protected]
set smtp=smtp.exmail.qq.com
set smtp-auth-user=yangxingyi@xxoocom
set smtp-auth-password=你的邮件密码smtp的哦,如果是126等的要开通smtp功能
set smtp-auth=login
温馨提示:mail -s title [email protected] [email protected] < /sh/abc.log 可以发送多个用户哦
然后用crontab来啦,crontab 默认是分钟为单位的,所以需要在shell里面循环,做到秒级咯!

**

Tomcat秒级监控和nginx分钟监控:

**

#/bin/sh
#auth yangxingyi 2017-04-28 10:02
#email [email protected]
#update 2017-09-05 optimize second,2017-09-21 add nginx status 
#version 1.2
webIp="www101.200.196.146"
nginxStatus=$(ps -ef|grep /usr/local/nginx/sbin/nginx|wc -l)
if [ "$nginxStatus" -ge "2" ]
   then
       echo  "$(date) nginx is healthy" >> /sh/nginx.log
   else
       echo  "$(date) down" >> /sh/nginx.log
       /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf&
       echo "{$webIp} nginx was down!" | mail -s "nginx server down !" 269754243@qq.com
fi

for ((i=1;i<60;i++))
 do
   tomcatStatus=$(ps -ef|grep /usr/java/jdk1.7.0_67/bin/java|wc -l)
    if [ "$tomcatStatus" -ge "2" ]
      then
         echo  "$(date) healthy" >> /sh/tom.log
      else
         echo  "$(date) down" >> /sh/tom.log
         cd /opt/apache-tomcat-7.0.61/bin &&  /opt/apache-tomcat-7.0.61/bin/startup.sh &
         echo "{$webIp} java tomcat  was down!" | mail -s "java tomcat down !" 269754243@qq.com

    fi
    sleep 1
done

磁盘用量hard_disk_capacity_check.sh

#/bin/sh
#auth yangxingyi 2017-04-18 23:33
#update at 2017-04-30 11:57
#email [email protected]
#desc this script  check hard disk Size  Used/Avail ,when nginx log gt 2GB tar zcvf and echo null to log file 
#and  check /opt/apache-tomcat-7.0.61/logs/ ,when apache-tomcat log gt 2GB tar zcvf and echo null to log file 
userEmail="[email protected]"
webIp="www101.200.196.146"
rate=$(df -h | awk '{print $5}'| cut -d "%" -f1|sed -n '2p')
if [ $rate -ge 70 ]
   then
      #echo "$(date) hard disk  was gt 93% !" >> /sh/log_hard_disk_check   
      echo "hard disk full {$webIp}!" | mail -s "{$webIp} hard disk full !" {$userEmail}
fi

#if /home/wwwlogs/www.duoduofenqi.com.log size gt  1GB tar zcvf this log  file and  echo  null to this log file
logSize=$(ls -l /home/wwwlogs/www.duoduofenqi.com.log|awk '{print $5}')
#if [ "$logSize" -gt "1073741824" ]
if [ "$logSize" -gt "1073741824" ]
   then
     echo "nginx log filesize warming large then 1GB,{$webIp}!" | mail -s "nginx log filesize warming!" {$userEmail}
     tar -zcvf /var/www/logBackUp/www.duoduofenqi.com.log_$(date +"%Y_%m_%d").tar.gz /home/wwwlogs
     echo "">/home/wwwlogs/www.duoduofenqi.com.log
     echo "">/home/wwwlogs/www.duoduofenqi.com.error.log
     echo "nginx log size auto moved success!" | mail -s "nginx log size auto move and tar zcvf success!" {$userEmail}
fi
#/opt/apache-tomcat-7.0.61/logs/catalina.out
tomcatLogSize=$(ls -l /opt/apache-tomcat-7.0.61/logs/catalina.out|awk '{print $5}')
if [ "$tomcatLogSize" -gt "1073741824" ]
   then
        echo "position /opt/apache-tomcat-7.0.61/logs size warming bigger then 1GB!" | mail -s "tomcat log size warming large than 1GB!" {$userEmail}
        tar -zcvf /var/www/logBackUp/tomcat/catalina.out_log_$(date +"%Y_%m_%d").tar.gz  /opt/apache-tomcat-7.0.61/logs/catalina.out
         echo "">/opt/apache-tomcat-7.0.61/logs/catalina.out
fi
#backup 
backpath="/var/www/"
webpath="/var/www/www.duoduofenqi.com/"
today=`date  +%Y%m%d`
day02=`date -d '2 days ago' +%Y%m%d`
#cd  ${webpath}
#tar -zcvf ${backpath}backup_${today} ${webpath} --exclude *.txt --exclude *.csv --exclude *.log --exclude *.jpeg --exclude *.png --exclude *.json --exclude log
#--exclude Runtime --exclude *.xls --exclude *.docx
#rm  -rf ${backpath}backup_${day02}

PHP进程监控脚本

#!/bin/bash
#Date 2017-04-21 0:36
#update 2017-05-20 16:00
#auth yangxignyi
#mail [email protected]
#func:This script is for www server php-cli check CsvStatus,popIceFeedQue status,popRiskQue status,popIceUpdateFeedQue status,popIceAliPayQue status,pop_sync_kk_Que status
#if this php-cli was down  crontab will restart without  human,crontab can restart this shell!!!
#version 2.0
CsvStatus=$(ps -ef |grep Home/Worker/exportCsv|grep  index.php|awk '{print $8}')
popIceFeedQue=$(ps -ef |grep Home/Worker/popIceFeedQue|grep  index.php|awk '{print $8}')
popRiskQue=$(ps -ef |grep Home/Worker/popRiskQue|grep  index.php|awk '{print $8}')
popIceUpdateFeedQue=$(ps -ef |grep Home/Worker/popIceUpdateFeedQue|grep  index.php|awk '{print $8}')
popIceAliPayQue=$(ps -ef |grep Home/Worker/popIceAliPayQue|grep  index.php|awk '{print $8}')
pop_sync_kk_Que=$(ps  -ef|grep Home/Worker/pop_sync_kk_Que|grep  index.php|awk '{print $8}')
pop_newRiskCsv_Que=$(ps  -ef|grep Home/Worker/pop_newRiskCsv|grep  index.php|awk '{print $8}')
webPath="/var/www/www.duoduofenqi.com/"
logPath="/sh/csv.log"
userEmail="[email protected]"
webIp="101.200.196.146 php-cli"
if [ "$CsvStatus" == "php" ]
   then
      echo  "$(date) csv php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) csv php-cli was down" >> ${logPath}
       cd  ${webPath} && php index.php Home/Worker/exportCsv &
       echo "${webIp} php index.php Home/Worker/exportCsv &  was down!" | mail -s "php index.php Home/Worker/exportCsv down !" ${userEmail}
fi
#popIceFeedQue
if [ "$popIceFeedQue" == "php" ]
   then
      echo  "$(date) popIceFeedQue php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) popIceFeedQue php-cli was down" >> ${logPath}
       cd  ${webPath} && nohup php index.php Home/Worker/popIceFeedQue &
       echo "${webIp} php index.php Home/Worker/popIceFeedQue  was down!" | mail -s "php index.php Home/Worker/popIceFeedQue was down !" ${userEmail}
fi
#popRiskQue
if [ "$popRiskQue" == "php" ]
   then
      echo  "$(date) popRiskQue php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) popRiskQue php-cli was down" >> ${logPath}
       cd  ${webPath} && nohup php index.php Home/Worker/popRiskQue &
   #    echo "${webIp} php index.php Home/Worker/popRiskQue &  was down!" | mail -s "php index.php Home/Worker/popRiskQue was down !" ${userEmail}
fi
#popIceUpdateFeedQue
if [ "$popIceUpdateFeedQue" == "php" ]
   then
      echo  "$(date) popIceUpdateFeedQue php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) popIceUpdateFeedQue php-cli was down" >> ${logPath}
       cd  ${webPath} && nohup php index.php Home/Worker/popIceUpdateFeedQue &
       echo "${webIp} popIceUpdateFeedQue  was down!" | mail -s "php popIceUpdateFeedQue was down !" ${userEmail}
fi
#popIceAliPayQue
if [ "$popIceAliPayQue" == "php" ]
   then
      echo  "$(date) popIceAliPayQue php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) popIceAliPayQue php-cli was down" >> ${logPath}
       cd ${webPath}  && nohup php index.php Home/Worker/popIceAliPayQue  &
       echo "${webIp} php index.php Home/Worker/popIceAliPayQue >cli_ice_error.log 2>&1 &  was down!" | mail -s "php popIceAliPayQue was down !" ${userEmail}
fi
#pop_sync_kk_Que
if [ "$pop_sync_kk_Que" == "php" ]
   then
      echo  "$(date) pop_sync_kk_Que php-cli is healthy" >> ${logPath}
    else
       echo  "$(date) pop_sync_kk_Que php-cli was down" >> ${logPath}
       cd  ${webPath} && nohup php index.php Home/Worker/pop_sync_kk_Que&
       echo "${webIp} nohup php index.php Home/Worker/pop_sync_kk_Que >cli_ice_error.log 2>&1 &  was down!" | mail -s "php pop_sync_kk_Que was down !" ${userEmail}
fi

你可能感兴趣的:(centos)