import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pymysql
import psutil
import os
import time def mail(name,description,disk_send_info,cpu_send_info,mem_send_info):
sender = 'mail_address'
password = 'mail_password'
reciver = 'xxx.xx@xx.com,xxx@xx.com' #可设置多人,用逗号分隔
subject = 'xxx(监控项目名称):%s警告'%name+ time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) msg = MIMEMultipart('mixed') #初始化 注意:标题和正文不能分别初始化
msg['From'] = sender
msg['To'] = reciver
msg['Subject'] = subject
msg.attach(MIMEText(str(description),'plain','utf-8'))
msg.attach(MIMEText(str(disk_send_info),'plain','utf-8'))#添加正式,MIMEText切记不能忘记
msg.attach(MIMEText(str(cpu_send_info),'plain','utf-8'))
msg.attach(MIMEText(str(mem_send_info),'plain','utf-8')) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.set_debuglevel(1)
smtp.login(sender,password)
smtp.sendmail(sender,reciver.split(','),msg.as_string())
print('%s 发送成功'%subject)
smtp.quit() #获得硬盘的信息
def disk_info(disk_find):
# disk = psutil.disk_partitions()
# for i in disk:
# disk_partition = i.device #所有硬盘盘符
# disk_use = psutil.disk_usage(disk_partition)
# disk_utilization = disk_use.percent
disk_use = psutil.disk_usage(disk_find)
disk_utilization = str(disk_use.percent)
disk_total = '%sG' %(round(disk_use.total/1024/1024/1024,2))
disk_used = '%sG' %(round(disk_use.used/1024/1024/1024,2))
disk_free = '%sG' %(round(disk_use.free/1024/1024/1024,2))
disk_msg = {'总量':disk_total,
'已使用':disk_used,
'空闲':disk_free,
'使用率':disk_utilization + '%'
}
return disk_msg def cpu_info():
cpu_idle = str(os.popen("top -n 1 | sed -n '3p' | awk '{print $8}'").read()).strip()
cpu_used = str(100-float(cpu_idle)) + '%'
cpu_msg = {'已使用':cpu_used,
'剩余':str(cpu_idle) + '%'}
return cpu_idle,cpu_msg def memory_info():
mem_free = str(os.popen("top -n 1 | sed -n '4p' | awk '{print $8}'").read()).strip()
mem_buff = str(os.popen("top -n 1 | sed -n '4p' | awk '{print $10}'").read()).strip()
mem_cached = str(os.popen("top -n 1 | sed -n '5p' | awk '{print $9}'").read()).strip()
mem_total = round(int(str(os.popen("top -n 1 | sed -n '4p' | awk '{print $4}'").read()).strip())/1024/1024,0)
mem_free_total = round((int(mem_free) + int(mem_buff) + int(mem_cached))/1024/1024,0)
mem_free_ratio = str(round(int(mem_free_total)*100/int(mem_total),2))
mem_msg = {
'总量':str(mem_total) + 'G',
'已使用':str(mem_total - mem_free_total) + 'G',
'剩余':mem_free_ratio + '%'
}
return mem_free_ratio,mem_msg def mysql_monitor(user,password,host,port):
try:
conn = pymysql.connect(host = host,port = port,user = user,passwd = password)
except:
return '主人,数据库无法正常连接了,请尽快处理!!!'
conn.close() def docker_monitor(): #disk_find = str(os.popen("df -hT | grep /data |awk 'NR==1{print}' | awk '{print $1}'").read()).strip()#读取第一行
disk_find = '/data'
while True:
disk_info_msg = disk_info(disk_find)
cpu_tup = cpu_info()
cpu_idle = cpu_tup[0]
cpu_msg = cpu_tup[1]
mem_tup = memory_info()
mem_free_ratio = mem_tup[0]
mem_msg = mem_tup[1]
mysql_info = mysql_monitor('xxx(user)','xxxx(password)','192.168.1.230',3306)
localtime = time.localtime()
if str(localtime[3]) == '11':
name = '正常'
description = '主人,我乖乖的努力工作了一天哟,犒赏犒赏!!!'
disk_send_info = '硬盘:' + str(disk_info_msg)
cpu_send_info = 'CPU:' + str(cpu_msg)
mem_send_info = '内存:' + str(mem_msg)
print(disk_send_info)
mail(name,description,disk_send_info,cpu_send_info,mem_send_info)
if mysql_info:
name = '数据库'
description = mysql_info
disk_send_info = '硬盘:' + str(disk_info_msg)
cpu_send_info = 'CPU:' + str(cpu_msg)
mem_send_info = '内存:' + str(mem_msg)
mail(name,description,disk_send_info,cpu_send_info,mem_send_info)
if int(float(disk_info_msg['使用率'][0:-1])) >= 80:
os.popen('docker stop default-vidio')
name = '硬盘'
description = '主人,我吃胀了,请给我健胃消食片!!!!'
disk_send_info = '硬盘:' + str(disk_info_msg)
cpu_send_info = 'CPU:' + str(cpu_msg)
mem_send_info = '内存:' + str(mem_msg)
mail(name,description,disk_send_info,cpu_send_info,mem_send_info)
#小数后面是.0,先转浮点数在转整数
if int(float(cpu_idle)) <= 20 :
name = 'CPU'
description = '主人,我大脑不够用了,快救救我!!!!'
disk_send_info = '硬盘:' + str(disk_info_msg)
cpu_send_info = 'CPU:' + str(cpu_msg)
mem_send_info = '内存:' + str(mem_msg)
mail(name, description, disk_send_info, cpu_send_info, mem_send_info)
if int(float(mem_free_ratio)) <=20:
name = '内存'
description = '主人,我血液不畅通,快疏通疏通!!!!'
disk_send_info = '硬盘:' + str(disk_info_msg)
cpu_send_info = 'CPU:' + str(cpu_msg)
mem_send_info = '内存:' + str(mem_msg)
mail(name, description, disk_send_info, cpu_send_info, mem_send_info)
time.sleep(3600)

