本文实现:flume 读取日志信息时,通过正则匹配过滤,将匹配到的结果存放在指定文件目录。
本文结构:
1. 编写自定义过滤器
2. 编写flume配置文件
3. 运行测试
日志信息
过滤前:
2017-01-06T11:32:48: Debug: D-UNK-000-000: Rules file processing took 332 usec.
2017-01-06T11:32:48: Debug: D-UNK-000-000: Flushing events to object servers
2017-01-06T11:32:48: Debug: D-UNK-000-000: 1 buffered alerts
2017-01-06T11:33:18: Debug: D-JPR-000-000: Parsing events: Omegamon_Base;cms_hostname='itmserver';cms_port='37076';integration_type='U';master_reset_flag='';appl_label='';situation_name='disk';situation_type='S';situation_origin='itmserver:LZ';situation_time='01/06/2017 11:33:23.000';situation_status='N';situation_thrunode='TEMS_TEST';situation_fullname='home_disk_error';situation_displayitem='';source='ITM';sub_source='itmserver:LZ';hostname='itmserver';origin='192.168.100.50';adapter_host='itmserver';date='01/06/2017';severity='CRITICAL';msg='itm server home directory > 80%';situation_eventdata='~';END
2017-01-06T11:33:18: Debug: D-UNK-000-000: [Event Processor] EventString: Omegamon_Base;
2017-01-06T11:33:18: Debug: D-UNK-000-000: [Event Processor] ClassName: Omegamon_Base
2017-01-06T11:33:18: Debug: D-UNK-000-000: [Event Processor] adapter_host: itmserver
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
只是提取其中 以Parsing events:为开头关键字,以END为结尾的日志信息内容。
过滤后
Parsing events: Omegamon_Base;cms_hostname='itmserver';cms_port='37076';integration_type='U';master_reset_flag='';appl_label='';situation_name='disk';situation_type='S';situation_origin='itmserver:LZ';situation_time='01/06/2017 11:33:23.000';situation_status='N';situation_thrunode='TEMS_TEST';situation_fullname='home_disk_error';situation_displayitem='';source='ITM';sub_source='itmserver:LZ';hostname='itmserver';origin='192.168.100.50';adapter_host='itmserver';date='01/06/2017';severity='CRITICAL';msg='itm server home directory > 80%';situation_eventdata='~';END
- 1
1. 自定义过滤器
新建maven 项目,pom.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.us</groupId>
<artifactId>flumeInterceptor</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<version.flume>1.7.0</version.flume>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>${version.flume}</version>
</dependency>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-configuration</artifactId>
<version>${version.flume}</version>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
新建自定义过滤器类MyInterceptor 继承Interceptor 类
package com.us;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.flume.interceptor.RegexFilteringInterceptor.Constants.DEFAULT_REGEX;
import static org.apache.flume.interceptor.RegexFilteringInterceptor.Constants.REGEX;
/**
* Created by yangyibo on 17/1/6.
*/
public class MyInterceptor implements Interceptor {
private final Pattern regex;
private MyInterceptor(Pattern regex) {
this.regex = regex;
}
@Override
public void initialize() {
// NO-OP...
}
@Override
public void close() {
// NO-OP...
}
// JAVA中用于处理字符串常用的有三个类:
//
// java.lang.String、
//
// java.lang.StringBuffer、
//
// java.lang.StringBuilder,
//
// 这三者的共同之处都是 final 类,不允许被继承,这主要是从性能和安全性上考虑的,因为这几个类都是经常被使用着的,且考虑到防止其中的参数被修改影响到其它的应用。
//
// StringBuffer 与 StringBuilder 两个基本上差不多,只是 StringBuffer 是线程安全,可以不需要额外的同步用于多线程中;
//
// StringBuilder 是非同步,运行于多线程中就需要使用着单独同步处理,但是速度就比 StringBuffer 快多了;二者之间的共同点都可以通过append、insert进行字符串的操作。
//
// String 实现了三个接口:Serializable、Comparable<String>、CharSequence,
//
// 而 StringBuffer 及 StringBuilder 只实现了两个接口 Serializable、CharSequence,相比之下 String 的实例可以通过 compareTo 方法进行比较,而其它两个就不可以。
@Override
public Event intercept(Event event) {
String body = new String(event.getBody(), Charsets.UTF_8);
//匹配日志信息中以 Parsing events: 为开头关键字,以END 为结尾关键字 的日志信息段
String pattern= "(Parsing events)(.*)(END)";
// 创建 Pattern 对象
Pattern r= Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m= r.matcher(body);
StringBuffer bodyoutput = new StringBuffer();
if(m.find())//此处可以用多个正则进行匹配,多条件可以用&& 或者|| 的方式约束连接。
{
//多个正则匹配后可以将多个匹配的结果append 到bodyoutput
bodyoutput=bodyoutput.append(m.group(0));
event.setBody(bodyoutput.toString().getBytes());
}else{
event=null;
}
return event;
}
@Override
public List<Event> intercept(List<Event> events) {
List<Event> intercepted = Lists.newArrayListWithCapacity(events.size());
for (Event event : events) {
Event interceptedEvent = intercept(event);
if (interceptedEvent != null) {
intercepted.add(interceptedEvent);
}
}
return intercepted;
}
public static class Builder implements Interceptor.Builder {
private Pattern regex;
//使用Builder初始化Interceptor
@Override
public Interceptor build() {
return new MyInterceptor(regex);
}
@Override
public void configure(Context context) {
String regexString = context.getString(REGEX, DEFAULT_REGEX);
regex = Pattern.compile(regexString);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
1.3 打包
将此maven项目打成jar包,将jar 包放到flume的lib 目录下。
关于jar 包管理请看 flume自定义组件的 jar 包管理
2. 编写flume配置文件
在flume的conf 文件夹下新建exec.conf 配置文件内容如下:
注意:(读取/opt/apps/logs/tail.log 文件中的内容,并将过滤结果输出到/opt/apps/tmp/ 目录下);
com.us.MyInterceptor 为我门自定义过滤器类的全称限定名
- 1
- 2
- 3
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = exec
#a1.sources.r1.shell = /bin/bash -c
a1.sources.r1.channels = c1
a1.sources.r1.command = tail -F /opt/apps/logs/tail.log
#filter
a1.sources.r1.interceptors=i1
a1.sources.r1.interceptors.i1.type=regex_filter
#a1.sources.r1.interceptors.i1.type= com.us.MyInterceptor$Builder
a1.sources.r1.interceptors.i1.regex=(Parsing events)(.*)(END)
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# sink
a1.sinks.k1.type = file_roll
a1.sinks.k1.channel = c1
#a1.sinks.k1.sink.rollInterval=0
a1.sinks.k1.sink.directory = /opt/apps/tmp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
3. 运行测试
在flume 的bin目录下执行启动命令
注意:/opt/apps/flume/ 是flume的位置,应替换为你flume的位置
- 1
- 2
./flume-ng agent -c /opt/apps/flume/conf -f /opt/apps/flume/conf/exec.conf -n a1 -Dflume.root.logger=INFO,console
- 1
在监控目录下心间tail.log 文件,并放入过滤前的日志原信息,保存文件。到结果目录(/opt/apps/tmp)下查看生成的结果文件。查看过滤结果是否正确。
注意:如果tail.log 被监控文件存在,那么启动flume以后 应当修改一下tail.log 文件,使其发生变化,这样才会被flume监控到。
- 1
- 2
结果如下:
版权声明:本文为博主编写文章,未经博主允许转载,转载请注明出处。