python系统监控及邮件发送

                                  python系统监控及邮件发送

 
#psutil模块是一个跨平台库,能轻松实现获取系统运行的进程和系统利用率
 
import psutil                                    #先导入psutil模块
 res = psutil.cpu_percent(1)              #1s内cpu的使用率
 print(res)
 
 res1 = psutil.cpu_percent(1,True)    #每一秒cpu的占用率
 print(res1)
 
 res2 = psutil.virtual_memory()          #内存使用率
 print(res2)
 print( '%.2f%%' %res2[2])              #以百分比形式打印第三位
 
 res4 = psutil.disk_usage('C:')            #硬盘分区的使用率
 print(res4)
 
 res5 = psutil.net_io_counters()       #查看网络流量
 print(res5)
 

监控程序

#导入模块(获取系统信息和邮件模块)
import psutil        
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 

#cpu模块

def cpu_info():
    cpu = '%.2f%%' %psutil.cpu_percent(1)    #把cpu的值改成百分比的形式
    return cpu
 
#内存模块
def mem_info():
    mem = psutil.virtual_memory()
    mem_per = '%.2f%%' %mem[2]                 #同上
    mem_total = str(int(mem[0]/1024/1024)) + 'M'   
    mem_used = str(int(mem[3]/1024/1024)) + 'M'
    info = {
        'mem_per' : mem_per,
        'mem_total' : mem_total,
        'mem_used' : mem_used
    }
    return info
 

#磁盘分区模块

def disk_info():
    c_per = '%.2f%%' %psutil.disk_usage('C:')[3]
    d_per = '%.2f%%' %psutil.disk_usage('D:')[3]
    info = {
        'c_per' : c_per,
        'd_per' : d_per
    }
    return info
 

#网卡模块

def network_info():
    network = psutil.net_io_counters()
    network_sent = str(int(network[0]/8/1024)) + 'kb'
    network_recv = str(int(network[1]/8/1024)) + 'kb'
    info = {
        'network_sent' : network_sent,
        'network_recv' : network_recv
    }
    return info
 

#邮件发送模块

def send_mail(message):
    sender = ' [email protected]'    #发送的邮箱账号
    receiver = [' [email protected]']           #接收的邮箱账号
    subject = '报警'                                       #主题
    username = ' [email protected]'   #发送的邮箱账号
    password = 'password'                              #发送的邮箱密码
    msg = MIMEText(message, 'plain', 'utf-8')    #(邮件正文,编码,编码)
    msg['Subject'] = Header(subject, 'utf-8')          #抬头为subject
    msg['From'] = 'TOP< [email protected]>'  #TOP标题 后面是邮箱
    msg['To'] = "' [email protected]'"                        #接收的邮箱
    smtp = smtplib.SMTP()
    smtp.connect(' smtp.163.com')                              #邮箱服务器
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
 

#主函数

def main():
    cpu = cpu_info()
    mem = mem_info()
    disk = disk_info()
    network = network_info()
    msg = '''
    cpu使用率:%s
    ==================
    内存占用率:%s
    内存总量:%s
    内存使用量:%s
    ===================
    C盘使用率:%s
    D盘使用率:%s
    ====================
    网卡发送量:%s
    网卡接收量:%s
    '''  % (cpu, mem.get('mem_per'), mem.get('mem_total'), mem.get('mem_used'), disk.get('c_per'), disk.get('d_per'), network.get('network_sent'), network.get('network_recv'))
    if int(cpu[:2]) > 80 or int(mem.get('mem_per')[:2]) > 80:   #报警发送条件
        send_mail(msg)
    else :
        print('没到阈值。')
if __name__ == '__main__':
    main()

猜你喜欢

转载自www.cnblogs.com/heiguu/p/10011976.html