ショコラ
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.confrsyslog.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'