forked from kitian616/jekyll-TeXt-theme
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liali
committed
Nov 24, 2023
1 parent
44dff24
commit b97f20a
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
layout: article | ||
tags: Linux | ||
title: rsyslog | ||
mathjax: true | ||
key: Linux | ||
--- | ||
|
||
[doc1](https://www.jianshu.com/p/a009c6690713) | ||
{:.info} | ||
[doc2](https://blog.csdn.net/zyy617532750/article/details/74942090) | ||
{:.info} | ||
|
||
|
||
## rsyslog | ||
``` | ||
man rsyslog.conf | ||
rsyslog 作为标准的syslog守护进程,预装在了大多数的Linux发行版中。在客户端/服务器架构的配置下,rsyslog同时扮演了两种角色: | ||
1.作为一个syslog服务器,rsyslog可以收集来自其他设施的日志信息; | ||
2.作为一个syslog客户端,rsyslog可以将其内部的日志信息传输到远程的syslog服务器。 | ||
当通过syslog机制来收集日志时,有3个必须要考虑到的重要事情: | ||
1、设施层级: 监听何种类型的进程 | ||
2、严重性 (优先) 级别: 收集何种级别的日志消息 | ||
3、目标: 发送或记录日志消息到何处 | ||
现在我们更加深入地了解一下配置是如何定义的 | ||
设施层级定义了一种用来对内部系统进程进行分类的方法,linux中的一些常见的设施包括: | ||
auth: 身份验证相关的消息(登录时) | ||
cron: 进程或应用调度相关的消息 | ||
daemon: 守护进程相关的消息(内部服务器) | ||
kernel: 内核相关的消息 | ||
mail: 内部邮件服务器相关的消息 | ||
syslog: syslog 守护进程本身相关的消息 | ||
lpr: 打印服务相关的消息 | ||
local0 – local7: 用户自定义的消息 (local7 通常被Cisco 和 Windows 服务器 使用) | ||
严重性(优先)级别有固定的标准缩写和指代的值,其中的数字7具有最高的级别,这些级别包含了: | ||
emerg: Emergency(紧急)- 0 | ||
alert: Alerts (报警)- 1 | ||
crit: Critical (关键)- 2 | ||
err: Errors (错误)- 3 | ||
warn: Warnings (警告)- 4 | ||
notice: Notification (通知)- 5 | ||
info: Information (消息)- 6 | ||
debug: Debugging (调试)- 7 | ||
最后,目标语句会让一个syslog客户端来执行以下三个任务之一: | ||
1.保存日志消息到一个本地文件; | ||
2.通过TCP/UDP将消息路由到远程的syslog服务器中; | ||
3.将其发送到一个标准输出中,例如控制台。 | ||
在 rsyslog里, syslog的配置是基于以下模式进行结构化的。 | ||
[facility-level].[severity-level] [destination] | ||
配置文件/etc/rsyslog.conf大概分为三个部分 | ||
####MODULES#### | ||
这个部分是针对接收配置的,主要是指定接收日志的协议和端口。若要配置日志服务器,则需要将相应的配置项去掉注释。 | ||
####GLOBAL DIRECTIVES#### | ||
这个部分主要用来配置模板,模板的作用是指定你希望在日志文件中保存的日志格式。 | ||
#### RULES #### | ||
这一部分是规则文件,每行配置分两个字段,第一字段是说明要记录哪类日志(包括消息类型和等级),第二字段是说明日志存放位置(action),可以是本地文件,也可以是远程服务器。 | ||
filter action | ||
rsyslog发现符合 filter 规则的日志后,会将日志发送到 action 指定的动作进行处理。 | ||
``` | ||
|
||
## filter | ||
``` | ||
(1) 基于设施/优先级的过滤器 (Facility/Priority-based filters) | ||
FACILITY.PRIORITY | ||
kern.* # 选择所有优先级的内核日志 | ||
mail.crit # 选择所有mail 的优先级高于crit的日志 | ||
cron.!info,!debug # 选择除了 info 和 debug 优先级的 cron 日志 | ||
(2) 基于属性的过滤器 | ||
:PROPERTY, [!]COMPARE_OPERATION, "STRING" | ||
:msg, contains, "error" | ||
:hostname, isequal, "host1" | ||
:msg, !regex, "fatal .* error" | ||
(3) 基于表达式的过滤器 | ||
基于表达式的过滤器使用了rsyslog自定义的脚本语言RainerScript构建复杂的filter | ||
``` | ||
|
||
## action | ||
``` | ||
Action定义了当匹配指定的 filter 的时候,执行什么操作。 | ||
如果要指定多个 ACTION, 使用 &连接多个 ACTION。 | ||
kern.=crit user1 | ||
& ^test-program;temp | ||
& @192.168.0.1 | ||
这里的;temp指定了传递日志给 test-program 程序时( ^ 开头表明日志发送给该可执行文件),使用它 temp 模板格式化日志。 | ||
local3.* ^/tmp/a.sh # ^号后跟可执行脚本或程序的绝对路径 | ||
在 ACTION 后面追加;模板名称可以为指定的 action 使用该模板格式化日志。 | ||
保存日志到日志文件 | ||
FILTER PATH | ||
这里的 PATH 指定了日志要保存到的文件。例如 cron.* /var/log/cron.log 指定了所有的定时任务日志都写入到/var/log/cron.log文件。 | ||
通过网络发送syslog | ||
@[(zNUMBER)]HOST:[PORT] | ||
这里的@告诉syslog使用 UDP 协议发送日志,要使用 TCP 的话,使用 @@。可选值zNUMBER设置了是否允许使用zlib对日志压缩(压缩级别1-9)。 | ||
*.* @192.168.0.1 # 使用 UDP 发送,默认端口514 | ||
*.* @@example.com:18 # 使用 TCP 发送到端口18, 默认10514 | ||
*.* @(z9)[2001:db8::1] # UDP, ipv6,使用zlib级别9压缩 | ||
丢弃日志 | ||
FILTER ~ | ||
``` | ||
|
||
## 模板 | ||
``` | ||
任何rsyslog生成的日志都可以根据需要使用模板进行格式化,要创建模板,使用如下指令 | ||
$template TEMPLATE_NAME,"text %PROPERTY% more text", [OPTION] | ||
这里的$template指令表明了接下来的内容定义了一个模板,TEMPLATE_NAME是模板的名称,接下来双引号之间的内容为模板的内容。 | ||
生成动态文件名 | ||
模板可以用来生成动态文件名,就如之前所述,在使用动态文件名的时候,需要在 ACTION 中的模板名称前增加?表明该文件名是动态生成的。 | ||
$template DynamicFile,"/var/log/test_logs/%timegenerated%-test.log" | ||
*.* ?DynamicFile | ||
timegenerated属性从日志信息中提取出消息的时间戳,这样可以为每个日志生成唯一文件名称。 | ||
属性 | ||
在模板中使用的属性是在%之间的内容,使用属性可以访问日志消息中的内容。 | ||
%PROPERTY_NAME[:FROM_CHAR:TO_CHAR:OPTION]% | ||
``` |