呵呵,GOOGLE上东拼西凑的东东。找了十来个综合,裁剪成一个。
没有风格,不正规,无注释,只是简单可以运行。PYTHON2.4以上通上。
代码:
1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 4 import os, socket, statvfs 5 from time import localtime, strftime 6 from smtplib import SMTP 7 8 smtp = SMTP() 9 #smtp.set_debuglevel(debuglevel) 10 smtp.connect('1.2.3.4', 25) 11 smtp.login('user', 'pass') 12 13 from_addr = "alert<alert@freedisk.com>" 14 to_addr = ["ag@163.com","ae@163.com","af@163.com"] 15 16 # Limit in MiB 17 LIMIT=200000; 18 19 #Define current Time as Fri, 04 Mar 2011 08:47:10 20 TIME = strftime("%d %b %Y", localtime()) 21 22 MONITOR = os.statvfs("/") 23 SPACE_LEFT=(MONITOR.f_bavail * MONITOR.f_frsize) / 1048576 24 SPACE_TOTAL = (MONITOR.f_blocks * MONITOR.f_frsize) / 1048576 25 26 if (SPACE_LEFT < LIMIT): 27 STATUS = "Free Disk Space is ALERT!!!ALERT!!!ALERT!!! " 28 SPACE_LEFT=SPACE_LEFT / 1024 #GiB 29 print ( TIME+"\n"+STATUS+"\n"+str(SPACE_LEFT)+" GB left on disk\n"+"Alert Space is:"+str((LIMIT/1024))+"GB") 30 subj = "ALERT!!!ALERT!!!ALERT!!!["+str(SPACE_LEFT)+" GB left]Free disk space on www.163.com "+TIME 31 message_text = TIME+"\n"+STATUS+"\n"+str(SPACE_LEFT)+" GB left on disk\n"+"Alert Space is:"+str((LIMIT/1024))+"GB" 32 msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, TIME, message_text ) 33 smtp.sendmail(from_addr, to_addr, msg) 34 smtp.sendmail(from_addr, to_addr, msg) 35 smtp.sendmail(from_addr, to_addr, msg) 36 smtp.quit() 37 print ('Done') 38 39 elif (SPACE_LEFT > LIMIT): 40 STATUS = "Free Disk Space is OK on " 41 SPACE_LEFT=SPACE_LEFT / 1024 #GiB 42 SPACE_TOTAL=SPACE_TOTAL / 1024 #GB 43 print ( TIME+"\n"+STATUS+"\n"+str(SPACE_LEFT)+" GB left on disk("+str(SPACE_TOTAL)+"GB)\n"+"Alert Space is:"+str((LIMIT/1024))+"GB") 44 subj = "["+str(SPACE_LEFT)+" GB left]Free disk space on www.163.com "+TIME 45 message_text = TIME+"\n"+STATUS+"\n"+"Total space is :"+str(SPACE_TOTAL)+"GB\n"+str(SPACE_LEFT)+" GB left on disk\n"+"Alert Space is:"+str((LIMIT/1024))+"GB" 46 msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, TIME, message_text ) 47 smtp.sendmail(from_addr, to_addr, msg) 48 smtp.quit() 49 print ('Done!') 50 51 else: 52 print (""" 53 A serius problem detected with the script. 54 Please check what mount points you monitor and check that they're in the MON_ARRAY aswell. 55 """ )
然后,将其放入/etc/spool/cron/相关用户文件里。
0 1 * * * python /root/diskspace/freedisk.py
测试截图:
时间: 2024-11-05 14:52:26