首页 > 数据库 >Rsyslog 收集 Redis 及第三方日志简单记录

Rsyslog 收集 Redis 及第三方日志简单记录

时间:2022-10-27 10:12:05浏览次数:46  
标签:log messages Redis Rsyslog rsyslog var 日志 ModLoad

背景

​ 现在服务器数量较多,经常需要做日志收集。耳熟能详的方案是 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 服务端指定的路径去查看是否出现了该文件夹和文件内容。
image

image

这里可以看到收集完成

标签:log,messages,Redis,Rsyslog,rsyslog,var,日志,ModLoad
From: https://www.cnblogs.com/fsckzy/p/16831169.html

相关文章

  • SpringBoot AOP异常日志处理 使用AOP+注解的方式进行异常日志的处理
    SpringBootAOP异常日志处理使用AOP+注解的方式进行异常日志的处理最近公司的一个项目需要将异常日志通过企业微信进行告警,由于消息推送已经有异常处理平台进行处理,现在......
  • Redis
    1.Redis官网redis.io2.操作流程2.1下载版本5.0.14cd/data/#下载wgethttps://download.redis.io/releases/redis-5.0.14.tar.gz#解压tarxfredis-5.0.14.tar.g......
  • 一篇文章带你了解NoSql数据库——Redis简单入门
    一篇文章带你了解NoSql数据库——Redis简单入门Redis是一个基于内存的key-value结构数据库我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储......
  • spdlog日志库源码:线程池thread_pool
    目录线程池thread_pool简介多生产者-多消费者阻塞队列模型阻塞与非阻塞方式插入数据取出数据overrun异常处理机制其他接口环形队列circular_qthreadpool模型threadpool实......
  • Redis持久化的研究---AOF
    Redis持久化的研究Redis的持久化的方式----AOF在redis中存在一个日志文件(aof日志),此日志文件中保存着在redis中执行的有效的命令;当需要进行数据恢复时就会加载该文件......
  • AgileBoot - 如何集成内置数据库H2和内置Redis
    本项目地址:github:https://github.com/valarchie/AgileBoot-Back-Endgitee:https://gitee.com/valarchie/AgileBoot-Back-End本项目说明:AgileBoot-基于SpringBoo......
  • Java连接Redis
    Java连接RedisRedis(RemoteDictionaryServer),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语......
  • zk,kafka,redis哨兵,mysql容器化
    1.zookeeper,kafka容器化1.1zookeeper+kafka单机docker模式dockerpullbitnami/zookeeper:3.6.3-debian-11-r46dockerpullbitnami/kafka:3.1.1-debian-11-r36dock......
  • vim查看携带颜色字符的日志文件
    很多时候可能会在服务器上用vim查看log文件,有时候文件中会有很多命令行颜色指令字符,类似:^[[0;37;40m2022-xxxx[0m。看的很难受。如果你的vim有+terminal特性,查看......
  • Redis快速入门
    认识NoSQL什么是NOSQLNoSQL最常见的解释是"non-relational",很多人也说它是"NotOnlySQL"NoSQL仅仅是一个概念,泛指非关系型的数据库区别于关系数据库,它们不保证关系......