项目需要将日志收集起来做存储分析,数据的流向为rsyslog(收集) -> kafka(消息队列) -> logstash(清理) -> es、hdfs; 今天我们先将如何利用rsyslog进行日志收集到kafka。

一、环境准备

通过对 rsyslog官方文档 查看,得知 rsyslog对 kafka的支持是 v8.7.0版本后才提供的支持.通过 ChangeLog 也可以看出 V8.X的版本变化.

最新V8稳定版已经提供RPM包的Rsyslog-kafka插件了,直接yum安装即可,添加yum源:

[rsyslog_v8]
name=Adiscon CentOS-$releasever - local packages for $basearch
baseurl=http://rpms.adiscon.com/v8-stable/epel-$releasever/$basearch
enabled=1
gpgcheck=0
gpgkey=http://rpms.adiscon.com/RPM-GPG-KEY-Adiscon
protect=1

添加后 yum install rsyslog rsyslog-kafka.x86_64即可完成安装。

二、配置

1. 处理原则

  • input submit received messages to rulesets, zero or many
  • ruleset contains rule, rule consist of a filter and an action list
  • actions consist of the action call itself (e.g. ”:omusrmsg:”) as well as all action-defining configuration statements ($Action... directives)

2. Statement Types 表达式类型

通常利用RainerScript type statements进行非常简洁明了的配置声明,例如:

mail.info /var/log/mail.log

3. 流程控制

  • Control structures
  • 过滤条件
    1. Selector: 传统方式,格式如下:

      <facility>[,facility...][,*].[=,!]<priority>[,priority...][,*];<facility>[,facility...][,*].[=|!]<priority>[,priority...][,*]...

      其中默认facility为auth, authpriv, cron, daemon, kern, lpr, mail, mark, news, security (same as auth), syslog, user, uucp and local0 through local7;

      默认priority为debug, info, notice, warning, warn (same as warning), err, error (same as err), crit, alert, emerg, panic (same as emerg);

      2) Property-based filters: new filter type. 形式如下:

      :property, [!]compare-operation, "value"

      分别对应 名字,比较符, 需要对比的字段。比较符包括 contains, isequal, startswith, regex, ereregex

      3) Expression based filters:

      if expr then action-part-of-selector-line
    2. BSD-style blocks:
    3. 例子: if $syslogfacility-text == 'local0' and $msg startswith 'DEVNAME' and not ($msg contains 'error1' or $msg contains 'error0') then /var/log/somelog

4. 数据处理:支持set, unset, reset操作

备注: Only message json (CEE/Lumberjack) properties can be modified by the set, unset andreset statements

5. input

有很多种input模块, 我们以imfile模块为例, 此模块将所有的文本文件内容逐行转到syslog中.

input(type="imfile" tag="kafka" file="analyze.log" ruleset="imfile-kafka"[, Facility=local.7])

6. outputs

也叫作actions, 处理动作,格式如下

 action (
type="omkafka"
topic="kafka_test"
broker="10.120.169.149:9092"
)

7. Rulesets and Rules

Rulesets包括多条rule,一条规则就是rsyslog处理消息的一种方式, 每个规则包含filter和actions

input(type="imfile" tag="kafka" file="analyze.log" ruleset="rulesetname")
ruleset(name="rulesetname") {
action(type="omfile" file="/path/to/file")
action(type="..." ...)
/* and so on... */
}

通过input里面的ruleset配置,将输入流进入ruleset进行规则匹配,然后执行action操作,完成对流的处理。

8. Queue parameters

将不同的输入流进入不同的队列并行处理数据,通常在ruleset或者action中配置,默认只有一个队列。配置参数例子

action(type="omfwd" target="192.168.2.11" port="10514" protocol="tcp"
queue.filename="forwarding" queue.size="1000000" queue.type="LinkedList"
)

9. templates

这是rsyslog一个重要的特性,它可以让用户自定义输入流格式,同样也可以用于动态生成日志文件, 默认是原始格式。

一般表达式如下:

template(parameters) { list-descriptions }

  • list : 列表模板,包含name, type="list", 多个constant和property对。
template(name="tpl1" type="list") {
constant(value="Syslog MSG is: '")
property(name="msg")
constant(value="', ")
property(name="timereported" dateFormat="rfc3339" caseConversion="lower")
constant(value="\n")
}
  • string: 字符串自定义格式模块, 由name, type="string", string="<onstant text and replacement variables>", 例如

%TIMESTAMP:::date-rfc3339% %HOSTNAME%%syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"

将每个日志字段通过自定义变量和处理方式(property replacer)得到全局能读取的日志变量。

注意:

  1. 原始格式: v6之前的格式,$template strtpl,"PRI: %pri%, MSG: %msg%\n"
  2. 利用action里的template参数将templates和action进行绑定,如

    action(template=TEMPLATENAME,type="omfile" file="/var/log/all-msgs.log")

三. 实例

增加一个将nginx access日志通过rsyslog传输到kafka的实例,将nginx_kafka.conf放入到/etc/rsyslog.d目录中,重启rsyslog即可。

# 加载omkafka和imfile模块
module(load="omkafka")
module(load="imfile") # nginx template
template(name="nginxAccessTemplate" type="string" string="%hostname%<-+>%syslogtag%<-+>%msg%\n") # ruleset
ruleset(name="nginx-kafka") {
#日志转发kafka
action (
type="omkafka"
template="nginxAccessTemplate"
confParam=["compression.codec=snappy", "queue.buffering.max.messages=400000"]
partitions.number="4"
topic="test_nginx"
broker="10.120.169.149:9092"
queue.spoolDirectory="/tmp"
queue.filename="test_nginx_kafka"
queue.size="360000"
queue.maxdiskspace="2G"
queue.highwatermark="216000"
queue.discardmark="350000"
queue.type="LinkedList"
queue.dequeuebatchsize="4096"
queue.timeoutenqueue="0"
queue.maxfilesize="10M"
queue.saveonshutdown="on"
queue.workerThreads="4"
)
} # 定义消息来源及设置相关的action
input(type="imfile" Tag="nginx,aws" File="/var/log/access.log" Ruleset="nginx-kafka")

