ネットワーク機器向けLokiログ収集基盤の構築

ネットワークスイッチのSyslogをLokiで可視化するための環境構築手順を解説します。Promtailとrsyslogdを連携させ、Grafana経由で検索・分析可能なログ基盤を構築します。

前提パッケージのインストール

yum install -y nano net-tools unzip sysstat iotop rsyslog iperf3
yum install -y grafana-enterprise-10.1.0-1.x86_64.rpm
yum install -y loki-2.8.4.x86_64.rpm promtail-2.8.4.x86_64.rpm

サービスの有効化と起動

systemctl enable --now grafana-server
systemctl enable --now loki
systemctl enable --now promtail

rsyslog設定(/etc/rsyslog.conf)

# 基本モジュール読み込み
$ModLoad imuxsock
$ModLoad imudp
$ModLoad imtcp

# UDP/TCP 514ポートで受信
$UDPServerRun 514
$InputTCPServerRun 514

# 作業ディレクトリ指定
$WorkDirectory /var/spool/rsyslog
$IncludeConfig /etc/rsyslog.d/*.conf

スイッチ用ログ保存ルール(/etc/rsyslog.d/switch-logs.conf)

$CreateDirs on
$Umask 0022

# ディレクトリ構造: IPアドレス別 + 日付ファイル
$template NetDevicePath, "/var/log/network/%fromhost-ip%/%fromhost-ip%_%$NOW%.log"

# 生ログをそのまま保存(改行追加)
$template RawLogFormat, "%rawmsg%\n"

# ローカル以外のホストからのログを保存
if ($fromhost-ip != '127.0.0.1' and $fromhost-ip != '::1') then {
    action(
        type="omfile"
        dynafile="NetDevicePath"
        template="RawLogFormat"
        fileCreateMode="0644"
        dirCreateMode="0755"
    )
    stop
}

Loki設定(/etc/loki/local-config.yaml)

auth_enabled: false

server:
  http_listen_port: 3100

common:
  path_prefix: /var/lib/loki
  storage:
    filesystem:
      directory: /var/lib/loki/chunks
  ring:
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: "2023-01-01"
      index:
        period: 24h
        prefix: loki_index_
      object_store: filesystem
      schema: v13
      store: tsdb

storage_config:
  tsdb_shipper:
    active_index_directory: /var/lib/loki/tsdb-index
    cache_location: /var/lib/loki/tsdb-cache

Promtail設定(/etc/promtail/config.yaml)

server:
  http_listen_port: 9080

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: network_devices
  static_configs:
  - targets: [localhost]
    labels:
      app: switches
      __path__: /var/log/network/*/*.log
  pipeline_stages:
  - regex:
      expression: '.*/(?P<device_ip>[\d\.]+)/.*_(?P<capture_day>\d{4}-\d{2}-\d{2})\.log'
  - labels:
      device_ip: ""
      capture_day: ""
  - timestamp:
      source: captured_at
      format: RFC3339
      fallback_to_current_time: true

Grafanaでのダッシュボード設定

GrafanaにログデータソースとしてLokiを追加後、コミュニティテンプレートID 13639 をインポートして可視化ダッシュボードを展開します。

タグ: loki promtail rsyslog grafana network-logging

7月21日 02:30 投稿