写的一般,将就看。
参数的写法
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
|
if
len
(sys.argv)
=
=
3
and
sys.argv[
2
]
in
accepted_modes:
java_name
=
sys.argv[
1
]
mode
=
sys.argv[
2
]
else
:
usage()
#Check if process is running / Get PID
jproc
=
Jprocess(java_name)
pid
=
jproc.chk_proc()
if
pid !
=
""
and
mode
=
=
'all'
:
jproc.get_jstats()
# print jproc.zdict
jproc.compute_jstats()
FNULL
=
open
(os.devnull,
'w'
)
for
key
in
jproc.zdict:
#print key,jproc.zdict[key]
jproc.send_to_zabbix(key)
FNULL.close()
print
pid
else
:
print
0
|
-
生成日期格式 比如error_list20170104.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
|
# !/usr/bin/env python
#-*-coding:utf-8-*-
import
os
import
sys
import
time
import
datetime
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
tomcat_log
=
'/home/logs/'
last_position_logfile
=
'/home/back/test/last_position.txt'
#error_list_logfile = '/home/back/test/error_list.txt'
error_list_logfile1
=
'/home/back/test/error_list'
now
=
datetime.datetime.now()
strdatetime
=
now.strftime(
"%Y/%m/%d"
)
strdatetime1
=
now.strftime(
"%Y%m%d"
)
Date
=
now.strftime(
"%d"
)
error_list_logfile
=
error_list_logfile1
+
strdatetime1
+
".txt"
print
error_list_logfile
pattern
=
compile
(r
'Exception|^\t+\bat\b'
, IGNORECASE)
fp
=
open
(error_list_logfile,
"w+"
)
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
):
#parent_file = os.path.dirname(file)
error_list
=
[]
try
:
data
=
open
(
file
,
'r'
)
except
:
exit()
for
line
in
data:
if
pattern.search(line):
error_list.append(
file
)
#print error_list
#errors_list += 1
#print "%s" %file
break
data.close()
if
len
(error_list)
=
=
1
:
#return ''.join(error_list)
writeToTxt(error_list,error_list_logfile)
#return error_list
#if errors_list == 1:
#print "%s num is %d" %(file,errors_list)
#print ''.join(errors_list)
def
walk(local_dir):
for
root, dirs, files
in
os.walk(local_dir):
for
filespath
in
files:
# return filespath
local_file
=
os.path.join(root, filespath)
# print error_info2
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)
#print "Date is %s" %Date
result
=
string.find(local_dir,strdatetime)!
=
-
1
if
result:
#print local_dir.count('/')
if
local_dir.count(
'/'
)
=
=
8
:
homedir.append(local_dir)
return
homedir
if
__name__
=
=
'__main__'
:
if
not
argv1:
tomcat_last_dir
=
walk_dir(tomcat_log)
for
i
in
tomcat_last_dir:
walk(i)
|
2.带参数的写法,比如python test.py 20170103,这样生成error_list20170103.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
|
# !/usr/bin/env python
#-*-coding:utf-8-*-
import
os
import
sys
import
time
import
datetime
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
tomcat_log
=
'/home/logs/'
last_position_logfile
=
'/home/back/test/last_position.txt'
#error_list_logfile = '/home/back/test/error_list.txt'
error_list_logfile1
=
'/home/back/test/error_list'
now
=
datetime.datetime.now()
strdatetime
=
now.strftime(
"%Y/%m/%d"
)
strdatetime1
=
now.strftime(
"%Y%m%d"
)
Date
=
now.strftime(
"%d"
)
error_list_logfile
=
error_list_logfile1
+
strdatetime1
+
".txt"
print
error_list_logfile
pattern
=
compile
(r
'Exception|^\t+\bat\b'
, IGNORECASE)
fp
=
open
(error_list_logfile,
"w+"
)
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
):
#parent_file = os.path.dirname(file)
error_list
=
[]
try
:
data
=
open
(
file
,
'r'
)
except
:
exit()
for
line
in
data:
if
pattern.search(line):
error_list.append(
file
)
#print error_list
#errors_list += 1
#print "%s" %file
break
data.close()
if
len
(error_list)
=
=
1
:
#return ''.join(error_list)
writeToTxt(error_list,error_list_logfile)
#return error_list
#if errors_list == 1:
#print "%s num is %d" %(file,errors_list)
#print ''.join(errors_list)
def
walk(local_dir):
for
root, dirs, files
in
os.walk(local_dir):
for
filespath
in
files:
# return filespath
local_file
=
os.path.join(root, filespath)
# print error_info2
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)
#print "Date is %s" %Date
result
=
string.find(local_dir,strdatetime)!
=
-
1
if
result:
#print local_dir.count('/')
if
local_dir.count(
'/'
)
=
=
8
:
homedir.append(local_dir)
return
homedir
def
walk_dir_argv(home_dir,strdatetime):
homedir
=
[]
for
root, dirs, files
in
os.walk(home_dir):
for
dirname
in
dirs:
local_dir
=
os.path.join(root, dirname)
#print "Date is %s" %Date
result
=
string.find(local_dir,strdatetime)!
=
-
1
if
result:
#print local_dir.count('/')
if
local_dir.count(
'/'
)
=
=
8
:
homedir.append(local_dir)
return
homedir
if
__name__
=
=
'__main__'
:
argv1
=
sys.argv[
1
]
if
not
argv1:
tomcat_last_dir
=
walk_dir(tomcat_log)
for
i
in
tomcat_last_dir:
walk(i)
else
:
error_list_logfile
=
error_list_logfile1
+
argv1
+
".txt"
tomcat_last_dir
=
walk_dir_argv(tomcat_log,strdatetime)
for
i
in
tomcat_last_dir:
walk(i)
|
参数和变量的位置,还需要优化处理。
本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1888882,如需转载请自行联系原作者