检查conf文件是否正确可以运行rsyslogd debug模式rsyslogd -dn运行,看日志输出结果,或者直接运行rsyslogd -N 1检查conf文件是否正确。

作者:modeyangg_cs
链接:https://www.jianshu.com/p/1b7fdb1cff3c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

日志收集之rsyslog to kafka的更多相关文章

  1. 9.3 k8s结合ELK实现日志收集

    数据流: logfile -> filebeat > kafka(依赖zookeeper)-> logstash -> elasticsearch -> kibana 1 ...

  2. ELK+kafka构建日志收集系统

    ELK+kafka构建日志收集系统   原文  http://lx.wxqrcode.com/index.php/post/101.html   背景: 最近线上上了ELK,但是只用了一台Redis在 ...

  3. ELK+Kafka 企业日志收集平台(一)

    背景: 最近线上上了ELK,但是只用了一台Redis在中间作为消息队列,以减轻前端es集群的压力,Redis的集群解决方案暂时没有接触过,并且Redis作为消息队列并不是它的强项:所以最近将Redis ...

  4. 【转】flume+kafka+zookeeper 日志收集平台的搭建

    from:https://my.oschina.net/jastme/blog/600573 flume+kafka+zookeeper 日志收集平台的搭建 收藏 jastme 发表于 10个月前 阅 ...

  5. rsyslog+LogAnalyzer 日志收集

    Linux 之rsyslog+LogAnalyzer 日志收集系统 一.LogAnalyzer介绍 LogAnalyzer工具提供了一个易于使用,功能强大的前端,用于搜索,查看和分析网络活动数据,包括 ...

  6. 日志收集之kafka

    日志收集之kafka http://www.jianshu.com/p/f78b773ddde5 一.介绍 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1 ...

  7. 日志收集ELK+kafka相关博客

    SpringBoot+kafka+ELK分布式日志收集 使用 logstash + kafka + elasticsearch 实现日志监控 Kibana 安装 与 汉化 windows系统安装运行f ...

  8. Go语言学习之11 日志收集系统kafka库实战

    本节主要内容: 1. 日志收集系统设计2. 日志客户端开发 1. 项目背景    a. 每个系统都有日志,当系统出现问题时,需要通过日志解决问题    b. 当系统机器比较少时,登陆到服务器上查看即可 ...

  9. SpringBoot+kafka+ELK分布式日志收集

    一.背景 随着业务复杂度的提升以及微服务的兴起,传统单一项目会被按照业务规则进行垂直拆分,另外为了防止单点故障我们也会将重要的服务模块进行集群部署,通过负载均衡进行服务的调用.那么随着节点的增多,各个 ...

随机推荐

  1. 【vue】两个页面间传参 - props

    目录 Step1 设置可以 props 传递数据 Step2 跳转前页面中传递数据 Step3 跳转后的页面接收数据 从 A 页面跳转到 B 页面, 参数/数据通过 props 传递到 B 页面,这种 ...

  2. SpringBoot碰到的疑问或问题

    1.@ResponseBody 和 @RequestBody 的区别 @ResponseBody是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response ...

  3. uniapp内嵌H5页面和uniapp页面相互传值

    最近项目有一个需求 -- 做一个百人抽奖的模块,要求展示百人的头像并且不断变化排列组合 先展示一部分的用户头像,然后每增加一个用户就增加一个头像在百人排列里面 我整一个gif图来展示一下 大概就是这种 ...

  4. C++学习笔记:07 类的继承与派生

    课程<C++语言程序设计进阶>清华大学 郑莉老师) 基本概念 继承与派生的区别: 继承:保持已有类的特性而构造新类的过程称为继承. 派生:在已有类的基础上新增自己的特性(函数方法.数据成员 ...

  5. C++中string和char字符串的异同与使用方法

    C++中string和char声明字符串的异同和使用 string类 必须在头文件中包含<string> 隐藏了字符串的数组性质,可以像处理普通变量那样处理字符串 string类位于名称空 ...

  6. react之组件生命周期

    四个阶段 初始化 运行中 销毁 错误处理(16.3以后) 初始化 constructor static getDerivedStateFromProps() componentWillMount() ...

  7. C++学习 2 指针

    指针:指针保存的是数据的地址: #include<iostream> using namespace std; int main() { //1.定义指针 int a = 10; //指针 ...

  8. springMVC上传和下载附件

    上传: 导入需要的jar包:Spring MVC类库 + 文件上传下载需要的JAR包,图中A处为文件上传下载需要的JAR包,其余为Spring MVC类库. 构建领域模层:model层和control ...

  9. C++ 与 Visual Studio 2019 和 WSL

    Visual Studio 使用 C++ 的 Linux 开发(WSL) https://devblogs.microsoft.com/cppblog/c-with-visual-studio-201 ...

  10. Linux常用命令,查看树形结构、删除目录(文件夹)、创建文件、删除文件或目录、复制文件或目录(文件夹)、移动、查看文件内容、权限操作

    5.查看树结构(tree) 通常情况下系统未安装该命令,需要yum install -y tree安装 直接使⽤tree显示深度太多,⼀般会使⽤ -L选项⼿⼯设定⽬录深度 格式:tree -L n [ ...