Python

Python 檢查硬碟空間並寄信通知

!/usr/bin/python

-coding:utf-8-

import os
import statvfs
import smtplib

vfs=os.statvfs(“/home”)
total = vfs.f_bsize * vfs.f_blocks / 1024
used = vfs.f_bsize * (vfs.f_blocks – vfs.f_bfree) / 1024
avail = vfs.f_bsize * vfs.f_bavail / 1024
used_percent = round(float(used) / float(used + avail) * 100, 2)

if used_percent > 85:
print(“disk usage warning”)
smtp=smtplib.SMTP(“smtp.gmail.com”,587)
smtp.ehlo()
smtp.starttls()
smtp.login(“testuser”,”1234″)
from_addr=”fromuser@gmail.com”
to_addr=”touserg@usiglobal.com”
msg=”Subject: blog.mm520.com disk usage analysis\n/blog.mm520.com home disk useage {}%”.format(used_percent)
status=smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()