Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Shipper Platform Beats

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Data Shipper Platform Beats

Avatar for Hiroki Takeda

Hiroki Takeda

March 24, 2016
Tweet

More Decks by Hiroki Takeda

Other Decks in Technology

Transcript

  1. バックエンドとしてElasticsearch + Kibanaを前提とした データ収集ツール データ可視化プラットフォームにおける位置付け 4 収集 ・Beats ・Logstash ・Fluentd

    ・Flume etc... 蓄積 可視化 ・Elasticsearch ・Graphite ・InfluxDB ・Solr etc... ・Kibana ・Grafana ・Giraffe ・Influga ・Banana etc...
  2. どんなデータが収集できるの? 現在Elastic社から公開されているBeatは4つ ✓ Packetbeat … パケット情報収集 ✓ Topbeat … リソース情報収集

    ✓ Filebeat … ログファイル収集 ✓ Winlogbeat … Windowsイベントログ収集 OSSのコミュニティでも様々なBeatが公開 https://www.elastic.co/guide/en/beats/libbeat/current/community-beats.html 5
  3. 特徴 ✓ バックエンドとしてElasticsearch + Kibanaを想定 ✓ 1Beat 1Binary ✓ シンプルで軽い

    ✓ Go製 • マルチプラットフォーム • 別途ランタイム(JVMとか)のインストール不要 ✓ 設定ファイルはYAML形式 6
  4. ① インストール 1. 公開鍵追加 2. リポジトリ追加 3. インストール ※deb, rpm,

    tgz, zip形式でも公開されているので、 Windowsの場合はzipを落としましょう。 https://www.elastic.co/guide/en/beats/topbeat/1.1/topbeat-installation.html 10 rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch [beats] name=Elastic Beats Repository baseurl=https://packages.elastic.co/beats/yum/el/$basearch enabled=1 gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch gpgcheck=1 yum install topbeat /etc/yum.repos.d/
  5. ② 設定 1. データ転送先のElasticsearch指定 11 output: ### Elasticsearch as output

    elasticsearch: hosts: ["localhost:9200"] /etc/topbeat/topbeat.yml
  6. 21

  7. ③ 構成 最低限実装すべきは下記の赤字 33 winappbeat │ main.go │ main_test.go │

    Makefile │ README.md │ winappbeat.yml … 設定ファイル ├─beater │ winappbeat.go … メインロジック ├─config │ config.go … 設定ファイルに対応する構造体 ├─docs │ index.asciidoc ├─etc │ beat.yml │ fields.yml │ winappbeat.template.json └─tests └─system │ requirements.txt │ test_base.py │ winappbeat.py └─config winappbeat.yml.j2
  8. ④ 設定ファイル作成 1. YAML形式で設定項目を記述 今回は ・ データの取得タイミング ・ 名称がないウィンドウの情報を取得するか? ・

    不可視ウィンドウの情報を取得するか? を設定項目として定義 34 input: # Defines how often an event is sent to the output period: 30s # Whether to get only window that has name onlyNotEmpty: true # Whether to get only visible window onlyVisible: true output: elasticsearch: hosts: ["10.23.46.5:9200"] winappbeat.yml
  9. ⑤ 設定ファイルに対応する構造体を実装 1. 設定ファイルと対になる構造体を実装 35 package config type Config struct

    { Winappbeat WinappbeatConfig } type WinappbeatConfig struct { Period string `yaml:"period"` OnlyNotEmpty string `yaml:"onlyNotEmpty"` OnlyVisible string `yaml:"onlyVisible"` } config/config.go
  10. ⑤ データ取得・転送ロジックを実装 libbeatのbeaterインターフェースで定義されている5メソッドを実装 ✓ Config … 設定ファイルを扱う ✓ Setup …

    初期処理を行う ✓ Run … データ収集のメイン処理を行う ✓ Cleanup … 終了処理を行う ✓ Stop … 停止要求があった場合の処理を行う 36 // Beater interface that every beat must use type Beater interface { Config(*Beat) error Setup(*Beat) error Run(*Beat) error Cleanup(*Beat) error Stop() } libbeat/beat/beat.go
  11. ⑤ データ取得・転送ロジックを実装 1. Config 設定ファイルを読込み 37 func (bt *Winappbeat) Config(b

    *beat.Beat) error { // Load beater configuration err := cfgfile.Read(&bt.Configuration, "") if err != nil { return fmt.Errorf("Error reading config file: %v", err) } return nil } beater/winappbeat.go
  12. ⑤ データ取得・転送ロジックを実装 2. Setup 設定ファイルの値がセットされていない場合のデフォルト値をセット 38 func (bt *Winappbeat) Setup(b

    *beat.Beat) error { // Setting default period if not set if bt.Configuration.Winappbeat.Period == "" { bt.Configuration.Winappbeat.Period = "30s" } var err error bt.period, err = time.ParseDuration(bt.Configuration.Winappbeat.Period) if err != nil { return err } // 省略(他の設定値も同様にデフォルト値をセット) return nil } beater/winappbeat.go
  13. ⑤ データ取得・転送ロジックを実装 3. Run(詳細は割愛) 設定ファイルで指定した間隔でループ処理 eventオブジェクトにデータを設定し、PublishEventで送信 39 func (bt *Winappbeat)

    Run(b *beat.Beat) error { ticker := time.NewTicker(bt.period) for { select { case <-bt.done: return nil case <-ticker.C: } list := listWindows(win.HWND(0)) for _, w := range list { if bt.onlyNotEmpty && w.name == "" { continue } if bt.onlyVisible && !w.visible { continue } event := common.MapStr{ "@timestamp": common.Time(time.Now()), "type": b.Name, "windowname": w.name, "windowcategory": w.nameCategory, "windowclass": w.class, } b.Events.PublishEvent(event) } } } beater/winappbeat.go
  14. ⑤ データ取得・転送ロジックを実装 4. Cleanup 特に処理なし 5. Stop チャネルをクローズして、Runメソッドの処理を終了 40 func

    (bt *Winappbeat) Cleanup(b *beat.Beat) error { return nil } beater/winappbeat.go func (bt *Winappbeat) Stop() error { close(bt.done) } beater/winappbeat.go
  15. 一通り使ってみて、作ってみての所感 ◎ Go言語実装 ✓ クロスコンパイルできるので展開しやすい ✓ OSネイティブな実行ファイルが作られるので別途ランタイムは不要 ◎ 個々のBeatsが「小さく」「疎」である ✓

    必要なものだけを組み合わせて使う ◎ Elasticsearch + Kibanaとの連携が容易 △ Elasticsearch以外とも連携したい。 (厳密にはElasticsearch, Logstash, File, Console, Redisは連携可能) △ 設定ファイルレベルでのフィルタやパースができない 44