python monitor CPU / memory / disk, more than a specified percentage, send e-mail

# ! / Usr / bin / Python 
# Coding: UTF. 8- 

# Import module psutil 
Import psutil
 Import yagmail 

DEF mail (Subject, Contents):       # parameter and contents of the mail header 
    YAG = yagmail.SMTP (= User ' zhangweihappy8 @ 163.com ' , password = ' xxx ' , Host = ' smtp.163.com ' )   # passwd login password is not authorized password 
    # send mail 
    yag.send (to = ' [email protected] ' , Subject = Subject , = Contents Contents)
     # disconnect 
    yag.close ()
  


def cpu_info():
    cpu = psutil.cpu_percent(1)
    return cpu
res=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
res2=mem_info()

def disk_info():
    disk = psutil.disk_usage(' / ' ) 
    INF02 = { ' Total ' : Disk [0], ' Used ' : Disk [. 1], ' Free ' : Disk [2], ' Percent ' : Disk [. 3]}    # same dictionary writes a 
    return INF02 
RES3 = are disk_info () 

DEF main (): 
    m_cpu = RES 
    m_mem = RES2 
    m_disk = RES3     # the result of each call as a function of partition function body input 
    MSG = '' '           
    usage% s cpu 
    amount of memory% sM      
    Remaining memory sM%
    % S memory usage 
    memory usage% sM 
    total disk% sGB 
    disk usage% sGB 
    disk% sGB remaining amount 
    disk usage %%% s 
    ' '' % (m_cpu, int (m_mem.get ( ' mem_total ' ) / 1024/1024), int (m_mem [ ' mem_free ' ] / 1024/1024), m_mem [ ' mem_percent ' ], int (m_mem [ ' mem_used ' ] / 1024/1024), int (m_disk [ ' Total ' ] / 1024 / 1024/1024), int (m_disk [ ' Used ' ] / 1024/1024/1024), int (m_disk [ ' Free ' ] / 1024/1024/1024), m_disk [ 'percent']) 
     Print (MSG)
     IF m_cpu> 50 : 
        mail ( ' CPU alarm ' , MSG)
     the else :
         Print ( ' CPU normal ' )
     IF m_mem [ ' mem_percent ' ]> 50 : 
        mail ( ' memory footprint than half ' , MSG)
     the else :
         Print ( ' Relax ' )
     IF m_disk [ ' Percent ' ]> 50 : 
        mail (' Disk is getting blown up ' , msg)
     the else :
         Print ( ' disk normal ' ) 

IF  __name__ == ' __main__ ' : 
    main ()

 

Guess you like

Origin www.cnblogs.com/hello-wei/p/11671506.html