CLOVER🍀

That was when it all began.

FilebeatのLog inputをシンプルに見る

これは、なにをしたくて書いたもの?

Filebeatの設定で以下のような感じで最初に出てくる、

- type: log
  paths:
    - /var/log/*.log

「log」ってなんだろう?と思ったので、少し見てみようかなと。

環境

今回の環境は、こちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:    18.04
Codename:   bionic

Ubuntu Linux 18.04 LTSです。

Filebeatは、こちら。

$ filebeat version
filebeat version 7.5.0 (amd64), libbeat 7.5.0 [6d0d0ae079e5cb1d4f224801ac6df926dfb1594c built 2019-11-25 23:34:45 +0000 UTC]

Log input

ドキュメントを見てみます。

Log input | Filebeat Reference [7.5] | Elastic

Use the log input to read lines from log files.

どうやら、ログファイルから読み込むことを想定しているみたいです。

ここで、用意されているinputの種類を見てみます。

Configure inputs / Input types

  • Log
  • Stdin
  • Container
  • Kafka
  • Redis
  • UDP
  • Docker(Deprecated)
  • TCP
  • Syslog
  • s3
  • NetFlow
  • Google Pub/Sub

といったものがあり、Containerこそコンテナ環境が出力したログファイル(/var/lib/docker/containers/*/*.log)を想定したものみたいですが、

Container input | Filebeat Reference [7.5] | Elastic

それ以外はネットワークだったりデータストアからデータを受け取ったり読み出したりするものです。

まあ、「Log」は「File」と読み替えてもよさそうな感じですね。

ちょっと試してみましょう。

FilebeatをLog inputを使って簡単に試す

シンプルに、こんな感じに設定してみました。

$ sudo grep -vE '^ *#|^$' /etc/filebeat/filebeat.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /tmp/example.log
output.console:
  enabled: true
  pretty: true

Log inputを使い、読み込むファイルは「/tmp」配下のファイルに。

- type: log
  enabled: true
  paths:
    - /tmp/example.log

出力は、標準出力にすることにしました。

output.console:
  enabled: true
  pretty: true

Configure the Console output | Filebeat Reference [7.5] | Elastic

Filebeatを再起動して

$ sudo systemctl restart filebeat

ファイルに書き出してみます。

$ echo 'hello world' >> /tmp/example.log

journalctlで追ってみましょう。

$ sudo journalctl -u filebeat -f

少し待つと、こんな感じでログが出力されます。

Dec 04 14:57:42 ubuntu1804.localdomain filebeat[3562]: 2019-12-04T14:57:42.890Z        INFO        log/harvester.go:251        Harvester started for file: /tmp/example.log
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]: {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "@timestamp": "2019-12-04T14:57:42.891Z",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "@metadata": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "beat": "filebeat",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "type": "_doc",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "version": "7.5.0"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "log": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "file": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:       "path": "/tmp/example.log"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "offset": 12
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "message": "hello world",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "input": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "type": "log"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "ecs": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "version": "1.1.0"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "host": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "name": "ubuntu1804.localdomain"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   },
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   "agent": {
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "ephemeral_id": "bb389d26-f63d-4de2-afb0-c3ebb681a8ae",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "hostname": "ubuntu1804.localdomain",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "id": "49ec660e-c0db-452b-8de0-0aa853ee476f",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "version": "7.5.0",
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:     "type": "filebeat"
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]:   }
Dec 04 14:57:43 ubuntu1804.localdomain filebeat[3562]: }

書き出した内容は、「message」として扱われるようですね。

"message": "hello world",

ちなみに、Console outputのprettyを外すと

output.console:
  enabled: true
#  pretty: true

こんな感じになります。

Dec 04 14:59:07 ubuntu1804.localdomain filebeat[3606]: 2019-12-04T14:59:07.343Z        INFO        log/harvester.go:251        Harvester started for file: /tmp/example.log
Dec 04 14:59:08 ubuntu1804.localdomain filebeat[3606]: {"@timestamp":"2019-12-04T14:59:07.345Z","@metadata":{"beat":"filebeat","type":"_doc","version":"7.5.0"},"input":{"type":"log"},"ecs":{"version":"1.1.0"},"host":{"name":"ubuntu1804.localdomain"},"agent":{"version":"7.5.0","type":"filebeat","ephemeral_id":"5e22ec2a-d883-4bd3-b9c6-58f929af56d7","hostname":"ubuntu1804.localdomain","id":"49ec660e-c0db-452b-8de0-0aa853ee476f"},"log":{"offset":39,"file":{"path":"/tmp/example.log"}},"message":"hello filebeat"}

では、今度はOutputをElasticsearchにしてみましょう。

$ sudo grep -vE '^ *#|^$' /etc/filebeat/filebeat.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /tmp/example.log
output.elasticsearch:
  hosts: ["localhost:9200"]

ファイルに書き込み。

$ echo 'hello elasticsearch' >> /tmp/example.log

インデックスに反映されたら、確認してみます。

$ curl localhost:9200/filebeat-7.5.0-2019.12.04/_search?pretty
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "filebeat-7.5.0-2019.12.04",
        "_type" : "_doc",
        "_id" : "7z9v0W4Bqxanhf8qQ2z-",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2019-12-04T15:02:38.849Z",
          "log" : {
            "offset" : 89,
            "file" : {
              "path" : "/tmp/example.log"
            }
          },
          "message" : "hello elasticsearch",
          "input" : {
            "type" : "log"
          },
          "ecs" : {
            "version" : "1.1.0"
          },
          "host" : {
            "name" : "ubuntu1804.localdomain"
          },
          "agent" : {
            "type" : "filebeat",
            "ephemeral_id" : "20be7f2a-a3ac-43c1-8039-219d80e07fd9",
            "hostname" : "ubuntu1804.localdomain",
            "id" : "49ec660e-c0db-452b-8de0-0aa853ee476f",
            "version" : "7.5.0"
          }
        }
      }
    ]
  }
}

こんな感じに入りました、と。

      {
        "_index" : "filebeat-7.5.0-2019.12.04",
        "_type" : "_doc",
        "_id" : "7z9v0W4Bqxanhf8qQ2z-",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2019-12-04T15:02:38.849Z",
          "log" : {
            "offset" : 89,
            "file" : {
              "path" : "/tmp/example.log"
            }
          },
          "message" : "hello elasticsearch",
          "input" : {
            "type" : "log"
          },
          "ecs" : {
            "version" : "1.1.0"
          },
          "host" : {
            "name" : "ubuntu1804.localdomain"
          },
          "agent" : {
            "type" : "filebeat",
            "ephemeral_id" : "20be7f2a-a3ac-43c1-8039-219d80e07fd9",
            "hostname" : "ubuntu1804.localdomain",
            "id" : "49ec660e-c0db-452b-8de0-0aa853ee476f",
            "version" : "7.5.0"
          }
        }
      }

これをパースしたりいろいろしたかったら、Ingest Nodeを使ったり、Logstashを使ったりしよう、ということになるんでしょうね。

まずは確認、と。