RSYSLOG についてまとめページ

ショコラ
ショコラ

RSYSLOG についてまとめページ

RSYSLOG について軽くまとめます。
ネットには新書式と旧書式の情報があるみたい。
SYSLOG には深い世界があるようです。
RSYSLOG のホームページです。
RedHat のページも日本語で分かり易いです。

もっさん先輩
もっさん先輩

新書式

新書式で書いた ommail.conf 設定ファイル

cat <<'EOF' > /etc/rsyslog.d/ommail.conf
module(load="ommail")
template(name="mailSubject" type="string" string="Syslog Warning")
template(name="mailBody" type="string" string="From:%fromhost%\r\n%msg%")
if (($syslogtag contains "postgres")
  and (($msg contains "FATAL") or (($msg contains "ERROR")))) then {
  action(type="ommail"
    server="localhost"
    port="25"
    mailfrom="syslog@answorz.com"
    mailto=["alert@answorz.com"]
    subject.template="mailSubject"
    action.execonlyonceeveryinterval="1")
}
EOF

旧書式

旧書式で書いた ommail.conf 設定ファイル

cat <<'EOF' > /etc/rsyslog.d/ommail.conf
$ModLoad ommail
$template mailSubject,"Syslog Warning"
$template mailBody,"From:%fromhost%\r\n%msg%"
$ActionMailSMTPServer localhost
$ActionMailSMTPPort 25
$ActionMailFrom syslog@answorz.com
$ActionMailTo alert@answorz.com
$ActionMailSubject mailSubject
$ActionExecOnlyOnceEveryInterval 1
if ($syslogtag contains 'postgres')
 and (($msg contains 'FATAL') or ($msg contains 'ERROR')) then {
 :ommail:;mailBody
}
EOF

まとめ

設定ファイルで使える主な変数(プロパティ)。

文字列の中に展開するには「”%msg%”」と書きます。変数(プロパティ)についての説明はRSyslogのページがいいかな。

logger -i -p local0.debug -t postgres '[FATAL]ommail test'

例えば↑の loggerコマンドの場合、↓以下のように変数に設定されます。

変数説明値(例)
$msgメッセージ[FATAL]ommail test
$programnameタグpostgres
$syslogtagタグにコロンが付く
※-iオプションでプロセス番号が付く
postgres[8552]:
$syslogfacilityファシリティ数値16
$syslogfacility-textファシリティlocal0
$syslogseverityプライオリティ数値7
$syslogseverity-textプライオリティdebug
$fromhostメッセージを受信したシステムのホスト名localhost
$hostnameログを出力したホストlocalhost

IF文も使える。

if ($syslogtag contains 'postgres')
 and (($msg contains 'FATAL') or ($msg contains 'ERROR')) then {
 :ommail:;mailBody
}
else {
}

==、!=、<=、>=、+、-、*、/、%、and、or、not、!等も使える。&で文字列結合。

比較オペレーション

演算子説明SQL的な
contains含む$a contains “b”a like ‘%b%’
isequal一致$a isequal “b”a = ‘b’
startswith前方一致$a startswith “b”a like ‘b%’
regex正規表現$a eregex “b”a ~ ‘b’
eregex正規表現$a eregex “b”a ~ ‘b’

! を使って !contains等で否定できる。

コメントアウト

#action(type="omfile" file="/var/log/log1.log")
/*
action(type="omfile" file="/var/log/log2.log")
*/

action(type=”omfile” file=”{ログファイル}”) で ログファイルにログを書き込める

例えば3つのファイルにログが書き込めます。

action(type="omfile" file="/var/log/log1.log")
action(type="omfile" file="/var/log/log2.log")
action(type="omfile" file="/var/log/log3.log")

プロパティベースのフィルタ機能

IF文を→「:{変数}, {比較オペレーション}, {文字列}」で書ける。

:msg,contains,"error1"
action(type="omfile" file="/var/log/log1.log")

:msg,contains,"error1"
:msg,contains,"error2"
action(type="omfile" file="/var/log/log2.log")

action(type="omfile" file="/var/log/log3.log")

if文で書き直すと↓こんな感じです。

if $msg contains "error1" then action(type="omfile" file="/var/log/log1.log")
if (($msg contains "error1") and ($msg contains "error2")) then {
  action(type="omfile" file="/var/log/log2.log")
}
action(type="omfile" file="/var/log/log3.log")

ファシリティ local0 のログファイルを作成します。

echo 'local0.* /var/log/postgres.log' > /etc/rsyslog.d/postgres.conf

rsyslog.conf のチェックします。

rsyslogd -N 1

エラーだと Segmentation fault になりました。

# rsyslogd -N 1
rsyslogd: version 8.24.0-55.el7, config validation run (level 1), master config /etc/rsyslog.conf
Segmentation fault (コアダンプ)

成功すると↓こんな感じです。

# rsyslogd -N 1
rsyslogd: version 8.24.0-55.el7, config validation run (level 1), master config /etc/rsyslog.conf
rsyslogd: End of config validation run. Bye.

ホストの rsyslog を再起動します。

systemctl restart rsyslog 

loggerコマンドでメールが送られるかテストします。

logger -i -p local0.debug -t postgres '[FATAL]ommail test'
Scroll to Top