Skip to main content

从日志文件中提取异常

项目描述

根据成功日志,logreduce 突出显示失败日志中的有用文本。目标是节省查找故障根本原因的时间。

平均而言,学习以每秒 2000 行的速度运行,测试以每秒 1300 行的速度运行。

这个怎么运作

logreduce 使用模型来学习成功的日志并检测失败日志中的新奇事物:

  • 使用正则表达式手动删除随机词

  • 然后将行转换为令牌出现的矩阵(使用HashingVectorizer),

  • 无监督学习器实现邻居搜索(使用NearestNeighbors)。

注意事项

当调试内容仅包含在失败的日志中时,此方法不起作用。要成功检测异常,失败日志和成功日志需要相似,否则失败日志中的额外信息将被视为异常。

例如,成功日志仅包含“SUCCESS”的 testr 会发生这种情况。

安装

  • 软呢帽:

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 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/.../

日志使用

Logreduce 可以在日志中查找异常,将最后一天/周/月与前一天进行比较:

  • 从最后一天的日记中提取新奇:

$ 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:
    - <s>"deployment-hieradata.j2.yaml"</s>
    - <s>"tempest.html"</s>
  exclude_paths:
    - <s>"group_vars/Compute"</s>
    - <s>"group_vars/Controller"</s>
    - <s>"group_vars/Undercloud"</s>
  exclude_lines:
    # neutron dhcp interface
    - <s>"^tap[^</s> <s>]*$"</s>
    # IPA cookies
    - <s>"^.*[Cc]ookie.*ipa_session="</s>

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))

日志减少测试

该软件包包含不同类型日志的测试数据,例如 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 (56.5 kB 查看哈希

已上传 source

内置分布

logreduce-0.6.1-py2.py3-none-any.whl (49.8 kB 查看哈希

已上传 py2 py3