脚本位置
城建大厦服务器
/root/logana.sh
https://coding.net/u/himan/p/script/git/raw/master/loganalys.py
脚本作用
logana.sh
搜索7天内的日志文件,调用loganalys.py文件进行错误日志过滤
loganalys.py
过滤日志文件将带有ERROR|Error|Exception|EXCEPTION这些字段的日志行输出到result.txt文本中。
使用方法
登陆城建大厦服务器
执行
sh /root/logana.sh
日志文件详细内容
vim /root/logana.sh
newbatchjob="/rizhi/web_batchjob_new/batchjob/"
batchjob="/rizhi/web_batchjob/batchjob/"
back="/rizhi/web_pc_back/web_back2/"
baobiao="/rizhi/service_pcbaobiao/pcbaobiao/"
rm -rf /tmp/logana_temp/*
mkdir -p /tmp/logana_temp${newbatchjob} /tmp/logana_temp${batchjob} /tmp/logana_temp${back} /tmp/logana_temp${baobiao}
find ${newbatchjob} -type f -mtime -7 -exec cp {} /tmp/logana_temp${newbatchjob} \; &
find ${batchjob} -type f -mtime -7 -exec cp {} /tmp/logana_temp${batchjob} \; &
find ${back} -type f -mtime -7 -exec cp {} /tmp/logana_temp${back} \; &
find ${baobiao} -type f -mtime -7 -exec cp {} /tmp/logana_temp${baobiao} \; && find /tmp/logana_temp${baobiao} | xargs gzip -d &
wait
cd /tmp/logana_temp${newbatchjob} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_newbatchjob.txt &
cd /tmp/logana_temp${batchjob} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_batchjob.txt &
cd /tmp/logana_temp${back} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_back.txt &
cd /tmp/logana_temp${baobiao} ; curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py | python3 && cp result.txt /tmp/logana_temp/result_baobiao.txt &
wait
echo "All Done!"
ls -l /tmp/logana_temp/*.txt
curl -sSL https://coding.net/u/himan/p/script/git/raw/master/loganalys.py
import re, os, sys
final = []
pwd = os.getcwd()
def listfilename():
cwd = os.getcwd()
list = os.listdir(cwd)
for i in list:
list2 = os.path.join(cwd, i)
if os.path.isdir(list2):
os.chdir(list2)
listfilename()
else:
final.append(list2)
if len(sys.argv) != 1:
filenames = sys.argv[1:]
else:
listfilename()
filenames = final
for file in final:
print(file)
filename = file
log_dir = os.path.join(pwd, filename)
with open(log_dir, 'r', encoding='utf-8') as f:
lines = f.readlines()
error_line = []
for line in lines:
if 'Error' in line or 'ERROR' in line or 'Exception' in line or 'EXCEPTION' in line:
line1 = re.findall(r'(ERROR|Error|Exception|EXCEPTION) (.*)', line)
if len(line1) >= 1:
line1 = line1[0]
error_line.append(line1[1])
else:
break
print('日志错误总数为------> ' + str(len(error_line)) + '个')
print('日志错误类型数量为------> ' + str(len(list(set(error_line)))) + '个')
for i in list(set(error_line)):
with open('result.txt', 'a') as result:
#result.write('日志错误总数为------> ' + str(len(error_line)) + '个'+ '\n' )
#result.write('日志错误类型数量为------> ' + str(len(list(set(error_line)))) + '个' + '\n')
result.write(file + '\n')
result.write(i + '\n')
print(i)