LoginSignup
11
15

More than 3 years have passed since last update.

Grafana + Loki + FluentdでSyslog Viewerを作る

Last updated at Posted at 2020-05-08

SyslogをWebUIで検索・閲覧する用途に最近では一般的にElasticsearchを利用しているケースが多いのだと思う。ただ特にクラスタ構成など必要ない小規模構成でもっと手軽に構築できれるものがほしいなと常々思っていたので早速自宅環境に導入してみた。1
2020-05-08-140356_1330x932_scrot.png

Syslogの時系列ヒストグラム表示がある。SeverityやFacility、Hostnameでの絞り込みも可能。もちろん特定時間帯のログのみを抽出することもできる。

構成

server側で用意するのはclientからのsyslogを514/udpポートで受けて5514/tcpポートへ転送するためのRsyslogに加え、Grafana, Loki, Fluentdのみ。

                   +------------------------------------------------+
                   |                                                |
                   |   +---------+                         server   |
+--------+ 514/udp |   |         |                                  |
| client +------------->         |                                  |
+--------+         |   |         |                                  |
                   |   | rsyslog |                                  |
+--------+ 514/udp |   |         |                                  |
| client +------------->         |                                  |
+--------+         |   |         |                                  |
                   |   +----+----+                                  |
                   |        |                                       |
                   |        | 5514/tcp                              |
                   |        |                                       |
                   |   +----v-----+  +----------+  +------------+   |
                   |   | fluentd  +-->   loki   +-->  grafana   |   |
                   |   +----------+  +----------+  +------------+   |
                   |                                                |
                   +------------------------------------------------+

設定

Grafana, Lokiに加えて、Fluentdの安定配布版であるtd-agentをそれぞれ手順通りにインストール&サービス起動する。
Grafanaは3000/tcpで、 Lokiは3100/tcpで動作する。

sudo systemctl start grafana
sudo systemctl start loki
sudo systemctl start td-agent

またFluentdにはLokiへsyslogを転送するためのプラグインを追加インストールしておく必要がある。

sudo td-agent-gem install fluent-plugin-grafana-loki

Rsyslogはclientから514/udpポートで受け取ったsyslogを5514/tcpポートへ転送する。
/etc/rsyslog.d/50-forward.confファイルを作成し、以下の通り記述する。

$ModLoad imudp
$UDPServerRun 514

$ActionQueueType LinkedList # use asynchronous processing
$ActionQueueFileName srvrfwd # set file name, also enables disk mode
$ActionResumeRetryCount -1 # infinite retries on insert failure
$ActionQueueSaveOnShutdown on # save in-memory data if rsyslog shuts down

*.* @@127.0.0.1:5514;RSYSLOG_SyslogProtocol23Format

Fluentdは5514/tcpポートで受け取ったsyslogをLokiへ転送する。
/etc/td-agent/td-agent.confに以下を追記する。

<source>
  @type syslog
  @id syslog_in
  port 5514
  bind 0.0.0.0
  severity_key level
  facility_key facility
  tag syslog
  <transport tcp>
  </transport>
  <parse>
    message_format rfc5424
  </parse>
</source>

<match syslog.**>
  @type loki
  @id syslog_out
  url "http://localhost:3100"
  extra_labels {"app":"syslog"}
  <label>
    pid
    host
    level
    facility
  </label>
</match>

RsyslogとFluentdを再起動して設定を反映。

sudo systemctl restart rsyslog
sudo systemctl restart td-agent

以上で設定は終わり。非常に簡単。

操作方法

上記の設定により既にLokiにはログデータが蓄積されてはじめている。
http://<サーバーのIPアドレス>:3000/ へアクセスし、Grafanaを起動してConfiguration > Data SourcesからLokiを選択・追加し、URLには http://localhost:3100を記載する。

ExplorerをクリックしてData SourceにLokiを選択。Log labels{app="syslog"}を記載してRun queryをクリックするとSyslogデータが表示される。

2020-05-08-140444_1330x932_scrot.png

ちなみに以下のコマンドでダミーログを飛ばしてきちんと受信・表示されていることを確認できるので、何もログが表示されない場合はこれで切り分けを。

logger -n <サーバーのIPアドレス> -d test

Fluentd転送時にSeverityやFacility, Hostnameに基づいてラベル付けされているため、これらを用いてフィルタが可能。
またログメッセージの検索は{severity="info"} |= "session closed"のように、以下の記法を組み合わせて実施できる。

|= line contains string.
!= line doesn’t contain string.
|~ line matches regular expression.
!~ line does not match regular expression.

message検索など多少重さは感じるけど、対象時間帯を予め区切っておけばよいので別に問題はない。

以下は公式サイトからの引用。
こんな感じで色々絞り込みをしたり詳細の確認ができる。
絞り込み

楽しい!:v:


  1. 以前Chronograf + Telegraf + InfluxDBによるSyslog Viewerを作ったが、普段はGrafanaを使うことが多くSyslog ViewerのためだけにChronografをインストールするのもなぁと思っていた。 

11
15
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
11
15