一个监控挂载盘的python脚本

释放双眼,带上耳机,听听看~!

公司产品线有一个公用的挂载盘,主要是用来方便各位开发人员去放置他们自己的一些工作材料,比如异常的日志或者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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
1#!/usr/bin/env python
2
3
4
5# coding=utf-8
6
7
8

10
11
12
13
14import os
15
16
17
18
19
20import AutoMail
21
22
23
24
25
26import commands
27
28
29
30
31 
32
33
34
35#设定变量判断是否挂载和挂载盘的容量
36
37
38
39
40mount = commands.getoutput("mount | grep ':.*nfs'|wc -l")
41
42
43
44
45
46size = commands.getoutput("df -h | grep share | awk '{print $5}' | cut -d '%' -f 1")
47
48
49
50
51 
52
53
54
55##建立发邮件的文本文件
56
57
58
59
60def Createalarm():
61
62
63
64
65
66    if os.path.exists('/root/chenscript/alarm.txt') == True:
67
68
69
70
71
72        os.system("python /root/chenscript/weixin_sharealarm.py")
73
74
75
76
77
78        print ("微信告警已经发送!")
79
80
81
82
83
84        os.system("cd /root/chenscript; echo 'share盘容量大于80%,现在将调出容量排名前十位的文件
85
86
87
88
89
90夹名字及对应的容量,请各位处理一下不需要的文件!' >/root/chenscript/alarm.txt")
91
92
93
94
95
96        os.system("cd /挂载盘名称 ;du -s * --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹'|sort -nr |head >>/root/chenscript/alarm.txt")
97
98
99
100
101
102        os.system("echo '\n' >> /root/chenscript/alarm.txt")
103
104
105
106
107 
108
109
110
111
112    if os.path.exists('/root/chenscript/alarm.txt') == False:
113
114
115
116
117
118        os.system("cd /root/chenscript;touch alarm.txt")
119
120
121
122
123 
124
125
126
127
128def Sendmail():
129
130
131
132
133
134        fp = open('/root/chenscript/alarm.txt', 'r')
135
136
137
138
139
140        content = fp.read()
141
142
143
144
145
146        AutoMail.send_mail('share挂载盘容量大于80%!收到邮件的各位请整理自己对应的文件夹!', content)
147
148
149
150
151 
152
153
154
155#将邮件的文件刷新
156
157
158
159
160def Dellist():
161
162
163
164
165
166        os.system("cd /root/chenscript/;rm -f alarm.txt;touch alarm.txt")
167
168
169
170
171 
172
173
174
175
176if mount == '1' and size >= '80':
177
178
179
180
181
182        print ("挂载盘存在!")
183
184
185
186
187
188        print ("share盘容量大于80%...")
189
190
191
192
193
194        Createlist()
195
196
197
198
199
200        Sendmail()
201
202
203
204
205
206        Dellist()
207
208
209
210
211
212elif mount == '1' and size < '80':
213
214
215
216
217
218        print ("挂载盘存在!")
219
220
221
222
223
224        print ("share盘容量正常...")
225
226
227
228
229
230else:
231
232
233
234
235
236        print ("挂载盘不存在,现在重新挂载...")
237
238
239
240
241
242        os.system("mount -t nfs -o acl,rw,intr,soft,nolock,rsize=8192,wsize=8192 10.160.43.172:/share /share ")
243

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
1#!/usr/bin/env python
2
3
4
5#coding=utf-8
6
7
8
9#这个脚本的用途是用来发送邮件
10
11
12
13
14import smtplib
15
16
17
18
19
20from email.mime.multipart import MIMEMultipart
21
22
23
24
25
26from email.mime.text import MIMEText
27
28
29
30
31
32from email.mime.application import MIMEApplication
33
34
35
36
37 
38
39
40
41
42mailto_list=[]    #这里为空list,会从list.txt里一行一行的当做元素添加进来
43
44
45
46
47 
48
49
50
51#生成list.txt
52
53
54
55
56if os.path.exists('/root/chenscript/list.txt') == True:
57
58
59
60
61
62        os.system("cd /挂载盘名称;du -s * --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹' --exclude='不想要计算在内的文件夹'|sort -nr |head|awk \'{print $2\"@dahuatech.com\"}\'  >>/root/chenscript/list.txt")
63
64
65
66
67
68if os.path.exists('/root/chenscript/list.txt') == False:
69
70
71
72
73
74        os.system("cd /root/chenscript/;rm -f list.txt;echo '本人的邮箱地址'>list.txt")
75
76
77
78
79 
80
81
82
83
84with open('/root/chenscript/list.txt','r') as f:
85
86
87
88
89
90    f=f.readlines()
91
92
93
94
95
96for i in f:
97
98
99
100
101
102        i=i.strip('\n')
103
104
105
106
107
108        mailto_list.append(i)
109
110
111
112
113
114mail_host="这里填写邮箱主机"
115
116
117
118
119
120mail_user="这里填写发送人的邮箱地址"
121
122
123
124
125
126mail_pass="发送人的邮箱密码"
127
128
129
130
131
132mail_postfix="dahuatech.com"
133
134
135
136
137
138mail_sender="与mail_host内容相同"
139
140
141
142
143
144def send_mail(sub, content):
145
146
147
148
149
150    me=mail_sender
151
152
153
154
155
156    msg = MIMEMultipart()
157
158
159
160
161
162    msg['Subject'] = sub
163
164
165
166
167
168    msg['From'] = me
169
170
171
172
173
174    msg['To'] = ";".join(mailto_list)
175
176
177
178
179
180    content1 = MIMEText(str(content), 'plain', 'utf-8')
181
182
183
184
185
186    msg.attach(content1)
187
188
189
190
191
192    try:
193
194
195
196
197
198        s = smtplib.SMTP()
199
200
201
202
203
204        s.connect(mail_host)
205
206
207
208
209
210        s.login(mail_user,mail_pass)
211
212
213
214
215
216        s.sendmail(me, mailto_list, msg.as_string())
217
218
219
220
221
222        print('发送成功!\n')
223
224
225
226
227
228        s.close()
229
230
231
232
233
234    except Exception as e: print(str(e))
235
236
237
238
239 
240
241
242
243
244os.system("cd /root/chenscript/;rm -f list.txt;echo '我本人的邮件地址'>list.txt")
245

执行的效果如下:

隐藏的知识点!

1)
**#du -s **是按照字节来统计,“–exclude='yunwei'”是在排序的时候忽略掉yunwei这个文件夹,容后再用
#s****ort -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
3
4
5
6
7
8
1for line in file.readlines():
2
3
4
5
6
7    line=line.strip('\n')
8

6)import Automail的时候,就已经把Automail.py这个脚本固定住了,这时候mailto_list已经不能变化了,所以要把添加list.txt放到这个脚本里。

发了邮件,连吼带骂一顿,终于把share盘容量下降到了69这样一个美妙的数字…

最后的最后,如果您觉得本文对您升职加薪有帮助,那么请不吝赞助之手,刷一下下面的二维码,赞助本人继续写更多的博文!

 本文转自 苏幕遮618 51CTO博客,原文链接:http://blog.51cto.com/chenx1242/1965358

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++迭代器

2022-1-11 12:36:11

安全经验

网站制作需要素材的实用网站

2021-10-11 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索