LoginSignup
0
0

More than 3 years have passed since last update.

受け取ったrsyslogの要素を出力する

Last updated at Posted at 2019-02-27

目的

rsyslogで受け取ったsyslogメッセージを他Appで解析する際になーんかうまく解析できねーぞと。
そのApp側で要素をdebug出力してくれればいいんだけど、開発チームと試験チーム、構成管理チームが別々だとそんな依頼すんのも面倒くさい。構成管理チームで一時解析してやろーじゃねーかと。

もしくは、どっかの誰かがうちのサーバにsyslogを送りつけてきやがる。犯人探したろ!みたいなとき。

環境

# cat /etc/redhat-release
CentOS release 6.6 (Final)

# rpm -qa rsyslog
rsyslog-5.8.10-10.el6_6.x86_64

実現方法

rsyslogのフォーマット(テンプレート)機能を使う。
デフォルトのフォーマット(テンプレート)には%HOSTNAME%はあるけど%fromhost-ip%が入ってない。不親切。

/etc/rsyslog.d/abcdefg.conf
$template template_out, "---------------\ntimestamp  :%TIMESTAMP%\nfromhost-ip:%fromhost-ip%\nhostname   :%HOSTNAME%\nfacility   :%syslogfacility-text%\nseverity   :%syslogseverity-text%\nsyslogtag  :%syslogtag%\nmsg         :%msg:::drop-last-lf%\n"
*.* /tmp/out.log;template_out

上記のように編集してrsyslogを再起動。

# service rsyslog restart
システムロガーを停止中:                                    [  OK  ]
システムロガーを起動中:                                    [  OK  ]
#

指定した「/tmp/out.log」をtailしてみる。

# tail -F /tmp/syslog_out.log
---------------
timestamp  :Mar 16 05:45:17
fromhost-ip:127.0.0.1
hostname   :localhost
facility   :daemon
severity   :info
syslogtag  :systemd[1]:
msg         :Started Session 6 of user root.
---------------
timestamp  :Mar 16 05:45:17
fromhost-ip:127.0.0.1
hostname   :localhost
facility   :auth
severity   :info
syslogtag  :systemd-logind[671]:
msg         :New session 6 of user root.
---------------
timestamp  :Mar 16 05:45:17
fromhost-ip:127.0.0.1
hostname   :localhost
facility   :authpriv
severity   :info
syslogtag  :sshd[9200]:
msg         :pam_unix(sshd:session): session opened for user root by (uid=0)

満足するまで確認したら/etc/rsyslog.d/abcdefg.confを消して# service rsyslog restartしておく。

ちなみに

良くある問い合わせが「loggerで送出したテストsyslogがうまく解析できない」なんて言われて確認すると
# logger -p user.err "test: hogehoge"なんて感じでシスログタグをmsgに入れ込んでたりする。
ホントによくある。人生でもう5回以上別の人から聞かれた。そんなに知らんもんかねみんな。

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