当前位置:Gxlcms > Python > Linux发邮件磁盘空间监控python示例

Linux发邮件磁盘空间监控python示例

时间:2021-07-01 10:21:17 帮助过:55人阅读

这篇文章主要介绍了Linux发邮件磁盘空间监控功能,python实现,需要的朋友可以参考下

核心代码:

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import smtplib
  4. import os
  5. import commands,time
  6. from email.mime.text import MIMEText
  7. #from email import MIMEText
  8. disk_free=os.popen('df -lh')
  9. list_disk=disk_free.read()
  10. mailto_list=["2880329185@qq.com","2881280685@qq.com","2880089704@qq.com","2880329180@qq.com"]
  11. mail_host="smtp.139.com"
  12. mail_user="user"
  13. mail_pass="password"
  14. mail_postfix="jljgl@.com"
  15. use01=commands.getstatusoutput("df -lh|awk '{print $4}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
  16. use02=commands.getstatusoutput("df -lh|awk '{print $5}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
  17. getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
  18. gettime=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $2}'")
  19. free01=use01[1]
  20. free02=use02[1]+'\n'+use01[1]
  21. new_time=time.strftime("%Y%m%d", time.localtime())
  22. def send_mail(to_list,sub,content):
  23. me=mail_postfix
  24. msg = MIMEText(content,_subtype='plain',_charset='gb2312')
  25. msg['Subject'] = sub
  26. msg['From'] = me
  27. msg['To'] = ";".join(to_list)
  28. try:
  29. server = smtplib.SMTP()
  30. server.connect(mail_host)
  31. server.login(mail_user,mail_pass)
  32. server.sendmail(me, to_list, msg.as_string())
  33. server.close()
  34. return True
  35. except SyntaxError:
  36. pass
  37. return False
  38. if __name__ == '__main__':
  39. if free02>30:
  40. id=0
  41. if new_time>gettime[1]:
  42. while 1:
  43. getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
  44. id=id+1
  45. '''
  46. jl=open('/home/oracle/script/dd.log')
  47. record=jl.read()
  48. '''
  49. line_l=str(id)+' '+new_time
  50. df=open('/home/oracle/script/mail.log','w')
  51. df.writelines(line_l)
  52. df.close()
  53. if send_mail(mailto_list,"hello",list_disk):
  54. print "发送成功"
  55. else:
  56. print "发送失败"
  57. if getrecord[1]>0:
  58. break
  59. else:
  60. line_m=str(0)+' '+new_time
  61. free=open('/home/oracle/script/mail.log','w')
  62. free.writelines(line_m)
  63. free.close()


更多Linux发邮件磁盘空间监控python示例相关文章请关注PHP中文网!

人气教程排行