Rsyslog簡介
- Rsyslog的全稱是 rocket-fast system for log,它提供了高性能,高安全功能和模塊化設計。rsyslog能夠接受從各種各樣的來源,將其輸入,輸出的結果到不同的目的地。rsyslog可以提供超過每秒一百萬條消息給目標文件。
- 日志是任何軟件或操作系統的關鍵組件。 日志通常會記錄用戶的操作、系統事件、網絡活動等等,具體取決於它們的用途。 Linux 系統上使用最廣泛的日志系統之一是 rsyslog 。
- Rsyslog 是一個強大、安全和高性能的日志處理系統,它接受來自不同類型源(系統/應用程序)的數據並輸出為多種格式。
- 它已經從一個常規的 syslog 守護進程發展成為一個功能齊全的企業級日志系統。 它采用客戶端/服務器模型設計,因此可以配置為客戶端和/或其他服務器、網絡設備和遠程應用程序的中央日志服務器。
功能特性
- 多線程
- 可以通過許多協議進行傳輸UDP,TCP,SSL,TLS,RELP;
- 直接將日志寫入到數據庫;
- 支持加密協議:ssl,tls,relp
- 強大的過濾器,實現過濾日志信息中任何部分的內容
- 自定義輸出格式
Rsyslog提供了三種遠程傳輸協議
- UDP 傳輸協議
基於傳統UDP協議進行遠程日志傳輸,也是傳統syslog使用的傳輸協議; 可靠性比較低,但性能損耗最少, 在網絡情況比較差, 或者接收服務器壓力比較高情況下,可能存在丟日志情況。 在對日志完整性要求不是很高,在可靠的局域網環境下可以使用。
- TCP 傳輸協議
基於傳統TCP協議明文傳輸,需要回傳進行確認,可靠性比較高; 但在接收服務器宕機或者兩者之間網絡出問題的情況下,會出現丟日志情況。 這種協議相比於UDP在可靠性方面已經好很多,並且rsyslog原生支持,配置簡單, 同時針對可能丟日志情況,可以進行額外配置提高可靠性,因此使用比較廣。
- RELP 傳輸協議
RELP(Reliable Event Logging Protocol)是基於TCP封裝的可靠日志消息傳輸協議; 是為了解決TCP 與 UDP 協議的缺點而在應用層實現的傳輸協議,也是三者之中最可靠的。 需要多安裝一個包rsyslog-relp以支持該協議。
Rsyslog架構闡述:
環境准備
屬性 | Rsyslog服務器 | Rsyslog客戶端 |
節點 | rsyslog-server | rsyslog-client |
系統 | CentOS Linux release 7.5.1804 (Minimal) | CentOS Linux release 7.5.1804 (Minimal) |
內核 | 3.10.0-862.el7.x86_64 | 3.10.0-862.el7.x86_64 |
SELinux | setenforce 0 | disabled | setenforce 0 | disabled |
IP地址 | 172.16.70.186 | 172.16.70.187 |
Rsyslog服務端/客戶端前提設置。
[root@rsyslog-server ~] # rpm -qa | grep rsyslog
[root@rsyslog-server ~] # yum install -y rsyslog
[root@rsyslog-server ~] # rsyslogd -v # 查看版本
rsyslogd 8.24.0-57.el7_9, compiled with:
PLATFORM: x86_64-redhat-linux-gnu
PLATFORM (lsb_release -d):
FEATURE_REGEXP: Yes
GSSAPI Kerberos 5 support: Yes
FEATURE_DEBUG (debug build, slow code): No
32bit Atomic operations supported: Yes
64bit Atomic operations supported: Yes
memory allocator: system default
Runtime Instrumentation (slow code): No
uuid support: Yes
Number of Bits in RainerScript integers: 64
See http: //www .rsyslog.com for more information.
[root@rsyslog-server ~] # rpm -qc rsyslog # 相關配置文件
/etc/logrotate .d /syslog
/etc/rsyslog .conf
/etc/sysconfig/rsyslog
[root@rsyslog-server ~] # setenforce 0
[root@rsyslog-server rsyslog] # getenforce
Permissive
# 防火牆允許tcp/udp的514端口
[root@rsyslog-server ~] # firewall-cmd --permanent --add-port=514/udp
[root@rsyslog-server ~] # firewall-cmd --permanent --add-port=514/tcp
[root@rsyslog-server ~] # firewall-cmd --reload
[root@rsyslog-server ~] # firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports: 514 /tcp 514 /udp
protocols:
masquerade: no
forward-ports:
source -ports:
icmp-blocks:
rich rules:
默認/etc/rsyslog.conf詳解。
注:rsyslog官方文檔:https://www.rsyslog.com/doc/v8-stable/index.html
# rsyslog v5 configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES #### # 模塊
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command) # 支持本地系統日志記錄(例如通過logger命令)
$ModLoad imjournal # provides access to the systemd journal # 提供對systemd日志的訪問
#$ModLoad imklog # provides kernel logging support (previously done by rklogd) # 提供內核日志記錄支持(以前由rklogd完成)
#$ModLoad immark # provides --MARK-- message capability # 提供了--MARK--消息功能
# Provides UDP syslog reception # 提供UDP系統日志接收
#$ModLoad imudp
#$UDPServerRun 514
# Provides TCP syslog reception # 提供TCP系統日志接收
#$ModLoad imtcp
#$InputTCPServerRun 514
#### GLOBAL DIRECTIVES #### # 全局設置
# Where to place auxiliary files ; 輔助文件路徑
$WorkDirectory /var/lib/rsyslog
# Use default timestamp format # 使用默認的時間戳格式
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# File syncing capability is disabled by default. This feature is usually not required, # 文件同步功能默認是關閉的。這個功能通常不是必需的
# not useful and an extreme performance hit
#$ActionFileEnableSync on
# Include all config files in /etc/rsyslog.d/ # 將所有配置文件包含在/etc/rsyslog.d/中
$IncludeConfig /etc/rsyslog .d/*.conf
# Turn off message reception via local log socket; # 關閉本地日志消息,使用imjournal
# local messages are retrieved through imjournal now.
$OmitLocalLogging on
# File to store the position in the journal # 存放日志文件位置
$IMJournalStateFile imjournal.state
#### RULES #### # 規則
# Log all kernel messages to the console. # 將所有內核消息記錄到控制台
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher. # 記錄級別信息或更高級別的任何信息(郵件除外)
# Don't log private authentication messages! # 不要記錄私人認證信息!
*.info;mail.none;authpriv.none; cron .none /var/log/messages
# 記錄info級別的日志信息 排除mail authpriv cron
# The authpriv file has restricted access. # authpriv文件具有受限制的訪問權限。
authpriv.* /var/log/secure
# Log all the mail messages in one place. # 將所有的郵件記錄在一個地方。
mail.* - /var/log/maillog
# -/var/log/maillog 前面"-"是:代表異步寫入,郵件量大占I/O在系統不忙時做
# Log cron stuff # 日志計划任務的東西
cron .* /var/log/cron
# Everybody gets emergency messages # 每用戶都得到緊急消息
*.emerg *
# Save news errors of level crit and higher in a special file. # 保存級別更高的錯誤信息的文件
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log # 將啟動信息保存到boot.log 只記錄當前系統啟動日志
local7.* /var/log/boot .log
# ### begin forwarding rule ### # 開始轉發規則
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ### 轉發規則結束
情景一:收集主機的crond,history的日志轉發到rsyslog服務端上做備份。
步驟1.Rsyslog服務端操作
[root@rsyslog-server ~] # cp /etc/rsyslog.conf /etc/rsyslog.conf_bak
[root@rsyslog-server ~] # cat /etc/rsyslog.conf
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
# 開啟udp 514
$ModLoad imudp
$UDPServerRun 514
# 開啟tcp 514
$ModLoad imtcp
$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog .d/*.conf
$OmitLocalLogging on
$IMJournalStateFile imjournal.state
#### RULES ####
*.info;mail.none;authpriv.none; cron .none /var/log/messages
authpriv.* /var/log/secure
mail.* - /var/log/maillog
cron .* /var/log/cron
*.emerg :omusrmsg:*
uucp,news.crit /var/log/spooler
local7.* /var/log/boot .log
# 新增內容
$template local1_path, "/data/rsyslog/%FROMHOST-IP%/history/%$YEAR%-%$MONTH%-%$DAY%.log"
if $fromhost-ip != '127.0.0.1' and $syslogfacility-text == 'local1' then ?local1_path
& ~
$template remote_path, "/data/rsyslog/%FROMHOST-IP%/%syslogfacility-text%/%$YEAR%-%$MONTH%-%$DAY%.log"
if $fromhost-ip != '127.0.0.1' then ?remote_path
& ~
# 驗證配置文件是否正確
[root@rsyslog-server ~] # rsyslogd -f /etc/rsyslog.conf -N8 //其中,-N后面的數值代表rsyslog的版本。
配置解析:
格式::
日志設備(類型).(連接符號)日志級別 日志處理方式(action)
1.日志級別
debug 有調式信息的,日志信息最多
info 一般信息的日志,最常用
notice 最具有重要性的普通條件的信息
warning 警告級別
err 錯誤級別,阻止某個功能或者模塊不能正常工作的信息
crit 嚴重級別,阻止整個系統或者整個軟件不能正常工作的信息
alert 需要立刻修改的信息
emerg 內核崩潰等嚴重信息
none 什么都不記錄
local 1~7 自定義的日志設備
從上到下,級別從低到高,記錄的信息越來越少
2.連接符號
.xxx :表示大於等於xxx級別的信息
.=xxx:表示等於xxx級別的信息
.!xxx:表示在xxx之外的等級的信息
3.Actions
記錄到普通文件或設備文件:
. /var/log/file .log # 絕對路徑
. /dev/pts/0
logger 命令用於產生日志: logger -p local3.info 'KadeFor is testing the rsyslog and logger'
轉發到遠程::
. @172.16.0.1 # 使用UDP協議轉發到172.16.0.1的514(默認)端口
. @@172.16.0.1:10514 # 使用TCP協議轉發到172.16.0.1的10514(默認)端口
發送給用戶(需要在線才能收到)::
. root
. root,kadefor,up01 # 使用,號分隔多個用戶
. * # *號表示所有在線用戶
忽略,丟棄::
local3.* ~ # 忽略所有local3類型的所有級別的日志
執行腳本::
local3.* ^ /tmp/a .sh # ^號后跟可執行腳本或程序的絕對路徑
# 日志內容可以作為腳本的第一個參數. 可用來觸發報警
注意: 日志記錄的順序有先后關系!
自定義模板
$template定義了兩個模板,名稱分別為local1_path和remote_path,模板名稱后面指定了將消息保存到的日志文件的路徑。
if 開頭的指令是基於表達式的過濾器。第一條 if 指令的意思是,如果日志消息的來源IP地址不是本機,且消息的syslogfacility-text為local1,那么將這條消息寫入由模板local1_path對應的文件。
& ~的含義是讓滿足上面過濾器條件的消息不再匹配后續的規則。默認情況下,日志消息會按順序匹配rsyslog.conf中的每條規則。& ~在這里會讓消息跳過后面的規則,這樣這條消息就不會再被寫入其他的日志文件了。
第二條 if 開頭的指令將來自於本機之外的所有日志消息(由於前面的& ~,滿足這個條件的只有auth、authpriv和 cron 消息)寫入remote_path所對應的文件。
[root@rsyslog-server ~] # systemctl restart rsyslog
[root@rsyslog-server ~] # netstat -auntpl | grep rsyslog
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 1351 /rsyslogd
tcp6 0 0 :::514 :::* LISTEN 1351 /rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0:* 1351 /rsyslogd
udp6 0 0 :::514 :::* 1351 /rsyslogd
[root@rsyslog-server ~] # mkdir -p /data/rsyslog/
[root@rsyslog-server ~] # ls -ld /data/rsyslog/
drwxr-xr-x. 2 root root 6 Jun 9 10:06 /data/rsyslog
步驟2.rsyslog客戶端操作
root@rsyslog-client ~] # cp /etc/rsyslog.conf /etc/rsyslog.conf_bak
# 在/etc/rsyslog.conf最尾行添加內容
[root@rsyslog-client ~] # sed -i '$acron.* @@172.16.70.186:514\nlocal1.notice @@172.16.70.186:514' /etc/rsyslog.conf
[root@rsyslog-client ~] # tail -2 /etc/rsyslog.conf
cron .* @@172.16.70.186:514
local1.notice @@172.16.70.186:514
[root@rsyslog-client ~] #rsyslogd -f /etc/rsyslog.conf -N8
# 在/etc/crontab最尾行添加內容
[root@rsyslog-client ~] # sed -i '$a* * * * * root ls -l /etc /var/log' /etc/crontab
[root@rsyslog-client ~] # tail -1 /etc/crontab
* * * * * root ls -l /etc /var/log
# 在/etc/profile最尾行添加內容
[root@rsyslog-client ~] # tail -2 /etc/profile
export HISTTIMEFORMAT
export PROMPT_COMMAND= '{ command=$(history 1 | { read x y; echo $y; }); logger -p local1.notice -t bash -i "user=$USER,ppid=$PPID,from=$SSH_CLIENT,pwd=$PWD,command:$command"; }'
[root@rsyslog-client ~] # source /etc/profile
[root@rsyslog-client ~] # systemctl restart crond rsyslog
# history Server端驗證作用
[root@rsyslog-client ~] # ls -l /etc/
[root@rsyslog-client ~] # ls -la /var/log/
步驟3.rsyslog服務端驗證
[root@rsyslog-server ~] # cd /data/rsyslog/
[root@rsyslog-server rsyslog] # tree ./
./
└── 172.16.70.187
├── cron
│ └── 2021-06-11.log
└── history
└── 2021-06-11.log
若無生成log,Server端,Client端各重啟幾遍rsyslog即可。
[root@rsyslog-server rsyslog] # tail 172.16.70.187/cron/2021-06-11.log
Jun 11 10:48:01 rsyslog-client CROND[9085]: (root) CMD ( ls -l /etc /var/log )
Jun 11 10:49:01 rsyslog-client CROND[9090]: (root) CMD ( ls -l /etc /var/log )
Jun 11 10:50:01 rsyslog-client CROND[9095]: (root) CMD ( ls -l /etc /var/log )
Jun 11 10:51:01 rsyslog-client CROND[9100]: (root) CMD ( ls -l /etc /var/log )
[root@rsyslog-server rsyslog] # tail 172.16.70.187/history/2021-06-11.log
Jun 11 10:54:07 rsyslog-client bash [9158]: user=root,ppid=1287,from=172.16.70.187 11930 22, pwd = /root , command : ls -l /etc/
Jun 11 10:54:14 rsyslog-client bash [9163]: user=root,ppid=1287,from=172.16.70.187 11930 22, pwd = /root , command : ls -la /var/log/
情景二:收集主機的應用服務的日志轉發到rsyslog服務端上做備份。
步驟1.Rsyslog服務端
[root@rsyslog-server ~] # cat /etc/rsyslog.conf
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog .d/*.conf
$OmitLocalLogging on
$IMJournalStateFile imjournal.state
#### RULES ####
*.info;mail.none;authpriv.none; cron .none /var/log/messages
authpriv.* /var/log/secure
mail.* - /var/log/maillog
cron .* /var/log/cron
*.emerg :omusrmsg:*
uucp,news.crit /var/log/spooler
local7.* /var/log/boot .log
$template HttpAccess, "%msg%\n"
$template AccessFile, "/data/rsyslog/%FROMHOST-IP%/httpd/http-access.log"
$template HttpError, "%msg%\n"
$template ErrorFile, "/data/rsyslog/%FROMHOST-IP%/httpd/http-error.log"
user.* ?AccessFile;HttpAccess
user.* ?ErrorFile;ErrorFile
[root@rsyslog-server ~] # systemctl restart rsyslog
[root@rsyslog-server ~] # mkdir /data/rsyslog
步驟2.Rsyslog客戶端
[root@rsyslog-client ~] # yum install -y hpptd
[root@rsyslog-client ~] # systemctl start httpd
[root@rsyslog-client ~] # netstat -nutpl | grep httpd
tcp6 0 0 :::80 :::* LISTEN 9225 /httpd
# apache日志文件路徑
[root@rsyslog-client ~] # ls -l /var/log/httpd/*
-rw-r--r--. 1 root root 43059 Jun 11 16:38 /var/log/httpd/access_log
-rw-r--r--. 1 root root 8823 Jun 11 16:38 /var/log/httpd/error_log
[root@rsyslog-client ~] # cp /etc/rsyslog.conf /etc/rsyslog.conf_bak
[root@rsyslog-client ~] # vim /etc/rsyslog.conf
....
# 最末行添加
$ModLoad imfile # 加載imfile模塊
$InputFileName /var/log/httpd/access_log
$InputFileTag apache_access
$InputFileStateFile apache-access
$InputFileName var /log/httpd/error_log
$InputFileTag apache_error
$InputFileStateFile apache-error
$InputFileSeverity info
$InputFileFacility user
$InputFilePollInterval 1
$InputFilePersistStateInterval 1
$InputRunFileMonitor
user.* @172.16.70.186
配置解析:
$InputFileName # 訪問應用日志文件路徑
$InputFileTag # tag 標簽,可用作過濾或分類
$InputFileStateFile # 狀態文件,只需要指定文件名,程序會在工作目錄$WorkDirectory下創建指定文件
$InputFileSeverity # 日志級別
$InputFileFacility # 日志類型,user代表應用日志
$InputFilePollInterval # 頻率輪詢新數據,單位秒
$InputFilePersistStateInterval # 寫入狀態文件時間間隔,單位秒
$InputRunFileMonitor # 激活當前監視器
user.* @172.16.70.186 # 接收服務器IP
root@rsyslog-client ~] # systemctl restart rsyslog
步驟3.rsyslog服務端驗證
在任意Window 10主機瀏覽器訪問rsyslog客戶端apache服務
http: //172 .16.70.187 # 正確訪問會生成access.log
http: //172 .16.70.187:808 # 錯誤訪問會生成error.log
[root@rsyslog-server ~] # cd /data/rsyslog
[root@rsyslog-server rsyslog] # tree
.
└── 172.16.70.187
└── httpd
├── http-access.log
└── http-error.log
注:若無生成log,Server端,Client端各重啟幾遍rsyslog即可。