Logstash cheat sheet: Difference between revisions

 
(160 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Elastic stack / ELK (Elasticsearch + Logstash + Kibana) =
= Elastic stack / ELK (Elasticsearch + Logstash + Kibana) =
'''諸般の事情により更新休止します'''
== 久しぶりの追記 ==
久しぶりに logstash.conf を修正したら、割と重要な仕様変更がたくさんあり、過去の記述は現在の仕様に合ってないです。ちなみに ruby code を追記したのですが、event.get event.set とかになっている。
<syntaxhighlight lang="javascript" enclose="div">
  ruby {
      code => "event.set('foo',event.get('foo')+ 5.1)"
  }
</syntaxhighlight>
こんな感じ。
その他、認証系強化による仕様の混乱はとりあえずなんとかするしかないw


== splunk のデモをみて、自分の環境に logstash を入れてみた。 :) ==
== splunk のデモをみて、自分の環境に logstash を入れてみた。 :) ==
:(ELKBの更新が速いので、随時記事をアップデートします。[https://www.elastic.co/downloads/elasticsearch Elasticsearch 6.0.0], [https://www.elastic.co/downloads/logstash Logstash 6.0.0], [https://www.elastic.co/downloads/beats/filebeat Filebeat 6.0.0], [https://www.elastic.co/downloads/kibana Kibana 6.0.0])
:(ELKBの更新が速いので、随時記事をアップデートします。[https://www.elastic.co/downloads/elasticsearch Elasticsearch 7.6.2], [https://www.elastic.co/downloads/logstash Logstash 7.6.2], [https://www.elastic.co/downloads/beats/filebeat Filebeat 7.6.2], [https://www.elastic.co/downloads/kibana Kibana 7.6.2])


:[http://ja.splunk.com/ splunk] のデモを見て、構造化ロギング・高速検索・可視化の素晴らしさが解ったので、それをオープンソースの Elastic stack (ELK stack) で実現する。
:[http://ja.splunk.com/ splunk] のデモを見て、構造化ロギング・高速検索・可視化の素晴らしさが解ったので、それをオープンソースの Elastic stack (ELK stack) で実現する。
Line 8: Line 21:


最近の Elastic Stack は logstash いらずで簡易にという方向を狙ってきている。それはそれで嬉しいが、いろいろ細かく触りたい場合には、まだまだ logstash をホゲらないと逝けない。
最近の Elastic Stack は logstash いらずで簡易にという方向を狙ってきている。それはそれで嬉しいが、いろいろ細かく触りたい場合には、まだまだ logstash をホゲらないと逝けない。
::完全に通常の運用につかっているので、アグレッシブな変更はできなくなってるので突っ込んだ記事がかけない^^;;;
7.6.0 は kibana の index の変換するのだが、elasticseach のサーキットブレーカーが作動して完了しない。
:一時的に elasticsearch の heap 増やした。(最終的には、今回の件がギリギリ回避できるメモリー量に増やした)
7.6.0 から X-Pack の概念が変わった(なくなった?)
:logstash.yml から xpack の設定をコメントアウト
timelion の位置付けが変わった。
kibana.ymlに
<syntaxhighlight lang="yaml" enclose="div">
timelion.ui.enabled: true
</syntaxhighlight>
で元に戻る。設定を確認して、新しいビジュアライズに移行する。
7.4.1 の logstash がダメっぽい。ヘビー io-wait
:以前よりメモリー食いになってるようで、ES も含めて頑張って使用メモリー削減してかろうじて稼働中
:: dns filter がぁ failed_cache_size ちゃんと設定しないとまずい。
'''ようやく 7.x にあげた'''
あら...意外と簡単に入った 7.4.0...でもないぞ。
よく見たら index 名が変わって Index Lifecycle Management がよしなにしてしまうようで、昔に戻すには elasticsearch output pulgin で
<syntaxhighlight lang="javascript" enclose="div">
ilm_enabled => false
</syntaxhighlight>
kibana の index pattern を削除したあとトラブル。新しい index pattern 作っているのに index pattern が無いので作れといわれる
解決策は、新しい index pattern を作るときに、advanced options を表示して、Custom index pattern ID に index pattern と同じ文字をいれる。たとえば index pattern が "logstash-*" なら Custom index pattern ID も "logstash-*" とする。
私は '*' という文字が嫌なので(あとで問題起きそう)、全部 id を書き換えた。
filebeat.yml で filebeat.registry_file 行削除すること
logstash の elasticsearch-template を変更
<syntaxhighlight lang="javascript" enclose="div">
    "_default_" : {
    ****
    }
</syntaxhighlight>
を削除
python beaver から送られる 'host' を remove
<syntaxhighlight lang="javascript" enclose="div">
    mutate {
      rename => { "host" => "my_host" }
    }
    mutate {
      add_field => { "[host][name]" => "%{my_host}" }
    }
</syntaxhighlight>
beats が送っていた "host" は host.name に
[host][name]
agent.* が増えた
7.0.0 出た! テストは気力があるときwにします。。。
6.6.1 で geoip.postal_code の型がコンフリクト
[https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/788/files#diff-c1b7e63b1e6f2c2cfe8656fa847ecba5 "postal_code" : { "type" : "keyword" }]
6.5.0 で kibana の index が変更されて .kibana_1 .kibana_2 というのができた。これですこしハマった。
6.2.0 で /etc/elasticsearch/jvm.options
-Djava.io.tmpdir=${ES_TMPDIR}
:: ES_TMPDIR の定義がないのでエラーになる。
'''filebeat 6.3.0 は問題ありそう。6.2.4 のままで様子を見る。'''
"error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse [host]"
:::[https://github.com/elastic/beats/issues/7050 Metricbeat `host` namespace causes mapping conflict when used with Logstash]
:::[https://discuss.elastic.co/t/logstash-errors-after-upgrading-to-filebeat-6-3-0/135984/5 Logstash errors after upgrading to filebeat-6.3.0]
[https://github.com/elastic/beats/blob/master/libbeat/docs/breaking.asciidoc#breaking-changes-in-63 Breaking changes in 6.3]
:ここに着地
<syntaxhighlight lang="javascript" enclose="div">
  "mappings" : {
      "properties" : {
        "host"  : {
          "dynamic": true,
          "properties" : {
            "name": { "type": "keyword" },
            "id" : { "type" : "keyword" },
            "architecture" : { "type" : "keyword" },
            "os" : {
              "dynamic" : true,
              "properties" : {
                "platform": { "type": "keyword" },
                "version": { "type": "keyword" },
                "family": { "type": "keyword" }
            }
            "ip" : { "type" : "ip" },
            "mac" : { "type" : "keyword" }
          }
        }
      }
    }
  }
</syntaxhighlight>
:こんな感じの template でいいのかねぇ
::[https://github.com/elastic/ecs Elastic Common Schema] こんなこと計画してるらしい
それか無視するか
<syntaxhighlight lang="javascript" enclose="div">
  # For Beat and LSF compatibility
  if [beat][name] {
    # Remove host metadata
    mutate {
      remove_field => [ "[host]" ]
    }
    mutate {
      add_field => {
        "host" => "%{[beat][name]}"
      }
    }
</syntaxhighlight>
:beat.hostname ではなく beat.name つかえと...


===logstash の書籍===
===logstash の書籍===
Line 33: Line 172:
Filebeat:
Filebeat:
  端末(filebeat) ----> 解析サーバ(logstash-shipper)
  端末(filebeat) ----> 解析サーバ(logstash-shipper)
[端末(filebeat) ----> 解析・蓄積(Elasticsearch Ingest Node)]
logstash-shipper:
logstash-shipper:
  Filebeat の待ち受け(input beats)、ブローカーへそのまま吐き出す(output redis)
  Filebeat の待ち受け(input beats)、ブローカーへそのまま吐き出す(output redis)
Line 138: Line 279:
#=========================== Filebeat prospectors =============================
#=========================== Filebeat prospectors =============================


filebeat.prospectors:
filebeat.inputs:
#filebeat.prospectors:


- type: log
- type: log
Line 302: Line 444:


*apache
*apache
  [https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/httpd 公式 pattern の COMBINEDAPACHELOG]  
  [https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/httpd 公式 pattern の COMBINEDAPACHELOG]
  [https://github.com/nxhack/logstash/blob/master/patterns/apache apache grok pattern]
  [https://github.com/nxhack/logstash/blob/master/patterns/apache apache grok pattern]


Line 342: Line 484:
  https://github.com/ua-parser/uap-core/blob/master/regexes.yaml
  https://github.com/ua-parser/uap-core/blob/master/regexes.yaml
  https://raw.githubusercontent.com/ua-parser/uap-core/master/regexes.yaml
  https://raw.githubusercontent.com/ua-parser/uap-core/master/regexes.yaml
geoip 更新処理
<syntaxhighlight lang="bash" enclose="div">
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz
rm *.old
mv GeoLite2-City.mmdb GeoLite2-City.mmdb.old
mv GeoLite2-Country.mmdb GeoLite2-Country.mmdb.old
mv GeoLite2-ASN.mmdb GeoLite2-ASN.mmdb.old
gunzip GeoLite2-City.mmdb.gz
gunzip GeoLite2-Country.mmdb.gz
tar xfz GeoLite2-ASN.tar.gz --strip=1 --wildcards */GeoLite2-ASN.mmdb
rm GeoLite2-ASN.tar.gz
</syntaxhighlight>


== enable high precision timestamps ==
== enable high precision timestamps ==
Line 347: Line 504:
===rsyslog の timestamp を milliseconds まで取る===
===rsyslog の timestamp を milliseconds まで取る===
:/etc/rsyslog.conf の
:/etc/rsyslog.conf の
  # Use traditional timestamp format.                                                                                          
  # Use traditional timestamp format.
  # To enable high precision timestamps, comment out the following line.                                                      
  # To enable high precision timestamps, comment out the following line.
  #                                                                                                                            
  #
  $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
  $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
:の指示通り、"ActionFileDefaultTemplate" 行をコメントアウトする。
:の指示通り、"ActionFileDefaultTemplate" 行をコメントアウトする。
Line 412: Line 569:


== X-Pack ==
== X-Pack ==
無償の範囲で使うには、ユーザ権限機能(旧 Shield)を無効にする。
Basic ライセンスというのが Free で、ライセンスを1年ごとに更新していけば、ずっと無料でつかえそうだ。
/etc/elasticsearch/elasticsearch.yml と /etc/kibana/kibana.yml に、
:6.3.0 から Basic ライセンスは有効期限が無制限になった。また面倒だったアップテート時の x-pack のインストース作業も不要になった。
<syntaxhighlight lang="text" enclose="div">
 
xpack.security.enabled: false
自分のテスト環境はシングルノードなので status が YELLOW にならないように、
/usr/share/elasticsearch/plugins/x-pack/x-pack-core/x-pack-core-6.2.4.jar
:テンプレート json の index.number_of_replicas を変更する。
 
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-logstash'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-alerts'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-es-2'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-es'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-data-2'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-kibana-2'
curl -XDELETE 'http://127.0.0.1:9200/_template/.monitoring-kibana'
 
作ってしまった index の index.number_of_replicas を変更する。
<syntaxhighlight lang="bash" enclose="div">
curl -XPUT 'http://localhost:9200/.monitoring-es-6-2017.12.05/_settings' -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : "0"
    }
}
'
 
curl -XPUT 'http://localhost:9200/.monitoring-logstash-6-2017.12.05/_settings' -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : "0"
    }
}
'
 
curl -XPUT 'http://localhost:9200/.monitoring-kibana-6-2017.12.05/_settings' -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : "0"
    }
}
'
</syntaxhighlight>
 
Elastic Stack がアップデートされたら、いちいち明示的に X-Pack をアップデートしなければいけないのは萎える。
:6.3.0 から標準パッケージにはいったっぽい。
<syntaxhighlight lang="bash" enclose="div">
systemctl stop logstash
systemctl stop kibana
systemctl stop elasticsearch
cd /usr/share/logstash
./bin/logstash-plugin install logstash-output-statsd
./bin/logstash-plugin install logstash-filter-throttle
./bin/logstash-plugin install logstash-filter-sleep
systemctl start elasticsearch
systemctl start kibana
systemctl start logstash
</syntaxhighlight>
</syntaxhighlight>
を追加する。


Logstash の Monitoring はまだかな?


いろいろ触りどころがありそうなので一旦凍結する。
また、elasticsearch を(再)起動する時点で、kibana や logstash が稼働している場合に、monitor を突きにくるので、認証エラーが多発する。
これは無視してよいが気持ち悪い。
 
Authentication of [kibana] was terminated by realm [reserved] - failed to authenticate user [kibana]
Authentication of [logstash_system] was terminated by realm [reserved] - failed to authenticate user [logstash_system]


== LS 2.3.1 geoip issue ==
== LS 2.3.1 geoip issue ==
Line 505: Line 715:
== RLIMIT_MEMLOCK ==
== RLIMIT_MEMLOCK ==
systemd 配下の場合は /usr/lib/systemd/system/elasticsearch.service
systemd 配下の場合は /usr/lib/systemd/system/elasticsearch.service
<syntaxhighlight lang="text" enclose="div">
[service]
LimitMEMLOCK=infinity
</syntaxhighlight>
こっちで対処するのが良いそうだ。
<syntaxhighlight lang="bash" enclose="div">
systemctl edit elasticsearch
</syntaxhighlight>
このコマンドで、
/etc/systemd/system/elasticsearch.service.d/override.conf
ここに、
<syntaxhighlight lang="text" enclose="div">
<syntaxhighlight lang="text" enclose="div">
[service]
[service]
Line 516: Line 738:


== Elastic Stack 6.0.0 upgrade memo ==
== Elastic Stack 6.0.0 upgrade memo ==
"type" "_all" 等々がつかえない
"type" がつかえない
 
 
<syntaxhighlight lang="text" enclose="div">
*************************************************************************************
Found index level settings on node level configuration.


Since elasticsearch 5.x index level settings can NOT be set on the nodes
https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html
configuration like the elasticsearch.yaml, in system properties or command line
arguments.In order to upgrade all indices the settings must be updated via the
/${index}/_settings API. Unless all settings are dynamic all indices must be closed
in order to apply the upgradeIndices created in the future should use index templates
to set default values.
 
Please ensure all required values are updated on all indices by executing:
 
curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
  "index.mapping.single_type" : "true"
}'
*************************************************************************************
</syntaxhighlight>


statsd が標準プラグインから消えた。
statsd が標準プラグインから消えた。
Line 892: Line 1,096:
/var/log/kibana/kibana.stdout が肥大化していたので logrotate 設定した。
/var/log/kibana/kibana.stdout が肥大化していたので logrotate 設定した。
-->
-->
=== IPv6 address の抽出 ===
単純な方法があった。
<syntaxhighlight lang="text" enclose="div">
client_ip: *\:*
</syntaxhighlight>
:これだとマッチしないのがあるっぽいな。要調査
<syntaxhighlight lang="text" enclose="div">
client_ip: ["::" TO "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]
</syntaxhighlight>
:これでも抜けるな
::これかな? [https://github.com/elastic/elasticsearch/issues/29288 unable to CIDR match IPv4-mapped IPv6]
:::あちゃ。type ipになってなかった。もうすこしホゲる。
:::: type ip にしてみたが、うまくマッチする方法がみつからない。
どうやら type ip ではこれで良い。このアドレスはインターネットに広報してよいブロックのみ。これ以外は変えないといけない。
<syntaxhighlight lang="text" enclose="div">
client_ip: "2000::/3"
</syntaxhighlight>
ちなみに logstash だとこんなコードでマッチする。実運用では logstash 側のロジックで新しいフィールドつくった。
結局 type ip ならこれでよさそうだ。
<syntaxhighlight lang="text" enclose="div">
if [client_ip] =~ /:/ {
  # is IPv6, do something
  mutate {
    add_field => { "client_ipv6" => "%{client_ip}" }
  }
}
</syntaxhighlight>


== dashboard URL ==
== dashboard URL ==
Line 936: Line 1,169:
= Elasticsearch =
= Elasticsearch =
== 構造 ==
== 構造 ==
  http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/{ID:Document}
  curl -XGET 'http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/{ID:Document}'
  http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/{ID:Document}/_source
  curl -XGET 'http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/{ID:Document}/_source'
  http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/_mapping
  curl -XGET 'http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/_mapping'
  http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/_mapping?pretty
  curl -XGET 'http://127.0.0.1:9200/{INDEX}/{TYPE:mapping}/_mapping?pretty'


  http://127.0.0.1:9200/_search?q=tags:_grokparsefailure
  curl -XGET 'http://127.0.0.1:9200/_search?q=tags:_grokparsefailure'


  http://127.0.0.1:9200/_cluster/health?pretty
  curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty'
  http://127.0.0.1:9200/_template?pretty
  curl -XGET 'http://127.0.0.1:9200/_template?pretty'


  http://127.0.0.1:9200/kibana-int/_search?q=*:*&pretty
  curl -XGET 'http://127.0.0.1:9200/kibana-int/_search?q=*:*&pretty'
  http://127.0.0.1:9200/kibana-int/_search?q=*:*&size=100&pretty
  curl -XGET 'http://127.0.0.1:9200/kibana-int/_search?q=*:*&size=100&pretty'
  http://127.0.0.1:9200/kibana-int/_search/?size=100&pretty
  curl -XGET 'http://127.0.0.1:9200/kibana-int/_search/?size=100&pretty'


== 設定 ==
== 設定 ==
Line 985: Line 1,218:
</syntaxhighlight>
</syntaxhighlight>
:curator4 で大幅に書式変わった。
:curator4 で大幅に書式変わった。
Disk full で index がロックされた場合
<syntaxhighlight lang="bash" enclose="div">
curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}'
</syntaxhighlight>


  [https://github.com/lmenezes/elasticsearch-kopf kopf web admin interface for elasticsearch]
  [https://github.com/lmenezes/elasticsearch-kopf kopf web admin interface for elasticsearch]