# Manticore 与 Logstash/Filebeat 的集成

一个示例，展示如何将 Logstash/Filebeat 与 Manticore 一起使用

## 介绍

[Logstash](https://www.elastic.co/logstash) 是一个日志管理工具，可以从各种来源收集数据，实时转换数据，并将其发送到您指定的目的地。它通常用作 Elasticsearch 的数据管道，Elasticsearch 是一个开源的分析和搜索引擎。
[Filebeat](https://www.elastic.co/beats/filebeat) 是一个轻量级的转发器，用于转发和集中日志数据。安装为代理后，它会监控您指定的日志文件或位置，收集日志事件，并将其转发进行索引，通常转发到 Elasticsearch 或 Logstash。

现在，Manticore 也支持使用 Logstash 和 Filebeat 作为处理管道。这允许收集和转换后的数据像发送到 Elasticsearch 一样发送到 Manticore。目前支持 Logstash 7.6-9.3 版本和 Filebeat 7.17-9.3 版本。

让我们检查一个用于对 `dpkg.log`（Debian 包管理器的标准日志文件）进行索引的简单 Logstash 配置文件示例。日志本身具有简单的结构，如下所示：

``` ini
2023-05-31 10:42:55 status triggers-awaited ca-certificates-java:all 20190405ubuntu1.1
2023-05-31 10:42:55 trigproc libc-bin:amd64 2.31-0ubuntu9.9 <none>
2023-05-31 10:42:55 status half-configured libc-bin:amd64 2.31-0ubuntu9.9
2023-05-31 10:42:55 status installed libc-bin:amd64 2.31-0ubuntu9.9
2023-05-31 10:42:55 trigproc systemd:amd64 245.4-4ubuntu3.21 <none>
```

## Logstash 配置

以下是一个 Logstash 配置示例：

``` bash
input {
  file {
    path => ["/var/log/dpkg.log"]
    start_position => "beginning"
    sincedb_path => "/dev/null"
    mode => "read"
    exit_after_read => "true"
   file_completed_action => "log"
   file_completed_log_path => "/dev/null"
  }
}

output {
  elasticsearch {
   index => " dpkg_log"
   hosts => ["http://localhost:9308"]
   ilm_enabled => false
   manage_template => false
  }
}
```

请注意，在继续之前，需要解决一个关键注意事项：Manticore 不支持 Elasticsearch 的日志模板管理和索引生命周期管理功能。由于这些功能在 Logstash 中默认是启用的，因此需要在配置中显式禁用它们。此外，`output` 配置部分中的 `hosts` 选项必须对应 Manticore 的 HTTP 监听端口（默认是 `localhost:9308`）。

## Logstash 结果

在按照上述说明调整配置后，您可以运行 Logstash，dpkg 日志中的数据将被传递到 Manticore 并正确索引。

以下是创建的表的结构和插入文档的示例：

``` sql
mysql> DESCRIBE dpkg_log;
+------------------+--------+---------------------+
| Field            | Type   | Properties          |
+------------------+--------+---------------------+
| id               | bigint |                     |
| message          | text   | indexed stored      |
| @version         | text   | indexed stored      |
| @timestamp       | text   | indexed stored      |
| path             | text   | indexed stored      |
| host             | text   | indexed stored      |
+------------------+--------+---------------------+

mysql> SELECT * FROM dpkg_log LIMIT 1\G

*************************** 1. row ***************************
id: 7280000849080746110
host: logstash-db848f65f-lnlf9
message: 2023-04-12 02:03:21 status unpacked libc-bin:amd64 2.31-0ubuntu9
path: /var/log/dpkg.log
@timestamp: 2023-06-16T09:23:57.405Z
@version: 1
```

## Filebeat 配置

另一种收集原始数据的方法是使用 Filebeat 代理。以下是用于我们示例 dpkg 日志的 Filebeat 配置：

``` yaml
filebeat.inputs:
- type: filestream
  id: example
  paths:
	- /var/log/dpkg.log

output.elasticsearch:
  hosts: ["http://localhost:9308"]
  index:  "dpkg_log"
  allow_older_versions: true

setup.ilm:
  enabled: false

setup.template:
  name: "dpkg_log"
  pattern: "dpkg_log"
```

### Filebeat 8.10 以上版本的配置

Filebeat 8.10 以上版本默认启用了输出压缩功能。因此，必须在配置文件中添加 `compression_level: 0` 选项以提供与 Manticore 的兼容性：

``` yaml
filebeat.inputs:
- type: filestream
  id: example
  paths:
	- /var/log/dpkg.log

output.elasticsearch:
  hosts: ["http://localhost:9308"]
  index:  "dpkg_log"
  allow_older_versions: true
  compression_level: 0

setup.ilm:
  enabled: false

setup.template:
  name: "dpkg_log"
  pattern: "dpkg_log"
```


## Filebeat 结果

一旦使用此配置运行 Filebeat，日志数据将被发送到 Manticore 并正确索引。以下是 Manticore 创建的表的结构和插入文档的示例：

``` sql
mysql> DESCRIBE dpkg_log;
+------------------+--------+--------------------+
| Field            | Type   | Properties         |
+------------------+--------+--------------------+
| id               | bigint |                    |
| @timestamp       | text   | indexed stored     |
| message          | text   | indexed stored     |
| log              | json   |                    |
| input            | json   |                    |
| ecs              | json   |                    |
| host             | json   |                    |
| agent            | json   |                    |
+------------------+--------+--------------------+


mysql> SELECT * FROM dpkg_log LIMIT 1\G
*************************** 1. row ***************************
id: 7280000849080753116
@timestamp: 2023-06-16T09:27:38.792Z
message: 2023-04-12 02:06:08 status half-installed libhogweed5:amd64 3.5.1+really3.5.1-2
input: {"type":"filestream"}
ecs: {"version":"1.6.0"}
host: {"name":"logstash-db848f65f-lnlf9"}
agent: {"ephemeral_id":"587c2ebc-e7e2-4e27-b772-19c611115996","id":"2e3d985b-3610-4b8b-aa3b-2e45804edd2c","name":"logstash-db848f65f-lnlf9","type":"filebeat","version":"7.10.0","hostname":"logstash-db848f65f-lnlf9"}
log: {"offset":80,"file":{"path":"/var/log/dpkg.log"}}
```

## 结论

因此，现在您可以轻松地使用 Manticore 结合 Logstash 或 Filebeat 来索引日志数据。Manticore 与 Logstash 和 Filebeat 的集成为轻松索引日志数据开辟了新的机会。
