forked from honglongwei/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_log.py
50 lines (44 loc) · 1.4 KB
/
monitor_log.py
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import time
import datetime
def GetLog(num):
fls = []
for dirpath, dirnames, filenames in os.walk('/opt/logbak'):
for filename in filenames:
fn = os.path.join(dirpath, filename)
now_time = datetime.datetime.now()
last_time = now_time + datetime.timedelta(days=-num)
tg_time = last_time.strftime('%Y-%m-%d')
endf = tg_time + '.tgz'
if filename.startswith('xxx_') and filename.endswith(endf):
if os.path.exists(fn):
fls.append(fn)
else:
pass
else:
pass
xlog = []
for a in fls:
xlog.append(a[13:17])
xlog.sort()
return xlog
blog = GetLog(2)
alog = GetLog(1)
'''
交集:list(set(a).intersection(set(b)))
并集:list(set(a).union(set(b)))
差集:list(set(b).difference(set(a))) // b中有而a中没有的
'''
jdata = set(blog).intersection(set(alog)) #交集
mor = list(set(alog).difference(jdata)) #新增
les = list(set(blog).difference(jdata)) #减少
if len(mor) == 0 and len(les) == 0:
print 0
elif len(mor) == 0 and len(les) != 0:
print '-: {0}'.format(','.join(les))
elif len(mor) != 0 and len(les) == 0:
print '+: {0}'.format(','.join(mor))
else:
print '+: {jia}\n-: {shao}'.format(jia=','.join(mor), shao=','.join(les))