# LogstashでFilebeatの送信ファイル名を取得する

**URL:** https://discuss.elastic.co/t/logstash-filebeat/218434
**Category:** 日本語による質問・議論はこちら
**Created:** [February 8, 2020, 11:12am UTC](https://discuss.elastic.co/t/logstash-filebeat/218434 "2020-02-08T11:12:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![echo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/echo/32/62310_2.png) [@echo](https://discuss.elastic.co/u/echo)
#### Post date: [February 8, 2020, 11:12am UTC](https://discuss.elastic.co/t/logstash-filebeat/218434/1 "2020-02-08T11:12:36Z")

</div>

Logstash.conf で Filebeat から送信されたファイル名を grok で取得したいのですが、  
下記で出来ませんでした。(バージョン7.5.2)

6.3.1 では出来ていて、ドキュメントを読んでも同様の動作が  
可能な方法が見つからない状況です。

Filebeatの送信ファイルパスは、/var/log/2.log です。  
どなたかご教示いただければと思います。

```
input {
  beats {
    port => 5044
  }
}

filter {
  grok {
    match => { "source" => "/var/log/%{INT:filename}.log" }
  }
}

output {
  file {
    path => "/usr/local/var/logstash/%{filename}.tsv"
    codec => line { format => "%{message}" }
  }
}
```

---

<div class="post-metadata">

### Author: ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)
#### Post date: [February 9, 2020, 1:10pm UTC](https://discuss.elastic.co/t/logstash-filebeat/218434/2 "2020-02-09T13:10:08Z")

</div>

こんにちわ

filebeat6.3.1と、7.5.2ではlogstashに送られる中身の構造が変わっているかと思います。  
これは、7系からElastic Common Schemaに準拠するようになったからかと思っています。

> **[Migrating to Elastic Common Schema (ECS) in Beats environments](https://www.elastic.co/jp/blog/migrating-to-elastic-common-schema-in-beats-environments)**
>
> Find out how to migrate to the Elastic Common Schema (ECS) in a Beats environment (after upgrading Elasticsearch and Kibana to version 7.x). Get tips on preparation and strategy, and see a walkthrough of a n example migration.

例えば、7.5.2ではこんなデータになってLogstashに送られていますね。

```ruby
{
    "@version" => "1",
    "@timestamp" => 2020-02-06T10: 14: 11.310Z,
    "agent" => {
        "hostname" => "a00271752ce1",
        "version" => "7.5.2",
        "id" => "b26e6309-0577-408e-9284-b059421fcf0c",
        "type" => "filebeat",
        "ephemeral_id" => "c4eb8204-77e1-4d87-a252-c5ad5f978afb"
    },
    "log" => {
        "offset" => 0,
        "file" => {
            "path" => "/var/log/2.log"
        }
    },
    "ecs" => {
        "version" => "1.1.0"
    },
    "message" => "aaaa",
    "host" => {
        "hostname" => "a00271752ce1",
        "containerized" => true,
        "os" => {
            "version" => "7 (Core)",
            "kernel" => "4.19.76-linuxkit",
            "family" => "redhat",
            "name" => "CentOS Linux",
            "platform" => "centos",
            "codename" => "Core"
        },
        "name" => "a00271752ce1",
        "architecture" => "x86_64"
    },
    "input" => {
        "type" => "log"
    }
}

```

このため、従来 sourceに入っていたところのファイルのパスが `log.file.path`になっています。  
ここがLogstash側でエラーになっている原因かと推察します。

ですので、grokの部分をlog.file.pathにあわせて変更してやれば良いとなります。  
こんな感じです。

```auto
input {
  beats {
    port => 5044
  }
}

filter {
  grok {
    # データ取得元を変更
    match => { "[log][file][path]" => "/var/log/%{INT:filename}.log" }
  }
}

output {
  stdout {
  }
}

```

まず、一度stdoutでgrokのエラーが出ないことを確認してから、ファイル出力でお試しいただくのが良いかと思います。

ご参考になれば幸いです。

---

<div class="post-metadata">

### Author: ![echo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/echo/32/62310_2.png) [@echo](https://discuss.elastic.co/u/echo)
#### Post date: [February 9, 2020, 3:51pm UTC](https://discuss.elastic.co/t/logstash-filebeat/218434/3 "2020-02-09T15:51:54Z")

</div>

> [@tsgkdt](#):
>
> [log][file][path]

ご丁寧なご説明と、貴重な情報をありがとうございます。  
無事に解決できました。

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 8, 2020, 3:51pm UTC](https://discuss.elastic.co/t/logstash-filebeat/218434/4 "2020-03-08T15:51:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
