简介
目前常用的开源日志收集系统有很多。Apache Flume 是一个分布式、高可靠、高可用的服务,用于高效地收集、聚合和移动大量的日志数据,它具有流式数据流的简单灵活的架构。其可靠性机制和许多故障转移及恢复机制使 Flume 具有强大的容错能力。
监控端口数据案例
-
案例需求:
首先启动 Flume 任务,监控本机 44444 端口,服务端;
然后通过 netcat 工具向本机 44444 端口发送消息,客户端;
最后 Flume 将监听的数据实时显示在控制台。 -
实现步骤:
安装 netcat 工具sudo apt-get -y install netcat-traditional
判断 44444 端口是否被占用
sudo apt install net-tools -y sudo netstat -tunlp | grep 44444
在flume 目录下创建 job 文件夹并进入 job 文件夹,创建 Flume Agent 配置文件 flume-netcat-logger.conf
cd $FLUME_HOME # 进入flume文件夹 mkdir job cd job vi flume-netcat-logger.conf
添加如下内容
# Name the components on this agent a1.sources = r1 # r1:表示a1的输入源 a1.sinks = k1 # k1:表示a1的输出目的地 a1.channels = c1 # c1:表示a1的缓冲区 # Describe/configure the source a1.sources.r1.type = netcat # 表示a1的输入源类型为netcat的端口类型 a1.sources.r1.bind = localhost # 表示a1的监听的主机 a1.sources.r1.port = 44444 # 表示a1的监听的端口号 # Describe the sink a1.sinks.k1.type = logger # 表示a1的输出目的地是控制台logger类型 # Use a channel which buffers events in memory a1.channels.c1.type = memory # 表示a1的channel类型是memory内存型 a1.channels.c1.capacity = 1000 # 表示a1的channel总容量1000个event a1.channels.c1.transactionCapacity = 100 # 表示a1的channel传输时收集到了100条event以后再去提交事务 # Bind the source and sink to the channel a1.sources.r1.channels = c1 # 表示将r1和c1连接起来 a1.sinks.k1.channel = c1 # 表示将k1和c1连接起来 # 注意:这里的 channel 是没有加s的,1个sink只能接受1个channel
开启 flume 监听端口
cd $FLUME_HOME # cd /usr/local/flume
第一种写法
bin/flume-ng agent -c conf -f job/flume-netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console
第二种写法
bin/flume-ng agent --conf conf --name a1 --conf-file job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
参数说明:
–conf conf/ :表示配置文件存储在conf/目录
–name a1 :表示给agent起名为a1
–conf-file job/flume-netcat.conf :flume本次启动读取的配置文件是在job文件夹下的flume-telnet.conf文件。
-Dflume.root.logger==INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。使用netcat工具向本机的44444端口发送内容
nc localhost 44444 telnet localhost 44444
实时读取本地文件到 HDFS 案例
- 案例需求:实时监控 aaa.txt 文件,并上传到 HDFS 中
- 实现步骤:Flume 要想将数据输出到 HDFS,必须持有 Hadoop 相关 jar 包
将 commons-configuration-1.6.jar、
hadoop-auth-2.7.2.jar、
hadoop-common-2.7.2.jar、
hadoop-hdfs-2.7.2.jar、
commons-io-2.4.jar、
htrace-core-3.1.0-incubating.jar
拷贝到 /usr/local/flume/lib 文件夹下。
在 job 下创建文件 flume-file-fdfs.conf 并添加如下内容
# Name the components on this angent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /usr/local/flume/job/aaa.txt
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://localhost:9000/flume/%Y%m%d/%H
# 上传文件前缀
a2.sinks.k2.hdfs.filePrefix = logs
# 是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
# 多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
# 重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour
# 是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
# 积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 1000
# 设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
# 多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 60
# 设置每个文件的滚动大小大概是128M
a2.sinks.k2.hdfs.rollSize = 134217700
# 文件的滚动与 Event 数量无关
a2.sinks.k2.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2
启动 hadoop:start-all.sh
启动 agent:
cd /usr/local/flume
bin/flume-ng agent -c conf -f job/flume-file-hdfs.conf -n a2
在 job 下创建 aaa.txt 并写入任意字符
实时读取目录文件到 HDFS 案例
-
案例需求:使用 Flume 监听整个目录的文件
-
实现步骤:
在 job 下创建文件 flume-dir-hdfs.conf 并添加如下内容a3.sources = r3 a3.sinks = k3 a3.channels = c3 # Describe/configure the source a3.sources.r3.type = spooldir a3.sources.r3.spoolDir = /usr/local/flume/upload a3.sources.r3.fileSuffix = .COMPLETED a3.sources.r3.fileHeader = true # 忽略所有以.tmp 结尾的文件,不上传 a3.sources.r3.ignorePattern = ([^ ]*\.tmp) # Describe the sink a3.sinks.k3.type = hdfs a3.sinks.k3.hdfs.path = hdfs://localhost:9000/flume/upload/%Y%m%d/%H a3.sinks.k3.hdfs.filePrefix = upload a3.sinks.k3.hdfs.round = true a3.sinks.k3.hdfs.roundValue = 1 a3.sinks.k3.hdfs.roundUnit = hour a3.sinks.k3.hdfs.useLocalTimeStamp = true a3.sinks.k3.hdfs.batchSize = 100 a3.sinks.k3.hdfs.fileType = DataStream a3.sinks.k3.hdfs.rollInterval = 60 a3.sinks.k3.hdfs.rollSize = 134217700 a3.sinks.k3.hdfs.rollCount = 0 # Use a channel which buffers events in memory a3.channels.c3.type = memory a3.channels.c3.capacity = 1000 a3.channels.c3.transactionCapacity = 100 # Bind the source and sink to the channel a3.sources.r3.channels = c3 a3.sinks.k3.channel = c3
在 /usr/local/flume 下创建 upload 文件夹
cd /usr/local/flume mkdir upload
启动 agent:
cd /usr/local/flume bin/flume-ng agent -c conf -f job/flume-dir-hdfs.conf -n a3
在 upload 文件夹下创建文件:touch aaa.txt
netstat 用法
功能描述:netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。
基本语法:netstat [选项]
选项参数:
-t或–tcp:显示TCP传输协议的连线状况;
-u或–udp:显示UDP传输协议的连线状况;
-n或–numeric:直接使用ip地址,而不通过域名服务器;
-l或–listening:显示监控中的服务器的Socket;
-p或–programs:显示正在使用Socket的程序识别码(PID)和程序名称;