公司产品线有一个公用的挂载盘,主要是用来方便各位开发人员去放置他们自己的一些工作材料,比如异常的日志或者tcpdump的抓包等等杂七杂八的东西,但是这个挂载盘由于使用人众多,容量自然要有监控,于是就有了写这个脚本的动机。
在这里我写了两个脚本,上面这个是用来监控磁盘容量,然后通过#df -h的排序生成前十名占容量最大的文件夹,把这个文件夹的名字和对应的大小重定向到一个叫alarm.txt这个文件里,这个文件就是邮件正文。然后在确定他们的主人,统一加上公司邮箱后缀来得到他们主人的邮箱地址,最后对应他们各自的邮箱地址用下面那个脚本来发送文件夹容量过高的邮件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/usr/bin/env python
# coding=utf-8
import
os
import
AutoMail
import
commands
#设定变量判断是否挂载和挂载盘的容量
mount
=
commands.getoutput(
"mount | grep ':.*nfs'|wc -l"
)
size
=
commands.getoutput(
"df -h | grep share | awk '{print $5}' | cut -d '%' -f 1"
)
##建立发邮件的文本文件
def
Createalarm():
if
os.path.exists(
'/root/chenscript/alarm.txt'
)
=
=
True
:
os.system(
"python /root/chenscript/weixin_sharealarm.py"
)
print
(
"微信告警已经发送!"
)
os.system("cd
/
root
/
chenscript; echo 'share盘容量大于
80
%
,现在将调出容量排名前十位的文件
夹名字及对应的容量,请各位处理一下不需要的文件!' >
/
root
/
chenscript
/
alarm.txt")
os.system(
"cd /挂载盘名称 ;du -s * --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹'|sort -nr |head >>/root/chenscript/alarm.txt"
)
os.system(
"echo '\n' >> /root/chenscript/alarm.txt"
)
if
os.path.exists(
'/root/chenscript/alarm.txt'
)
=
=
False
:
os.system(
"cd /root/chenscript;touch alarm.txt"
)
def
Sendmail():
fp
=
open
(
'/root/chenscript/alarm.txt'
,
'r'
)
content
=
fp.read()
AutoMail.send_mail(
'share挂载盘容量大于80%!收到邮件的各位请整理自己对应的文件夹!'
, content)
#将邮件的文件刷新
def
Dellist():
os.system(
"cd /root/chenscript/;rm -f alarm.txt;touch alarm.txt"
)
if
mount
=
=
'1'
and
size >
=
'80'
:
print
(
"挂载盘存在!"
)
print
(
"share盘容量大于80%..."
)
Createlist()
Sendmail()
Dellist()
elif
mount
=
=
'1'
and
size <
'80'
:
print
(
"挂载盘存在!"
)
print
(
"share盘容量正常..."
)
else
:
print
(
"挂载盘不存在,现在重新挂载..."
)
os.system(
"mount -t nfs -o acl,rw,intr,soft,nolock,rsize=8192,wsize=8192 10.160.43.172:/share /share "
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#!/usr/bin/env python
#coding=utf-8
#这个脚本的用途是用来发送邮件
import
smtplib
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
from
email.mime.application
import
MIMEApplication
mailto_list
=
[]
#这里为空list,会从list.txt里一行一行的当做元素添加进来
#生成list.txt
if
os.path.exists(
'/root/chenscript/list.txt'
)
=
=
True
:
os.system(
"cd /挂载盘名称;du -s * --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹'|sort -nr |head|awk \'{print $2\"@dahuatech.com\"}\' >>/root/chenscript/list.txt"
)
if
os.path.exists(
'/root/chenscript/list.txt'
)
=
=
False
:
os.system(
"cd /root/chenscript/;rm -f list.txt;echo '本人的邮箱地址'>list.txt"
)
with
open
(
'/root/chenscript/list.txt'
,
'r'
) as f:
f
=
f.readlines()
for
i
in
f:
i
=
i.strip(
'\n'
)
mailto_list.append(i)
mail_host
=
"这里填写邮箱主机"
mail_user
=
"这里填写发送人的邮箱地址"
mail_pass
=
"发送人的邮箱密码"
mail_postfix
=
"dahuatech.com"
mail_sender
=
"与mail_host内容相同"
def
send_mail(sub, content):
me
=
mail_sender
msg
=
MIMEMultipart()
msg[
'Subject'
]
=
sub
msg[
'From'
]
=
me
msg[
'To'
]
=
";"
.join(mailto_list)
content1
=
MIMEText(
str
(content),
'plain'
,
'utf-8'
)
msg.attach(content1)
try
:
s
=
smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
print
(
'发送成功!\n'
)
s.close()
except
Exception as e:
print
(
str
(e))
os.system(
"cd /root/chenscript/;rm -f list.txt;echo '我本人的邮件地址'>list.txt"
)
|
执行的效果如下:
隐藏的知识点!
1)#du -s 是按照字节来统计,“--exclude='yunwei'”是在排序的时候忽略掉yunwei这个文件夹,容后再用#sort -nr|head是得到从大到小前10名,如果得到后10名就是#sort -nr|tail;
2)如果使用的是import commands,那么commands.getoutput得到的是字符串!
3)用#mount | grep ':.*nfs'来判断挂载盘是否存在是一个很简单的方式,如果挂了多个,就用ip in的方式来进一步判断;
4)python要一行一行的读取文件,就readline;
5)python按行读取文件,去掉换行符"\n"的方法:
1
2
|
for
line
in
file
.readlines():
line
=
line.strip(
'\n'
)
|
6)import Automail的时候,就已经把Automail.py这个脚本固定住了,这时候mailto_list已经不能变化了,所以要把添加list.txt放到这个脚本里。
发了邮件,连吼带骂一顿,终于把share盘容量下降到了69这样一个美妙的数字...
最后的最后,如果您觉得本文对您升职加薪有帮助,那么请不吝赞助之手,刷一下下面的二维码,赞助本人继续写更多的博文!