背景
现在服务器数量较多,经常需要做日志收集。耳熟能详的方案是 ELK 和后起之秀 Loki,但是有的时候项目组的资源有限,用这些方案稍显笨重,所以这时候需要操作系统自带的 Rsyslog 了。
简单记录
Rsyslog 通常操作系统自带,分为服务端和客户端,如何区分,主要是看配置。下面演示一下如何通过 Rsyslog 收集 Redis 的日志。
服务端
[root@l-rsj-wjfwq ~]# egrep -v '#|^$' /etc/rsyslog.conf
$ModLoad imudp ##开启udp的514端口
$UDPServerRun 514
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$template Remote,"/data/logbackup/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log" ##定义模板,接受日志文件路径,区分了不同主机的日志
:fromhost-ip, !isequal, "127.0.0.1" ?Remote # # 过滤server 本机的日志
& ~
$IncludeConfig /etc/rsyslog.d/*.conf
$OmitLocalLogging on
$IMJournalStateFile imjournal.state
*.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
客户端
Redis 的日志路径是 /data/redis/6379.log
。收集这种第三方的日志时需要使用 imfile 模块。
[root@l-rsj-zjjfwq2 ~]# cat /etc/rsyslog.conf
# rsyslog 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 ####
# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark # provides --MARK-- message capability
# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514
# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
# 重点在这里,启用模块及指定日志路径
$ModLoad imfile
$InputFileName /data/redis/6379.log
$InputFileTag nova-info:
$InputFileStateFile stat-nove-info
$InputRunFileMonitor
#### 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/
$IncludeConfig /etc/rsyslog.d/*.conf
# Turn off message reception via local log socket;
# 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
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg :omusrmsg:*
# 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
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.
$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
# 这里是 Rsyslog 服务端的日志
*.* @172.19.17.2:514
# ### end of the forwarding rule ###
测试
模拟在日志文件里写入一行文字。然后去 Rsyslog 服务端指定的路径去查看是否出现了该文件夹和文件内容。
这里可以看到收集完成
标签:log,messages,Redis,Rsyslog,rsyslog,var,日志,ModLoad From: https://www.cnblogs.com/fsckzy/p/16831169.html