用函數實現!~~小程序
實現: cpu 使用率大於百分之50 時 , C 盤容量不足5 G 時, 內存 低於2G 時。 出現以上其中一種狀況,發送自動報警郵件!函數
主要運用 到了兩個 模塊 yagmail 與 psutil (沒有的須要下載 pip 或者導入環境變量)spa
廢話很少說 ip
源代碼以下:內存
import yagmail
def sendmail(subject,contents):
yag = yagmail.SMTP(user='xxxxxxx@qq.com',password='xxxxxxxxxx',host='xxxx.xxx.com')
yag.send(to='xxxxxxxx@qq.com',subject=subject, contents=contents)
yag.close()
import psutil
def mem () :
mem = psutil.virtual_memory()
free_mem = int(mem[4]/1024/1024/1024)
return (free_mem)
def cpu () :
cpu1 = psutil.cpu_percent(1)
return (cpu1)
def disk () :
disk = psutil.disk_usage(r'c:')
free_disk = int(disk[2]/1024/1024/1024)
return (free_disk)
def main ():
mem1 = mem()
cpu1 = cpu()
disk1 = disk()
msg1 = '''
cpu使用率 : %s%%
內存剩餘量 : %s G
C盤容量剩餘量 : %s G
'''%(cpu1,mem1,disk1)
if cpu1 > 50:
print('cpu太高')
sendmail('cpu報警',msg1)
elif mem1 < 2 :
print('內存剩餘量不足!')
sendmail('內存報警,可用量不足',msg1)
elif disk1 < 5 :
print('c盤容量過少')
sendmail('C盤可用容量不足',msg1)
else:
print('您的電腦一切正常')
if __name__ == '__main__' :
main()