跳转到主要内容

轻量级HTTP日志监控和警报工具

项目描述

watchl

build status codecov code style license

使用Python编写的轻量级HTTP日志监控和警报控制台工具。

注意:目前仅支持W3C格式日志。请参阅此处的W3C格式日志文件示例。

功能

  • 日志文件实时尾部查看。
  • 控制台中的统计报告:热门部分、流量、吞吐量等。
  • 具有自定义阈值和延迟的实时警报和恢复检测。
  • 友好的命令行界面。
  • 详细且机器可读的JSON格式输出。
  • 异步架构,资源占用低。
  • 可选基于Docker的执行。

示例命令

$ watchl -f access.log --report-every=2 --alert-tick=2 --traffic-up=1 --traffic-down=1

输出(通过管道输入jq进行JSON美化)

{
  "levelname": "INFO",
  "message": "process started",
  "timestamp": "2019-07-30T21:02:30.652107",
  "pid": 2417
}
{
  "levelname": "INFO",
  "message": "watching logs",
  "timestamp": "2019-07-30T21:02:30.653034",
  "path": "access.log"
}
{
  "levelname": "INFO",
  "message": "activity in the past 2.0 seconds",
  "timestamp": "2019-07-30T21:02:32.708673",
  "sections.count": {
    "unit": "hits",
    "value": [
      {
        "key": "/presentations",
        "count": 68
      },
      {
        "key": "/images",
        "count": 13
      },
      {
        "key": "/blog",
        "count": 9
      }
    ]
  },
  "requests.total": {
    "unit": "req",
    "value": 135
  },
  "requests.average": {
    "unit": "req/s",
    "value": 67.5
  },
  "transfer.average": {
    "unit": "bytes/s",
    "value": 3064559
  },
  "transfer.mean": {
    "unit": "bytes/req",
    "value": 45400.874074074076
  }
}
{
  "levelname": "WARNING",
  "message": "alert status changed",
  "timestamp": "2019-07-30T21:02:34.773988",
  "status": "active",
  "previous_status": "activating",
  "value": 89.5,
  "reason": "stayed above threshold for longer than up delay",
  "metric": "requests.average",
  "threshold": 10,
  "up": "0:00:01",
  "down": "0:00:01"
}
...

安装

  • 如果您已安装Python 3.6+,可以从PyPI安装watchl
pip install watchl

或通过克隆存储库并运行本地

pip install .
  • 如果您已安装Docker,可以通过克隆存储库并运行来构建镜像
docker build -t watchl .

用法

一般说明

watchl公开一个名为watchl的命令行可执行文件。

在没有其他选项的情况下运行时,watchl将从/tmp/access.log读取日志(这是可配置的 - 请参阅CLI参考以获取选项)并将它们作为写入文件的消费内容。

已知限制watchl 目前尚不支持日志轮转。如果日志文件被重命名、截断或移动,watchl 将无法再读取新日志,需要重启。参见 #9

如果使用 Docker,并且假设你的机器上某处有日志文件,你可以使用以下命令运行 watchl 容器

docker run -it --rm -v /host/path/to/access.log:/tmp/access.log watchl <options>

默认情况下,watchl 在相对较长的时标(分钟)上工作。为了获得更快的反馈(例如,当你第一次尝试 watchl 时),你可以尝试以下选项

watchl --report-every=5 --alert-tick=1 --traffic-up=1 --traffic-down=1

CLI 参考

Usage: watchl [OPTIONS]

Options:
  -f PATH                         Path to an actively written-to log file.
                                  [default: /tmp/access.log]
  --report-every FLOAT            Number of seconds between stats reports.
                                  [default: 10]
  --sections-top INTEGER          Number of most hit website sections to
                                  display in the report.  [default: 3]
  --traffic-threshold FLOAT       [default: 10]
  --traffic-up FLOAT              Number of seconds to trigger an alert after
                                  traffic exceeded the threshold.  [default:
                                  120]
  --traffic-down FLOAT            Number of seconds to stop an alert after
                                  traffic dropped below the threshold.
                                  [default: 120]
  --alert-tick FLOAT              Number of seconds between evaluation of
                                  stats for alerting purposes.  [default: 10]
  --tick FLOAT                    Number of seconds between system event
                                  checks. Note: lower values increase CPU
                                  usage.  [default: 0.5]
  --log-level [debug|info|warning|critical|error]
                                  Level for internal log messages  [default:
                                  info]
  --limit-max-log-lines INTEGER   Limit the number of log lines to ingest
                                  before terminating the process.
  --help                          Show this message and exit.

贡献

参见 贡献指南

许可

MIT

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

watchl-0.2.tar.gz (12.2 kB 查看散列)

上传时间

构建分发

watchl-0.2-py3-none-any.whl (13.6 kB 查看散列)

上传时间 Python 3

支持者