0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ZABBIX + Fluentd + mongo ZABBIXのイベントログをMongoDBに入れる

Last updated at Posted at 2020-06-04

やりたいこと

他システムにZABBIXの警報ログを参照させたい。
でも、いちいちZABBIX APIを叩くのは嫌。
じゃあ、警報のログをMongoDBなり、ElasticSearchなりに格納して、他システムはそれを参照したらWin-Winじゃん、というアプローチ。

前提条件

  • ZABBIX 4.0+ (realtime exportが使えないから)

試した環境

  • CentOS 8.1.1911
  • ZABBIX 5.0
  • Fluentd 1.10.2
  • MongoDB 4.2.7

ZABBIXの設定

/etc/zabbix/zabbix_server.conf

### Option: ExportDir
#       Directory for real time export of events, history and trends in newline delimited JSON format.
#       If set, enables real time export.
#
# Mandatory: no
# Default:
ExportDir=/path/to/dir/ #<--なんか適当なディレクトリ

設定を読み込むため再起動

systemctl restart zabbix-server

ExportDirで指定したディレクトリの配下に色々Jsonファイルが出来ているはず。

Fluentd

ruby全然分からないのでツッコミ歓迎。。

プラグインを入れる

その前に必要なものがまるで無かったので。。

yum install -y ruby ruby-devel gcc rpm-build
td-agent-gem install fluent-plugin-mongo

余談
↓これだとプラグインを認識してくれなかった。なんで??

gem install fluent-plugin-mongo

コンフィグ修正

/etc/td-agent/td-agent.conf
<source>
  @type tail
  path /path/to/dir/*
  # 無視するファイル
  exclude_path ["/path/to/dir/history-*", "/path/to/dir/trends-*"]
  # ファイルを読込ポジションを保持するファイル
  pos_file /var/lib/td-agent/ps/zabbix-problems-history-syncer.ndjson.pos
  <parse>
    @type json
  </parse>
  tag mongo.zabbix.problems
</source>

<match mongo.zabbix.**>
  # plugin type
  @type mongo
  # mongodb db + collection
  database zabbix
  collection problems

  # mongodb host + port
  host localhost
  port 27017
  # interval
  <buffer>
    #// MongoDBに入れるまで2秒保持する↓
    # flush_interval 2s
    #// それだとちょっと遅いので、即書き込みするようにする
    flush_mode immediate
  </buffer>
</match>

できたら、再起動

systemctl restart td-agent

確認

mongo zabbix
> db["problems"].find();
{ "_id" : ObjectId("5ed89f75c9d8c969150e8a09"), "clock" : 1591254836, "ns" : 969998197, "value" : 1, "eventid" : 155, "name" : "Zabbix agent is not available (for 3m)", "hosts" : [ { "host" : "hoge host", "name" : "hoge host" } ], "groups" : [ "Linux servers" ], "tags" : [ ], "time" : ISODate("2020-06-04T07:13:57.542Z") }
{ "_id" : ObjectId("5ed8a0dac9d8c96e9ba1e773"), "clock" : 1591255256, "ns" : 198390872, "value" : 0, "eventid" : 159, "p_eventid" : 155, "time" : ISODate("2020-06-04T07:20:56.963Z") }

でけた。

さいごに

今回はイベントを詰め込んだけど、ヒストリデータもJSONとして吐き出しているので、そっちの方が需要があるかもしれない。
ZABBIX 5.0からElasticSearchにヒストリデータを送れるし、そっちの方が設定は簡単そうだけど。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?