源自个人博客:kelvin-qzy.top/post/rsyslo…
安装
sudo yum install rsyslog
不幸的是,发行版通常提供相当旧的rsyslog版本。可以通过官网下载rpm或deb包来安装新版,或使用docker容器安装。
一般来说,这句也不用执行,系统已经默认安装过了!
安装新版本
这是rsyslog下载链接 例如:
cd /etc/yum.repos.d/
wget http://rpms.adiscon.com/v8-stable/rsyslog.repo
yum install rsyslog
查看版本
rsyslogd -ver
别说版本还是很新的,v8:
包结构
- 核心软件包(通常简称为“ rsyslog”)-它包含所有其他软件包都需要的核心技术。它还包含诸如文件编写器或syslog转发器之类的模块,这些模块非常常用并且几乎没有依赖性。
- 例如对于MySQL组件来说是“ rsyslog-mysql”,对于ElasticSearch支持来说是“ rsyslog-elasticsearch”。如有疑问,建议使用发行版的软件包管理器并搜索“ rsyslog *”。
使用Docker容器
docker run -ti rsyslog/syslog_appliance_alpine
配置
通常在/etc/rsyslog.conf
配置语法有三种,见名知义的:
- basic 最适合表示基本内容的格式,例如语句适合单行的格式。它起源于最初的syslog.conf格式,现在已经使用了几十年。
- advanced 以前称为RainerScript格式,此格式最早在rsyslog v6中可用,最好在v7之后用。专门针对更高级的用例,例如转发到可能部分处于脱机状态的远程主机。
- obsolete legacy 其名称所隐含的含义:它已过时,在编写新配置时不应使用。
- 本质上,所有需要在以美元符号开头的一行上编写的内容都是旧格式。鼓励这种格式的用户迁移到basic或advanced格式。
配置示例
basic
模式仍被建议使用
对于大多数由简单语句组成的配置仍然建议使用该格式
# 把mail的info级别的日志写入到mail.log
mail.info /var/log/mail.log
# 把mail的error级别的日志使用tcp协议转发到远端server上
mail.err @@server.example.net
高级功能使用advanced
格式
优势:
- 通过高级参数精细控制rsyslog操作
- 块结构容易理解
- 容易写
- 可安全地与包含文件一起使用
对于上面的例子,比如你想在远端server目标掉线时不丢数据,使用如下如法:
mail.err action(type="omfwd" protocol="tcp" queue.type="linkedList" target="server")
高端大气上档次
加载模块
module(load="module-name")
模块全局配置
module(load="imtcp" maxSessions="500")
语法对比
行动链
使用 &
链接出一个行动链条
*.error /var/log/errorlog
& @remote
完整写法:
*.error {
action(type="omfile" file="/var/log/errorlog")
action(type="omfwd" target="remote" protocol="udp")
}
这样易于拓展,模块化不易出错。
stop
阻止进一步的处理,符号为~
:
:msg, contains, "error" @remote
& ~
高级写法为:
:msg, contains, "error" {
action(type="omfwd" target="remote" protocol="udp")
stop
}
或:
if $msg contains "error" then {
action(type="omfwd" target="remote" protocol="udp")
stop
}
sysklogd 格式
语法为:选择器字段 (空格或TAB) 行动字段
选择器
选择器由facility
和priority
组成,用.
连接起来。这二者遵循syslog(3)描述。
可直接通过系统文件/usr/include/sys/syslog.h
查看相应值。
facility: auth, authpriv, cron, daemon, ftp, kern, lpr, mail, mark, news, security (same as auth), syslog, user, uucp and local0 through local7.
priority: debug, info, notice, warning, err, crit, alert, emerg.
warn, error and panic 已被弃用!
“*”代表全部,none
代表没有,“,”分隔多个设施,“;”分隔多规则
行动
行动抽象了一个“日志文件”。
- 常规文件 真实的文件,一般使用绝对路径
- 命名管道(fifos)
- 终端和控制台 tty 或 /dev/console
- 远端机器 统一收集处理
- 用户列表等
示例
# Store critical stuff in critical
#
*.=crit;kern.none /var/adm/critical
#########
# Kernel messages are stored in the kernel file,
# critical messages and higher ones also go
# to another host and to the console
#
kern.* /var/adm/kernel
kern.crit @finlandia
kern.crit /dev/console
kern.info;kern.!err /var/adm/kernel-info
#########
# The tcp wrapper logs with mail.info, we display
# all the connections on tty12
#
mail.=info /dev/tty12
############
# Write all mail related logs to a file except for the info priority.
#
mail.*;mail.!=info /var/adm/mail
########
# Log all mail.info and news.info messages to info
#
mail,news.=info /var/adm/info
# Log info and notice messages to messages file
#
*.=info;*.=notice;\
mail.none /var/log/messages