Linux 发邮件磁盘空间监控(python)
作者:mdxy-dxy 时间:2022-03-15 09:17:24
核心代码:
#!/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 "发送成功"
else:
print "发送失败"
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()
标签:python,磁盘空间监控
0
投稿
猜你喜欢
Python实现去除列表中重复元素的方法总结【7种方法】
2021-10-08 00:24:16
mysql中explain用法详解
2024-01-13 16:28:25
在Python中处理日期和时间的基本知识点整理汇总
2021-05-13 07:12:14
vue router 动态路由清除方式
2024-05-08 10:12:22
基于Python制作一个文件解压缩工具
2021-01-10 08:00:13
Mysql5.7服务无法启动的图文解决教程
2024-01-29 03:44:39
多个域名后缀同时查询的域名查询系统代码
2008-05-20 11:53:00
asp如何读取注册表的信息?
2009-11-19 21:18:00
在thinkphp5.0路径中实现去除index.php的方式
2024-05-11 09:54:34
Python随机函数库random的使用方法详解
2021-06-07 16:16:23
MySQL如何解决DOS窗口乱码问题
2024-01-22 11:44:56
如何使用ASP来读写注册表
2007-09-20 13:08:00
vue递归获取父元素的元素实例
2024-05-05 09:24:34
PHP抓取及分析网页的方法详解
2023-11-24 08:17:10
七种Python代码审查工具推荐
2021-06-16 07:07:34
对fieldset和legend的默认样式一点分析
2008-07-02 12:56:00
django之跨表查询及添加记录的示例代码
2022-11-23 14:44:25
SQL Server数据库触发器安全隐患解析
2009-03-25 12:56:00
如何用Python合并lmdb文件
2023-08-05 17:42:01
记一次MySQL的优化案例
2024-01-14 21:32:58