功能介绍:

1.系统方面:

                  (1)CPU检查报警

                  (2)内存检查,自动释放

                  (3)硬盘检查报警

                  (4)网络检查

2.服务方面:

                  (1)NGINX检查报警

                  (2)PHP检查报警

                  (3)MYSQL检查报警

代码如下:

#!/bin/bashIP=`ifconfig|awk'NR==2{print $2}'|awk-F: '{print $2}'`#一.检查系统情况#1.CPU检查echo"##########CPU state show##########"echo"__________________________________________________________________________________"top-n 1|grepCpuus=`top-n 1|grepCpu|awk'{print $2}'|awk-F"%"'{print $1}'`us100=`echo"$us*100"|bc|awk-F"."'{print $1}'`#echo us*100 = $us100sy=`top-n 1|grepCpu|awk'{print $3}'|awk-F"%"'{print $1}'`sy100=`echo"$sy*100"|bc|awk-F"."'{print $1}'`#echo sy*100 = $sy100cpu_number=`echo"$us100+$sy100"|bc`#echo us*100+sy*100 = $cpu_numberif[ $cpu_number -gt "7000"];thenecho"$IP CPU is hight"| mail -s "warn!server: $IP CPU is hight"15210942688@163.comelseecho-e "\033[32m ...CPU is (OK) ! \033[0m"fiecho"___________________________________________________________________________________"echo""#2.内存检查echo"##########MEM state show##########"echo"_____________________________________________________________________________________________"free-m|awk'NR<3{print}'free-g |grep-i mem |awk'{if($4 < 10){ printf("3") > "/proc/sys/vm/drop_caches"}}';echo-e "\033[32m ...内存 is (OK) ! \033[0m"echo"_____________________________________________________________________________________________"echo""#3.硬盘空间检查echo"##########DRIVE state show##########"echo"___________________________________________________________________________________"df-huse_df=`df-h|awk'NR==3{print $4}'`if[ $use_df == "90%"];thenecho"$IP DF is hight"| mail -s "warn!server: $IP DF is hight"15210942688@163.comelseecho-e "\033[32m ...硬盘 is (OK) ! \033[0m"fiif[ $use_df == "95%"];thenecho"$IP df is hight"| mail -s "warn!server: $IP DF is hight"15210942688@163.comelseecho-e "\033[32m ...硬盘 is (OK) ! \033[0m"fiecho"___________________________________________________________________________________"echo""#4.网络检查echo"##########NETWORK state show##########"ifconfig|awk-F":"'NR==2 {print $2}'|awk'BEGIN{print "IP"}{print $1}'ifconfig|grepRX|awk'NR==2{print}'echo-e "\033[32m ...网络 is (OK) ! \033[0m"echo"_____________________________________________________________________________________________"echo""#####################################################################################################二.检查nginx、php、mysql状态#1.nginx ps and netstatecho"##########nginx state show##########"echo"___________________________________________________________________________________"nginx_port_number=`netstat-lntp|grep:80|wc-l`   #1nginx_process_number=`ps-ef|grepnginx|grep-vgrep|wc-l`  #5if[ $nginx_port_number == "0"];thenecho"$IP nginx is down"| mail -s "warn!server: $IP nginx_port is down"15210942688@163.com/usr/local/nginx/sbin/nginxif[ $test_nginx != "0"];thenecho"$IP nginx is down"| mail -s "warn!server: $IP not't restart"15210942688@163.comfielseecho-e "\033[32m ...nginx is running(OK) ! $(date) \033[0m"fiif[ $nginx_process_number == "0"];thenecho"$IP nginx is down"| mail -s "warn!server: $IP nginx_process is down"15210942688@163.com/usr/local/nginx/sbin/nginxtest_nginx2=echo$?if[ $test_nginx2 != "0"];thenecho"$IP nginx is down"| mail -s "warn!server: $IP not't restart"15210942688@163.comfielseecho-e "\033[32m ...nginx is running(OK) ! $(date) \033[0m"fiecho"__________________________________________________________________________________"echo""#2.php ps and netstatecho"##########php state show##########"echo"__________________________________________________________________________________"php_fpm_port_number=`netstat-lntp|grep:9000|wc-l`  #1php_fpm_process_number=`ps-ef|grepphp-fpm|grep-vgrep|wc-l`  #129if[ $php_fpm_port_number == "0"];thenecho"$IP php-fpm is down"| mail -s "warn!server: $IP php_fpm_port is down"15210942688@163.com/etc/init.d/php-fpmrestarttest_php1=echo$?if[ $test_php1 != "0"];thenecho"$IP php-fpm is down"| mail -s "warn!server: $IP not't restart"15210942688@163.comfielseecho-e "\033[32m ...PHP is running(OK) ! $(date) \033[0m"fiif[ $php_fpm_process_number == "0"];thenecho"$IP nginx is down"| mail -s "warn!server: $IP php_fpm_process is down"15210942688@163.com/etc/init.d/php-fpmrestarttest_php2=echo$?if[ $test_php2 != "0"];thenecho"$IP php is down"| mail -s "warn!server: $IP not't restart"15210942688@163.comfielseecho-e "\033[32m ...PHP is running(OK) ! $(date) \033[0m"fiecho"__________________________________________________________________________________"echo""##3.mysql ps and netstat##echo "##########mysql state show##########"#echo "________________________________________________________________________________"#mysql_port_number=`netstat -lntp|grep :3306|wc -l`  #1#mysql_process_number=`ps -ef|grep mysql|grep -v grep|wc -l`  #2##if [ $mysql_port_number == "0" ];then#   echo "$IP mysql is down" | mail -s "warn!server: $IP mysql_port is down" 15210942688@163.com#   /etc/init.d/mysqld restart#   test_mysql1=echo $?#   if [ $test_mysql1 != "0" ];then#       echo "$IP mysql is down" | mail -s "warn!server: $IP not't restart" 15210942688@163.com#   fi#else#   echo -e "\033[32m ...MYSQL is running(OK) ! $(date) \033[0m"#fi##if [ $mysql_process_number == "0" ];then#        echo "$IP mysql is down" | mail -s "warn!server: $IP mysql_process is down" 15210942688@163.com#        /etc/init.d/mysqld restart#        test_mysql2=echo $?#        if [ $test_php2 != "0" ];then#                echo "$IP mysql is down" | mail -s "warn!server: $IP not't restart" 15210942688@163.com#        fi#else#   echo -e "\033[32m ...MYSQL is running(OK) ! $(date) \033[0m"#fi#echo "________________________________________________________________________________"

171306308.jpg测试成功的回显如下图,可以通过计划任务+脚本实现实时监控。

可以参考: