LoginSignup
2
6

More than 3 years have passed since last update.

ネットワーク機器のログをsyslogサーバーに転送しlogrotateで世代管理する

Last updated at Posted at 2020-02-14

概要

Cisco Catalyst2960Xなどのネットワークスイッチのログをsyslogサーバーに転送し、syslogサーバー側では受信したログをlogrotateで世代管理する設定を行います。
またおまけでlogrotate用のファイルをpythonで生成するサンプルも記載します。

環境

想定環境は以下の通りです。
・Catalyst 2960X (WS-C2960X-48TD-L) IOS Version 15.2(4)E5
・CentOS Linux release 7.5.1804 (Core)
・logrotate-3.8.6-15.el7
・rsyslog-8.24.0-16.el7
・Python 2.7.5

IPアドレス
・スイッチ:192.168.1.1
・syslogサーバー:192.168.1.100

以下説明では上記環境が既に構築済であるとします。

Catalystスイッチ側の設定

Catalystスイッチにログイン後、特権モード、グローバルコンフィギュレーションモードに移行し
以下コマンドでsyslogサーバーへのログ転送を設定します。

switch1(config)#logging host 192.168.1.100

syslogサーバー側の設定

firewalldの設定

firewall-cmdにてsyslogサービス用ポートを開放します。
※ --add-service=syslogで開放するポートはudp514です。

# firewall-cmd --add-service=syslog --zone=public
# firewall-cmd --add-service=syslog --zone=public --permanent

rsyslogの設定

スイッチから送信されるログを /var/log/networkdevices 以下に、スイッチのIPアドレスのディレクトリ、
スイッチのIPアドレスのファイル名 + ".log"の形式で保存することを想定します。

以下設定を /etc/rsyslog.conf の #### TEMPLATES #### 以下に追加します。

#### TEMPLATES ####
$template RemoteHost,"/var/log/networkdevices/%fromhost%/%fromhost%.log"

また /var/log/networkdevices/ ディレクトリを作成しておきます。

# mkdir /var/log/networkdevices

logrotateの設定

/var/log/networkdevices/スイッチのIP/スイッチのIP.logファイルを圧縮、ローテーションさせるための設定を行います。

/etc/logrotate.d/以下にスイッチのIPを名前とするファイルを以下内容で作成します。

# pwd
/etc/logrotate.d

# vi 192.168.1.1
/var/log/networkdevices/192.168.1.1/192.168.1.1.log {
        daily
        rotate 31
        compress
        delaycompress
        missingok
        notifempty
        create 0664 root root
}

これにより192.168.1.1.logファイルは圧縮され、31世代でローテーションされるようになります。

おまけ

ipアドレスのリストが記載されたテキストファイルを1行ずつ読み込みlogrotate用の設定ファイルを生成するpythonプログラムの例です。

list.txt
192.168.1.1
192.168.2.1
・・・
sample01.py
#!/usr/bin/env python

f = open('list.txt', 'r')
line = f.readline()

while line:
        af = line.strip()

        file = '/etc/logrotate.d/' + af

        with open(file, 'w') as outp:
                outp.write("/var/log/networkdevices/" + af + "/" + af + ".log {\n")
                outp.write("\tdaily\n")
                outp.write("\trotate 31\n")
                outp.write("\tcompress\n")
                outp.write("\tdelaycompress\n")
                outp.write("\tmissingok\n")
                outp.write("\tnotifempty\n")
                outp.write("\tcreate 0664 root root\n")
                outp.write("}\n")

        line = f.readline()
f.close()
2
6
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
2
6