阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux系统CPU、内存、硬盘、网络、LNMP服务整体监控邮件报警

229次阅读
没有评论

共计 5858 个字符,预计需要花费 15 分钟才能阅读完成。

Linux 系统 CPU、内存、硬盘、网络、LNMP 服务整体监控邮件报警

功能介绍:

1. 系统方面:

(1)CPU 检查报警

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

(3)硬盘检查报警

(4)网络检查

2. 服务方面:

(1)NGINX 检查报警

(2)PHP 检查报警

(3)MySQL 检查报警

代码如下:

#!/bin/bash
IP=`ifconfig|awk'NR==2{print $2}'|awk-F: '{print $2}'`
# 一. 检查系统情况
#1.CPU 检查
echo"##########CPU state show##########"
echo"_____________________________________________________________________________________________"
top-n 1|grepCpu
us=`top-n 1|grepCpu|awk'{print $2}'|awk-F"%"'{print $1}'`
us100=`echo"$us*100"|bc|awk-F"."'{print $1}'`
#echo us*100 = $us100
sy=`top-n 1|grepCpu|awk'{print $3}'|awk-F"%"'{print $1}'`
sy100=`echo"$sy*100"|bc|awk-F"."'{print $1}'`
#echo sy*100 = $sy100
cpu_number=`echo"$us100+$sy100"|bc`
#echo us*100+sy*100 = $cpu_number
if[$cpu_number -gt "7000"];then
echo"$IP CPU is hight"| mail -s "warn!server: $IP CPU is hight"15210942688@163.com
else
echo-e "\033[32m ...CPU is (OK) ! \033[0m"
fi
echo"_____________________________________________________________________________________________"
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-h
use_df=`df-h|awk'NR==3{print $4}'`
if[$use_df == "90%"];then
echo"$IP DF is hight"| mail -s "warn!server: $IP DF is hight"15210942688@163.com
else
echo-e "\033[32m ... 硬盘 is (OK) ! \033[0m"
fi
if[$use_df == "95%"];then
echo"$IP df is hight"| mail -s "warn!server: $IP DF is hight"15210942688@163.com
else
echo-e "\033[32m ... 硬盘 is (OK) ! \033[0m"
fi
echo"_____________________________________________________________________________________________"
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 netstat
echo"##########nginx state show##########"
echo"_____________________________________________________________________________________________"
nginx_port_number=`netstat-lntp|grep:80|wc-l` #1
nginx_process_number=`ps-ef|grepnginx|grep-vgrep|wc-l` #5
if[$nginx_port_number == "0"];then
echo"$IP nginx is down"| mail -s "warn!server: $IP nginx_port is down"15210942688@163.com
/usr/local/nginx/sbin/nginx
if[$test_nginx != "0"];then
echo"$IP nginx is down"| mail -s "warn!server: $IP not't restart"15210942688@163.com
fi
else
echo-e "\033[32m ...nginx is running(OK) ! $(date) \033[0m"
fi
if[$nginx_process_number == "0"];then
echo"$IP nginx is down"| mail -s "warn!server: $IP nginx_process is down"15210942688@163.com
/usr/local/nginx/sbin/nginx
test_nginx2=echo$?
if[$test_nginx2 != "0"];then
echo"$IP nginx is down"| mail -s "warn!server: $IP not't restart"15210942688@163.com
fi
else
echo-e "\033[32m ...nginx is running(OK) ! $(date) \033[0m"
fi
echo"_____________________________________________________________________________________________"
echo""
#2.php ps and netstat
echo"##########php state show##########"
echo"_____________________________________________________________________________________________"
php_fpm_port_number=`netstat-lntp|grep:9000|wc-l` #1
php_fpm_process_number=`ps-ef|grepphp-fpm|grep-vgrep|wc-l` #129
if[$php_fpm_port_number == "0"];then
echo"$IP php-fpm is down"| mail -s "warn!server: $IP php_fpm_port is down"15210942688@163.com
/etc/init.d/php-fpmrestart
test_php1=echo$?
if[$test_php1 != "0"];then
echo"$IP php-fpm is down"| mail -s "warn!server: $IP not't restart"15210942688@163.com
fi
else
echo-e "\033[32m ...PHP is running(OK) ! $(date) \033[0m"
fi
if[$php_fpm_process_number == "0"];then
echo"$IP nginx is down"| mail -s "warn!server: $IP php_fpm_process is down"15210942688@163.com
/etc/init.d/php-fpmrestart
test_php2=echo$?
if[$test_php2 != "0"];then
echo"$IP php is down"| mail -s "warn!server: $IP not't restart"15210942688@163.com
fi
else
echo-e "\033[32m ...PHP is running(OK) ! $(date) \033[0m"
fi
echo"_____________________________________________________________________________________________"
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 "_____________________________________________________________________________________________"

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

Linux 系统 CPU、内存、硬盘、网络、LNMP 服务整体监控邮件报警

 

相关阅读:

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 6.4 下的 LNMP 生产环境搭建及安装脚本 http://www.linuxidc.com/Linux/2013-11/92428.htm

生产环境实用之 LNMP 架构的编译安装 +SSL 加密实现 http://www.linuxidc.com/Linux/2013-05/85099.htm

LNMP 全功能编译安装 for CentOS 6.3 笔记 http://www.linuxidc.com/Linux/2013-05/83788.htm

CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.htm

在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm

Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计5858字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中