python3 简单服务器监控,自动发送邮件的更多相关文章

  1. java使用JMail通过QQ邮件服务器实现自动发送邮件

    前言:项目开发的过程中,我们项目需要一个自动发送邮件提醒的小功能,于是简单的研究了一下java的JMail来实现自动发送邮件的功能.已被后期需要参考. 一.准备 实现的原理很简单:发送人 , 中转的邮 ...

  2. Python3.x:定时自动发送邮件

    定时自动发送邮件 一.简述 python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email ...

  3. [rrdtool]监控和自动画图,简单的监控.md

    现在想要监控服务的流量和并发数,可是又没那么多时间来写系统,其他的运维系统又不熟悉,于是就用现有的rrdtool shell做了个简单的监控界面,临时用下,也算是个小实验把. rrdtool也是刚接触 ...

  4. python3+request接口自动化框架中自动发送邮件

    在上一篇中的自动化框架中没有放上自动发送测试结果到邮箱的功能,在这篇文章中在补一下,哈哈 1.上一篇的代码就不在一一介绍了,本篇只介绍发送邮件的功能代码 2.在public common 文件夹中创建 ...

  5. jmeter服务器监控插件指标简单说明

    以下是下载了服务器监控插件的各个组件的功能介绍,有助于以后jmeter的性能测试 1.jp@gc - Actiive Threads Over Time:不同时间的活动用户数量展示(图表) 当前的时间 ...

  6. 1. SQL Server服务器监控实现方法

    对于服务器的监控,和对数据库的监控,很少有合二为一的工具,如果有的话,一般是付费软件,或者自行开发的工具.所以如果不想购买软件,也不想花精力去开发的话,可以结合一些免费/开源的工具.自定义脚本,来完成 ...

  7. python☞自动发送邮件

    一.SMTP 协议 SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 二.smtplib ...

  8. SQL SERVER 2008自动发送邮件(完整版)

    这两天都在搞这个东西,从开始的一点不懂,到现在自己可以独立的完成这个功能!在这个过程中,CSDN的好多牛人都给了我很大的帮助,在此表示十二分的感谢!写这篇文章,一是为了巩固一下,二嘛我也很希望我写的这 ...

  9. centos7安装Logwatch配合msmtp邮件客户端发送服务器监控分析日志

    ########################### #DATE 2016-07-29                         # #Authur by Denilas Yeung     ...

随机推荐

  1. [NWPU2016][寒假作业][正常版第三组]搜索和二分 N

    题意,一条数轴上,告诉你起点和终点,只能向前走1,向后走1,或者走到二倍的现在的位置,每次都耗时一分钟.问从起点到终点的最短时长. 简单地bfs #include <iostream> # ...

  2. Net Core微服务

    Net Core微服务 http://www.cnblogs.com/qhbm/category/1235971.html 开发工具:VS2017 .Net Core 2.1 什么是微服务? 单体结构 ...

  3. python 3 学习字符串和编码

    字符串和编码 阅读: 895464 字符编码 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字 ...

  4. java实现xml文件读取并保存到对象

    首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...

  5. java es 骤合操作

    ElasticSearch java API - 聚合查询 以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: &q ...

  6. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:4.技术简介之Spring

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Spring是一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...

  7. pc端常见布局---水平居中布局 单元素不定宽度

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. python爬虫之路——基本文件操作

    介绍python如何打开文件和读取数据 新建TXT文档,为追加模式: f=open('c;/wendang/demo.txt','a+') content="abcdefg123456789 ...

  9. Problem X: C语言习题 学生成绩输入和输出

    Problem X: C语言习题 学生成绩输入和输出 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 4722  Solved: 2284[Submit] ...

  10. CPP-基础:虚函数表

    虚函数表 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的.简称为V-Table.在这个表中,主是要一个类的虚函数的地址表 ...