针对于linux服务器CPU、memory、disk监控报警
#!/usr/bin/env bash
source /etc/profile
source ~/.bashrc
yum install bc -y
FILE=/usr/local/bin/sendEmail
if [ -f $FILE ];then
echo "文件已存在"
else
cd /root && wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
mkdir -p /usr/local/bin
tar zxf sendEmail-v1.56.tar.gz -C /usr/src/
cd /usr/src/sendEmail-v1.56/
cp -a sendEmail /usr/local/bin/
chmod +x /usr/local/bin/sendEmail
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
fi
Item=生产环境
Host=`/usr/bin/hostname`
IP=`ip a | grep 'inet'|awk -F '[ ]' '{print $6}' | grep 24`
Cpu=`top -b -n1 | fgrep "Cpu(s)" | tail -1 | awk -F ' ' '{ print $2 }'`
KMem=`top -b -n1 | fgrep "KiB Mem" |tail -l | awk -F ' ' '{ print $8 }'`
ZKMem=`top -b -n1 | fgrep "KiB Mem" |tail -l | awk -F ' ' '{ print $4 }'`
BFBMem=$(printf "%.3f" `echo "scale=3;$KMem*100/$ZKMem"|bc`)
SMem=`top -b -n1 | fgrep "KiB Swap" |tail -l| awk -F ' ' '{ print $5 }'`
ZSMem=`top -b -n1 | fgrep "KiB Swap" |tail -l| awk -F ' ' '{ print $3 }'`
DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
for I in $DEV
do dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`
mount=`df -Ph | grep $I | awk '{print $6}'`
echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
echo "$IP"
F=`echo $rate | awk -F% '{print $1}'`
if [ $F -ge 90 ];then
/usr/local/bin/sendEmail -f xxxxx@163.com -t xxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu qwolfking@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
-H 'Content-Type: application/json' \
-d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "系统资源监控:'$Item' \n\n 主机地址:'$IP' \n\n 主机名:'$Host' \n\n Disk Used:'$rate' \n\n Cpu Used:'$Cpu'% \n\n KiB Mem Used:'$BFBMem'% \n\n @xxxxx"}}'
fi
done
if [ $(echo "$Cpu > 90"|bc) -eq 1 ];then
/usr/local/bin/sendEmail -f xxxxx@163.com -t xxxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu xxxxx@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
-H 'Content-Type: application/json' \
-d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "'$Item' ":系统资源监控 \n\n 主机地址:'$Ip' \n\n 主机名:'$Host' \n\n Disk Used:'$rate' \n\n Cpu Used:'$Cpu' \n\n KiB Mem Used:'$BFBMem' \n\n @xxxxx"}}'
fi
echo $BFBMen
if [ (echo "$BFBMem > 95"|bc) -eq 1 ];then
/usr/local/bin/sendEmail -f xxxxx@163.com -t xxxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu xxxxx@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
-H 'Content-Type: application/json' \
-d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "'$Item' ":系统资源监控 \n\n 主机地址:'$Ip' \n\n 主机名:'$Host' \n\n Disk Used:'$rate' \n\n Cpu Used:'$Cpu' \n\n KiB Mem Used:'$BFBMem' \n\n @xxxxx"}}'
fi