Linux 發(fā)郵件磁盤空間監(jiān)控(python)
更新時間:2016年04月23日 10:55:13 投稿:mdxy-dxy
這篇文章主要介紹了Linux發(fā)郵件磁盤空間監(jiān)控功能,python實現(xiàn),需要的朋友可以參考下
核心代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import os
import commands,time
from email.mime.text import MIMEText
#from email import MIMEText
disk_free=os.popen('df -lh')
list_disk=disk_free.read()
mailto_list=["2880329185@qq.com","2881280685@qq.com","2880089704@qq.com","2880329180@qq.com"]
mail_host="smtp.139.com"
mail_user="user"
mail_pass="password"
mail_postfix="jljgl@.com"
use01=commands.getstatusoutput("df -lh|awk '{print $4}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
use02=commands.getstatusoutput("df -lh|awk '{print $5}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
gettime=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $2}'")
free01=use01[1]
free02=use02[1]+'\n'+use01[1]
new_time=time.strftime("%Y%m%d", time.localtime())
def send_mail(to_list,sub,content):
me=mail_postfix
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except SyntaxError:
pass
return False
if __name__ == '__main__':
if free02>30:
id=0
if new_time>gettime[1]:
while 1:
getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
id=id+1
'''
jl=open('/home/oracle/script/dd.log')
record=jl.read()
'''
line_l=str(id)+' '+new_time
df=open('/home/oracle/script/mail.log','w')
df.writelines(line_l)
df.close()
if send_mail(mailto_list,"hello",list_disk):
print "發(fā)送成功"
else:
print "發(fā)送失敗"
if getrecord[1]>0:
break
else:
line_m=str(0)+' '+new_time
free=open('/home/oracle/script/mail.log','w')
free.writelines(line_m)
free.close()
相關(guān)文章
python自動化測試之從命令行運行測試用例with verbosity
這篇文章主要介紹了python自動化測試之從命令行運行測試用例with verbosity,是一個較為經(jīng)典的自動化測試實例,需要的朋友可以參考下2014-09-09
詳解在python中如何使用zlib模塊進行數(shù)據(jù)壓縮和解壓縮
Python有一些內(nèi)置庫用于處理數(shù)據(jù)壓縮和解壓縮,其中一個就是zlib模塊,這個模塊為DEFLATE壓縮算法和相關(guān)的gzip(文件格式)提供了支持,在這篇文章中,我們將深入探討如何使用zlib模塊進行數(shù)據(jù)壓縮和解壓縮2023-06-06
Python實現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運算示例
這篇文章主要介紹了Python實現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運算,結(jié)合實例形式分析了Python針對矩陣進行轉(zhuǎn)置與相乘運算的相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2019-03-03

