import
os,sys,tempfile,time,datetime,re
import
string
from
smtplib
import
SMTP
from
email
import
MIMEText
from
email
import
Header
from
os.path
import
getsize
from
sys
import
exit
from
re
import
compile
, IGNORECASE
now
=
datetime.datetime.now()
strdatetime1
=
now.strftime(
"%Y%m%d"
)
strdatetime
=
now.strftime(
"%Y/%m/%d"
)
tomcat_log
=
'/home/logs/'
last_position_logfile
=
'/home/back/test/last_position.txt'
error_list_logfile1
=
'/home/back/test/error_list'
Date
=
now.strftime(
"%d"
)
error_list_logfile
=
error_list_logfile1
+
strdatetime1
+
".txt"
print
error_list_logfile
errorfile
=
error_list_logfile
pattern
=
compile
(r
'Exception|^\t+\bat\b'
, IGNORECASE)
def
writeToTxt(list_name, file_path):
try
:
fp
=
open
(file_path,
"a+"
)
for
item
in
list_name:
fp.write(
str
(item)
+
"\n"
)
fp.close()
except
IOError:
print
(
"fail to open file"
)
def
analysis_log(
file
):
error_list
=
[]
try
:
data
=
open
(
file
,
'r'
)
except
:
exit()
for
line
in
data:
if
pattern.search(line):
error_list.append(
file
)
break
data.close()
if
len
(error_list)
=
=
1
:
writeToTxt(error_list,error_list_logfile)
def
walk(local_dir):
for
root, dirs, files
in
os.walk(local_dir):
for
filespath
in
files:
local_file
=
os.path.join(root, filespath)
error_info
=
analysis_log(local_file)
def
walk_dir(home_dir):
homedir
=
[]
for
root, dirs, files
in
os.walk(home_dir):
for
dirname
in
dirs:
local_dir
=
os.path.join(root, dirname)
result
=
string.find(local_dir,strdatetime)!
=
-
1
if
result:
if
local_dir.count(
'/'
)
=
=
8
:
homedir.append(local_dir)
return
homedir
def
make_ex(errorfile):
with
open
(errorfile) as f:
exfile
=
[]
exdict
=
{}
for
i
in
f.readlines():
expname
=
i.strip().split(
'/'
)[
3
]
exname
=
i.strip().split(
'/'
)[
8
]
estr
=
i.strip()
youpattern
=
re.
compile
(
'/(\w+)/(\w+)/(\w+)'
)
result
=
youpattern.match(estr)
r1
=
result.group()
r2
=
"ex/"
+
strdatetime1
r3
=
exname
rall
=
[r1, r2, r3]
rexall
=
"/"
.join(rall)
+
'/'
if
not
os.path.exists(rexall):
os.makedirs(rexall)
os.system(
'cp -r %s %s'
%
(estr, rexall))
def
make_diff_ex(c):
for
j
in
c:
i
=
str
(j)
expname
=
i.strip().split(
'/'
)[
3
]
exname
=
i.strip().split(
'/'
)[
8
]
estr
=
i.strip()
youpattern
=
re.
compile
(
'/(\w+)/(\w+)/(\w+)'
)
result
=
youpattern.match(estr)
if
result:
r1
=
result.group()
r2
=
"ex/"
+
strdatetime1
r3
=
exname
rall
=
[r1, r2, r3]
rexall
=
"/"
.join(rall)
+
'/'
if
not
os.path.exists(rexall):
os.makedirs(rexall)
os.system(
'cp -r %s %s'
%
(estr, rexall))
def
temps(errorfile):
tmpfile
=
[]
with
open
(errorfile,
'r'
) as f:
tmpfile.append(f.readlines())
return
tmpfile
def
list_diff(a, b):
ret
=
[]
for
i
in
a:
if
i
not
in
b:
ret.append(i)
return
ret
if
__name__
=
=
'__main__'
:
if
not
os.path.exists(errorfile):
fp
=
open
(error_list_logfile,
"w+"
)
tomcat_last_dir
=
walk_dir(tomcat_log)
for
i
in
tomcat_last_dir:
walk(i)
make_ex(errorfile)
else
:
a
=
temps(errorfile)
fp
=
open
(error_list_logfile,
"w+"
)
tomcat_last_dir
=
walk_dir(tomcat_log)
for
i
in
tomcat_last_dir:
walk(i)
b
=
temps(errorfile)
c
=
list_diff(a,b)
if
c:
make_diff_ex(c)