十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件
Posted 心动丶男孩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件相关的知识,希望对你有一定的参考价值。
发送邮箱小工具,将它放在#/usr/bin/mail chmod +x /usr/bin/mail
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
server = ‘smtp.163.com‘
port = ‘25‘
def sendmail(server,port,user,pwd,msg):
smtp = smtplib.SMTP()
smtp.connect(server,port)
smtp.login(user, pwd)
smtp.sendmail(msg[‘from‘], msg[‘to‘], msg.as_string())
smtp.quit()
print(‘邮件发送成功email has send out !‘)
if __name__ == ‘__main__‘:
msg = email.mime.multipart.MIMEMultipart()
msg[‘Subject‘] = ‘监控‘
msg[‘From‘] = ‘[email protected]‘
msg[‘To‘] = ‘[email protected]‘
user = ‘python4_mail‘
pwd = ‘sbalex3714‘
content=‘%s\n%s‘ %(‘\n‘.join(sys.argv[1:4]),‘ ‘.join(sys.argv[4:])) #格式处理,专门针对我们 的邮件格式
txt = email.mime.text.MIMEText(content, _charset=‘utf-8‘)
msg.attach(txt)
sendmail(server,port,user,pwd,msg)
监控脚本
#!/bin/bash
#/author/dengsiyuan
cpu_limit=0 #定义cpu报警线
memory_limit=0 #定义内存报警线
disk=‘/dev/sda1‘ # 定义要监控的磁盘
disk_inode_limit=0 #定义磁盘inode报警线
disk_space_limit=0 #定义磁盘使用空间报警线
function bc_install() #查看是否安装了bc软件包
{
rpm -qa |grep ‘^bc.*$‘ >> /dev/null
if [ $? -eq 0 ];then
echo ‘bc has been installed‘
else
yum install bc -y
fi
}
function nettools_install() #查看是否安装了nettools 软件包
{
rpm -qa |grep net-tools >> /dev/null
if [ $? -eq 0 ];then
echo ‘nettools has been installed‘
else
yum install net-tools -y
Fi
}
function monitor_cpu()
{
cpu_id_free= `vmstat 1 5 |awk ‘NR>=3{ x = x + $15 } END {print x/5}‘|awk -F. ‘{print $1}‘`
#提取CPU空余空间
cpu_used= $((100-cpu_id_free)) #提取CPU使用空间
if [ $cpu_used -gt $cpu_limit ]then; #与cpu使用率报警线比较
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
MSG:cpu usage exceeds the limit ,current value is ${cpu_used}%"
echo $msg
/usr/bin/mail $msg
fi
}
function monitor_mem()
{
mem_total=`free |awk ‘NR==2{print $2}‘` #提取内存总量
mem_used=`free |awk ‘NR==2{print $3}‘` #提取内存使用
mem_percent=`echo "scale=2;$mem_used/$mem_total" |bc -l |cut -d. -f2` #计算内存使用率
if [ $mem_percent -gt $mem_limit ];then #与内存使用率报警线比较
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
-- IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
MSG:mem usage exceeds the limit,current value is ${mem_percent}%"
echo $msg
/usr/bin/mail $msg # 调用邮件
fi
}
function monitor_disk_inode()
{
disk_inode_used=`df -i $disk |awk ‘NR==2{print $5}‘|cut -d% -f1` #提取磁盘Inode使用情况
if [ $disk_inode_used -gt $disk_inode_limit ];then #比较
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
MSG:disk_inode usage exceeds the limit,current vaule is ${disk_inode_used}%"
echo $msg
/usr/bin/mail $msg
fi
}
function monitor_disk_space()
{
disk_space_used=`df $disk |awk ‘NR==2{print $5}‘|cut -d% -f1` #提取磁盘使用情况
if [ $disk_space_used -gt $disk_space_limit ];then #比较
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
--PADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
MSG:disk_space_used usage exceeds the limit,current vaule is ${disk_space_used}%"
echo $msg
/usr/bin/mail $msg
fi
}
bc_install
nettools_install
monitor_cpu &>> /tmp/monitor.log
monitor_mem &>> /tmp/monitor.log
monitor_disk_inode &>> /tmp/monitor.log
monitor_disk_space &>> /tmp/monitor.log
以上是关于十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件的主要内容,如果未能解决你的问题,请参考以下文章
AWS 监控与报警 aws CloudWatch 自动恢复硬件故障实例 Auto Recover