解析字符串的多个日志文件
Parse multiple log files for strings
我正在尝试从日志目录中解析多个日志文件,以在列表中搜索任意数量的字符串以及服务器名称。我觉得我已经尝试了一百万种不同的选择,而且我只使用一个日志文件就可以正常工作。但是当我尝试浏览目录中的所有日志文件时,我似乎无处可去。
if args.f:
logs = args.f
else:
try:
logs = glob("/var/opt/cray/log/p0-current/*")
except IndexError:
print "Something is wrong. p0-current is not available."
sys.exit(1)
valid_errors = ["error", "nmi", "CATERR"]
logList = []
for log in logs:
logList.append(log)
#theLog = open("logList")
#logFile = log.readlines()
#logFile.close()
#printList = []
#for line in logFile:
# if (valid_errors in line):
# printList.append(line)
#
#for item in printList:
# print item
# with open("log", "r") as tmp_log:
# open_log = tmp_log.readlines()
# for line in open_log:
# for down_nodes in open_log:
# if valid_errors in open_log:
# print valid_errors
down_nodes
是脚本上方的预填充列表,其中包含标记为关闭的服务器列表。
注释掉了我一直在努力的一些尝试。
logList = []
for log in logs:
logList.append(log)
我认为这可能是将每个单独的日志文件放在一个列表中的方法,然后遍历该列表并使用 open()
后跟 readlines()
但我缺少某种逻辑在这里..也许我想的不正确。
请给我一些建议。
谢谢。
所以你的最后一个 for 循环是多余的,因为 logs
已经是一个字符串列表。有了这些信息,我们可以遍历 logs
并为每个 log
.
做一些事情
for log in logs:
with open(log) as f:
for line in f.readlines():
if any(error in line for error in valid_errors):
#do stuff
行 if any(error in line for error in valid_errors):
检查 line
以查看 valid_errors
中是否存在任何错误。语法是一个生成器,它为 valid_errors
中的每个 error
生成 error
。
要回答您涉及 down_nodes
的问题,我认为您不应该将其包含在同一个 any()
中。你应该试试
if any(error in line for error in valid_errors) and \
any(node in line for node in down_nodes):
首先你需要找到所有日志:
import os
import fnmatch
def find_files(pattern, top_level_dir):
for path, dirlist, filelist in os.walk(top_level_dir):
for name in fnmatch.filter(filelist, pattern)
yield os.path.join(path, name)
例如,要查找当前目录中的所有 *.txt
个文件:
txtfiles = find_files('*.txt', '.')
然后从名称中获取文件对象:
def open_files(filenames):
for name in filenames:
yield open(name, 'r', encoding='utf-8')
最后来自文件的个别行:
def lines_from_files(files):
for f in files:
for line in f:
yield line
由于您想查找一些错误,检查可能如下所示:
import re
def find_errors(lines):
pattern = re.compile('(error|nmi|CATERR)')
for line in lines:
if pattern.search(line):
print(line)
您现在可以处理从给定目录生成的行流:
txt_file_names = find_files('*.txt', '.')
txt_files = open_files(txt_file_names)
txt_lines = lines_from_files(txt_files)
find_errors(txt_lines)
将日志作为数据流处理的想法源自 David Beazley 的演讲。
我正在尝试从日志目录中解析多个日志文件,以在列表中搜索任意数量的字符串以及服务器名称。我觉得我已经尝试了一百万种不同的选择,而且我只使用一个日志文件就可以正常工作。但是当我尝试浏览目录中的所有日志文件时,我似乎无处可去。
if args.f:
logs = args.f
else:
try:
logs = glob("/var/opt/cray/log/p0-current/*")
except IndexError:
print "Something is wrong. p0-current is not available."
sys.exit(1)
valid_errors = ["error", "nmi", "CATERR"]
logList = []
for log in logs:
logList.append(log)
#theLog = open("logList")
#logFile = log.readlines()
#logFile.close()
#printList = []
#for line in logFile:
# if (valid_errors in line):
# printList.append(line)
#
#for item in printList:
# print item
# with open("log", "r") as tmp_log:
# open_log = tmp_log.readlines()
# for line in open_log:
# for down_nodes in open_log:
# if valid_errors in open_log:
# print valid_errors
down_nodes
是脚本上方的预填充列表,其中包含标记为关闭的服务器列表。
注释掉了我一直在努力的一些尝试。
logList = []
for log in logs:
logList.append(log)
我认为这可能是将每个单独的日志文件放在一个列表中的方法,然后遍历该列表并使用 open()
后跟 readlines()
但我缺少某种逻辑在这里..也许我想的不正确。
请给我一些建议。
谢谢。
所以你的最后一个 for 循环是多余的,因为 logs
已经是一个字符串列表。有了这些信息,我们可以遍历 logs
并为每个 log
.
for log in logs:
with open(log) as f:
for line in f.readlines():
if any(error in line for error in valid_errors):
#do stuff
行 if any(error in line for error in valid_errors):
检查 line
以查看 valid_errors
中是否存在任何错误。语法是一个生成器,它为 valid_errors
中的每个 error
生成 error
。
要回答您涉及 down_nodes
的问题,我认为您不应该将其包含在同一个 any()
中。你应该试试
if any(error in line for error in valid_errors) and \
any(node in line for node in down_nodes):
首先你需要找到所有日志:
import os
import fnmatch
def find_files(pattern, top_level_dir):
for path, dirlist, filelist in os.walk(top_level_dir):
for name in fnmatch.filter(filelist, pattern)
yield os.path.join(path, name)
例如,要查找当前目录中的所有 *.txt
个文件:
txtfiles = find_files('*.txt', '.')
然后从名称中获取文件对象:
def open_files(filenames):
for name in filenames:
yield open(name, 'r', encoding='utf-8')
最后来自文件的个别行:
def lines_from_files(files):
for f in files:
for line in f:
yield line
由于您想查找一些错误,检查可能如下所示:
import re
def find_errors(lines):
pattern = re.compile('(error|nmi|CATERR)')
for line in lines:
if pattern.search(line):
print(line)
您现在可以处理从给定目录生成的行流:
txt_file_names = find_files('*.txt', '.')
txt_files = open_files(txt_file_names)
txt_lines = lines_from_files(txt_files)
find_errors(txt_lines)
将日志作为数据流处理的想法源自 David Beazley 的演讲。