从日志文件中提取异常
项目描述
基于成功日志,logreduce突出显示失败日志中的有用文本。目标是节省查找故障根本原因的时间。
平均而言,学习运行速度为每秒2000行,测试运行速度为每秒1300行。
工作原理
logreduce使用一个模型来学习成功日志并在失败日志中检测新颖之处
使用正则表达式手动删除随机单词
然后,将行转换为标记出现矩阵(使用HashingVectorizer),
无监督学习器实现邻域搜索(使用NearestNeighbors)。
注意事项
当调试内容仅在失败日志中包含时,此方法不起作用。要成功检测异常,失败和成功日志需要相似,否则失败日志中的额外信息将被视为异常。
例如,在testr中会发生这种情况,其中成功日志只包含‘SUCCESS’。
安装
Fedora
sudo dnf install -y python3-scikit-learn
git clone https://softwarefactory-project.io/r/logreduce
pushd logreduce
python3 setup.py develop --user
popd
openSUSE
sudo zypper install python3-scikit-learn
git clone https://softwarefactory-project.io/r/logreduce
pushd logreduce
python3 setup.py develop --user
popd
Pip
pip install --user logreduce
命令行界面使用方法
Logreduce需要一个用于成功日志训练的基线,以及一个用于减少日志的目标。
Logreduce在控制台打印异常,日志文件不会被修改
"%(distance)f | %(log_path)s:%(line_number)d: %(log_line)s"
本地文件使用
比较两个文件或目录,无需构建模型
$ logreduce diff testr-nodepool-01/output.good testr-nodepool-01/output.fail
0.232 | testr-nodepool-01/output.fail:0677: File "voluptuous/schema_builder.py", line 370, in validate_mapping
0.462 | testr-nodepool-01/output.fail:0678: raise er.MultipleInvalid(errors)
0.650 | testr-nodepool-01/output.fail:0679: voluptuous.error.MultipleInvalid: required key not provided @ data['providers'][2]['cloud']
比较两个文件或目录
$ logreduce dir preprod-logs/ /var/log/
或者首先构建模型,然后单独运行它
$ logreduce dir-train sosreport.clf old-sosreport/ good-sosreport/
$ logreduce dir-run sosreport.clf new-sosreport/
Zuul作业使用
Logreduce可以查询Zuul构建数据库以训练模型。
从工作日志中提取新颖性
$ logreduce job http://logs.openstack.org/...
# Reduce comparaison to a single project (e.g. for tox jobs)
$ logreduce job --project openstack/nova http://logs.openstack.org/...
# Compare using many baselines
$ logreduce job --count 10 http://logs.openstack.org/...
# Include job artifacts
$ logreduce job --include-path logs/ http:/logs.openstack.org/...
或者首先构建模型,然后单独运行它
$ logreduce job-train --job job_name job_name.clf
$ logreduce job-run job_name.clf http://logs.openstack.org/.../
Journald使用
Logreduce可以在journald中查找异常,比较前一天/周/月与上一个月的差异
从昨天的日志中提取新颖性
$ logreduce journal --range day
使用上个月的日志构建模型,并在上周查找新颖性
$ logreduce journal-train --range month good-journal.clf
$ logreduce journal-run --range week good-journal.clf
过滤器配置
某些内容会产生假阳性,可以通过过滤器忽略。使用–config命令行属性,可以为exclude_files、exclude_paths和exclude_lines设置过滤器。以下是一个过滤器配置文件的示例
filters:
exclude_files:
- "deployment-hieradata.j2.yaml"
- "tempest.html"
exclude_paths:
- "group_vars/Compute"
- "group_vars/Controller"
- "group_vars/Undercloud"
exclude_lines:
# neutron dhcp interface
- "^tap[^ ]*$"
# IPA cookies
- "^.*[Cc]ookie.*ipa_session="
Python模块API
Logreduce可以用作Python模块进行自定义使用。
首先您需要创建一个分类器对象
from logreduce import Classifier, Tokenizer, render_html
clf = Classifier(
# A function to normalize filename, for example to remove dates or id
filename_to_modelname=lambda fn: fn,
# A function to ignore some file, for example configuration file
keep_file=lambda _: True,
# A function to process line
process_line=Tokenizer.process
)
然后在基线对象上训练
clf.train(["./success-logs/"])
然后测试目标并创建报告
result = clf.process(["./failed-logs/"])
with open("report.html", "w") as of:
of.write(render_html(result))
logreduce-tests
该软件包包含不同类型日志(如testr或syslog)的测试数据。每个测试都包括日志故障中的预计算异常列表。
该软件包还包括一个命令行实用程序,可以运行logreduce对所有测试数据,并打印其性能摘要。
测试格式
每个测试案例由以下组成
一个.good文件(或目录),包含基线
一个.fail文件(或目录)
一个info.yaml文件,描述预期输出
threshold: float # set the distance threshold for the test
anomalies:
- optional: bool # to define minor anomalies not considered false positive
lines: | # the expected lines to be highlighted
Traceback...
RuntimeError...
评估
要运行评估,首先安装logreduce-tests
git clone https://softwarefactory-project.io/r/logreduce-tests
pushd logreduce-tests
python3 setup.py develop --user
logreduce-tests期望测试目录作为参数
$ logreduce-tests tests/testr-zuul-[0-9]*
[testr-zuul-01]: 100.00% accuracy, 5.00% false-positive
[testr-zuul-02]: 80.00% accuracy, 0.00% false-positive
...
Summary: 90.00% accuracy, 2.50% false-positive
添加–debug以显示假阳性和缺失块。
待办事项
添加终端颜色输出
添加进度条
更好地区分训练调试和测试调试
添加起始日志行和已编写的报告
在utils.files_iterator中添加tarball遍历
添加logstash过滤器模块
改进标记化测试
路线图
丢弃100%异常的文件
报告平均偏差而不是绝对距离
研究第二阶段模型
贡献
贡献最欢迎,使用git-review提出更改。在登录后设置ssh密钥https://softwarefactory-project.io/auth/login
代码风格由black管理,在提交之前运行black logreduce格式化源文件。
项目详情
下载文件
下载您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
构建分布
logreduce-0.6.1.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 599464cfb79db2c6070601f04606f16ddab294e576479d3feef10c53fbb6d2b3 |
|
MD5 | dc4007539e1be7ac01a6a650074b9cce |
|
BLAKE2b-256 | 912806d9fdb1a1763bfa7a88e09645315ac0d599af14ac5e7b079136440d560d |