python脚本--yagmail邮件发送
#!/usr/bin/env python# -*- coding: UTF-8 -*-
import os
import socket
import psutil
import datetime
import time
import re
import subprocess
import yagmail
def net_info():
p = os.popen("hostname -I")
p.read()
res1 = net_info()
def host_name():
HostName = socket.gethostname()
return HostName
res2 = host_name()
def local_time():
local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return local_time
res3 = local_time()
def cpu_info():
cpu = psutil.cpu_percent(1)
return cpu
res4 = cpu_info()
def mem_info():
mem = psutil.virtual_memory()
info1 = {'mem_total': mem[0], 'mem_free': mem[1], 'mem_percent': mem[2], 'mem_used': mem[3]}
return info1
res5 = mem_info()
def disk_info():
disk = psutil.disk_usage('/')
info2 = {'total': disk[0], 'used': disk[1], 'free': disk[2], 'percent': disk[3]} # 同样写入一个字典
return info2
res6 = disk_info()
#def main():
m_ip = res1
m_host_name = res2
m_local_time = res3
m_cpu_info = res4
m_mem_info = res5
m_disk_info = res6
msg = '''
ip地址:%s
主机名:%s
触发时间:%s
cpu使用率:%s
内存总量:%sM
内存剩余:%sM
内存使用率:%s
内存使用量:%sM
磁盘总量:%sGB
磁盘使用量:%sGB
磁盘剩余量:%sGB
磁盘使用率:%s%%
''' % (m_ip, str(m_host_name), str(m_local_time), m_cpu_info, int(m_mem_info.get('mem_total') / 1024 / 1024),
int(m_mem_info['mem_free'] / 1024 / 1024), m_mem_info['mem_percent'],
int(m_mem_info['mem_used'] / 1024 / 1024), int(m_disk_info['total'] / 1024 / 1024 / 1024),
int(m_disk_info['used'] / 1024 / 1024 / 1024), int(m_disk_info['free'] / 1024 / 1024 / 1024),
m_disk_info['percent'])
#print(msg)#res7 = main()
#第三方邮件配置
yag = yagmail.SMTP( user="邮箱地址", password="邮箱的独立密码", host='smtp.qq.com')
#contents = res7
yag.send('493630393@qq.com','服务器告警',msg)
以上是 python脚本--yagmail邮件发送 的全部内容, 来源链接: utcz.com/z/388344.html