服务器系统运行状态检测&邮件报警

服务器系统运行状态检测&邮件报警

每10分钟监测一次服务器运行状态,如果存在超出警戒值状态则邮件发送至管理员账户

警戒值:
网速:90%
目录:90%
CPU:90%
内存:剩余量小于1G

Centos7下使用mail发送邮件

首先检测相关服务是否已安装

[root@ProxyServer ~]# rpm -qa|grep mail
libreport-plugin-mailx-2.0.9-19.el6.x86_64
mailx-12.4-7.el6.x86_64
procmail-3.22-25.1.el6.x86_64
mailcap-2.1.31-2.el6.noarch
sendmail-8.14.4-8.el6.x86_64

若服务未安,则执行以下命令进行服务安装

 [root@ProxyServer ~]# yum install sendmail -y
[root@ProxyServer ~]# yum install mailx –y
[root@ProxyServer ~]# yum update libreport-plugin-mailx -y 
[root@ProxyServer ~]# yum install sharutils -y
[root@ProxyServer ~]# yum install mutt -y

为了防止垃圾邮件泛滥,阿里云服务器已不再开放smtp邮件的25端口,现在需要使用465端口进行smtp邮件发送。 生成密钥

[root@ProxyServer ~]mkdir -p /etc/pki/nssdb/
echo -n "" | openssl s_client -connect smtp.126.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /etc/pki/nssdb/126.crt
certutil -A -n '126' -t "P,P,P" -d . -i /etc/pki/nssdb/126.crt

2.配置密钥(红色代表修改内容)

[root@ProxyServer ~]# vi /etc/mail.rc
set bsdcompat
set from=aa123@163.com #之前设置好的邮箱地址
set smtp=smtps://smtp.163.com #邮件服务器
set smtp-auth-user=aa123@163.com #之前设置好的邮箱地址
set smtp-auth-password=lvyf0802 #授权码
set smtp-auth=login  #默认login
set ssl-verify=ignore #ssl认证方式
set nss-config-dir=/root/.certs #证书所在目录

服务器 硬盘、宽带、内存、CPU 监测脚本

需要添加报警邮箱接收地址
#!/bin/bash#这个脚本是用来统计CPU、磁盘、内存使用率、带宽的
total=0
system=0
user=0
i=0

#带宽使用情况
time=`date "+%Y-%m-%d %k:%M"`
day=`date "+%Y-%m-%d"`
minute=`date "+%k:%M"`
echo  "*************************************************************************" >> /var/log/check.log
echo  "**********************************" >> /var/log/check.log
echo "统计开始时间:$day $minute" >> /var/log/check.log

#循环五次,避免看到的是偶然的数据
echo "#带宽的使用情况:#" >>/var/log/check.log
while (( $i<5 ))
do
rx_before=$(cat /proc/net/dev | grep 'eth0' | awk 'NR==1' | tr : " " | awk '{print $2}')
tx_before=$(cat /proc/net/dev | grep 'eth0' | awk 'NR==1' | tr : " " | awk '{print $10}')
sleep 2
rx_after=$(cat /proc/net/dev | grep 'eth0' | awk 'NR==1' | tr : " " | awk '{print $2}')
tx_after=$(cat /proc/net/dev | grep 'eth0' | awk 'NR==1' | tr : " " | awk '{print $10}')

rx_result=$[(rx_after-rx_before)/1024/1024/2*8]
tx_result=$[(tx_after-tx_before)/1024/1024/2*8]
echo  "$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps" >>/var/log/check.log

let "i++"
done

rx_result=$(cat /var/log/check.log|grep "$time"|awk '{In+=$4}END{print In}')
tx_result=$(cat /var/log/check.log|grep "$time"|awk '{Out+=$7}END{print Out}')
In_Speed=$(echo "scale=2;$rx_result/5"|bc)
Out_Speed=$(echo "scale=2;$tx_result/5"|bc)
#echo "#带宽的5次的平均值是:#" >>/var/log/check.log
echo  "$time In_Speed_average: $In_Speed Mbps Out_Speed_average: $Out_Speed Mbps" >>/var/log/check.log

#CPU使用情况
which sar > /dev/null 2>&1
if [ $? -ne 0 ]
then
  total=`vmstat 1 5|awk '{x+=$13;y+=$14}END{print x+y}'`
  average=$(echo "scale=2;$total/5"|bc)
fi
echo "#CPU使用率:#" >>/var/log/check.log
echo "Total CPU  is already use: $average%" >>/var/log/check.log

#磁盘使用情况(注意:需要用sed先进行格式化才能进行累加处理)
disk_used=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{used+=$3} END{print used}')
disk_totalSpace=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{totalSpace+=$2} END{print totalSpace}')
disk_all=$(echo "scale=4;$disk_used/$disk_totalSpace" | bc)
disk_percent1=$(echo $disk_all | cut -c 2-3)
disk_percent2=$(echo $disk_all | cut -c 4-5)
disk_warning=`df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{if ($5>90) print $5"\t"$6;} '`
echo "#磁盘利用率#" >>/var/log/check.log
echo "hard disk has used: $disk_percent1.$disk_percent2%" >>/var/log/check.log
echo -e "\t\t#磁盘存在目录使用率超过90%报警#" >>/var/log/check.log
echo -e "\t\tover used: $disk_warning" >>/var/log/check.log

root_used=$(df -h | grep 'vda1'|sed -e  's/%/ /g'|awk -F ' ' '{print $5}')
if [ $root_used > 85 ];then
    echo "#根目录使用量#" >>/var/log/check.log
    echo "total_used:$root_used%" >>/var/log/check.log
fi

#内存使用情况
memery_used=$(free -m | awk 'NR==2' | awk '{print $3}')
buffer_used=$(free -m | awk 'NR==2' | awk '{print $6}')
cache_used=$(free -m | awk 'NR==2' | awk '{print $7}')
free=$(free -m | awk 'NR==2' | awk '{printf $4}')
memery_all=$(free -m | awk 'NR==2' | awk '{print $2}')
used_all=$[memery_all-(free+cache_used)]

echo "#内存剩余量#" >>/var/log/check.log
echo "Free:$free M" >>/var/log/check.log
echo "$used_all $memery_all $free" >>/var/log/check.log
memery_percent=$(echo "scale=4;$memery_used / $memery_all" | bc)
memery_percent2=$(echo "scale=4; $used_all / $memery_all" | bc)
percent_part1=$(echo $memery_percent | cut -c 2-3)
percent_part2=$(echo $memery_percent | cut -c 4-5) 
percent_part11=$(echo $memery_percent2 | cut -c 2-3)
percent_part22=$(echo $memery_percent2 | cut -c 4-5)
echo "#内存使用率#" >> /var/log/check.log
echo "system memery is already use: $percent_part1.$percent_part2%" >>/var/log/check.log
echo "actual memery is already use: $percent_part11.$percent_part22%" >>/var/log/check.log

echo  "结束本次统计:$day $minute" >> /var/log/check.log
echo  "*************************************************************************" >> /var/log/check.log
echo -e "\n\n" >> /var/log/check.log
if [[ $average > 90  ||  $free < 1024 || "$disk_warning" != "" ]];then
    echo '发送邮件'
    cat /var/log/check.log | tail -n 30 | mail -s '服务器预警' xxx@xxx.com
fi

crontab 定时运行

*/10 * * * * sh check.sh
每10分钟运行一次

crontab -e
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值