0
0

Fluentdを一番シンプルに試す

Last updated at Posted at 2023-12-10

やりたいこと

  • Fluentdを設定し、外部のサーバからのログを受け付けるようにする

検証環境

  • VagrantでホスティングしたUbuntu Server 22.04 LTS

インストール

  • makeやruby-devがインストールされていないとFluentdのインストールに失敗するため注意
$ sudo apt update
$ sudo apt install make -y
$ sudo apt install ruby-dev -y
$ sudo gem install fluend

設定する

  • リモートからログを受け取るための設定
  • TCPポート24224番で待ち受ける設定
  • 受け取ったログを標準出力に出力する設定
$ vim fluent.conf 
<source>
  @type forward
  port 24224
</source>

<match **>
  @type stdout
</match>
  • UFWを有効化する設定
  • TCPポート24424番への外部からのアクセスを許可する設定
$ sudo ufw status
Status: inactive

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

$ sudo ufw allow 24224/tcp
Rule added
Rule added (v6)

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
24224/tcp                  ALLOW       Anywhere                  
24224/tcp (v6)             ALLOW       Anywhere (v6)

起動する

$ fluentd -c ./fluent.conf

動作確認

  • 擬似的にログを送信するコードをPythonで作成する
$ pip install fluent-logger
client.py
from fluent import sender
from fluent import event

fluentd_host = "192.168.56.10"
fluentd_port = 24224
tag = "my.tag"

sender.setup(tag, host=fluentd_host, port=fluentd_port)

data = {"message": "hello, world!"}
event.Event(tag, data)
$ python client.py
  • 上記のコードを実行すると、Fluentdの標準出力にログが流れてくることが確認できる
$ fluentd -c ./fluent.conf
...
省略
...
2023-12-10 05:54:50.000000000 +0000 my.tag.my.tag: {"message":"hello, world!"}
0
0
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
0