Flume实例一 | 监控服务器Lynis日志上传hdfs | 监控目录上传hdfs


由于lynis的日志是直接覆盖原来的文件,似乎无法通过flume的exec等source对其进行监控(我写的agent都shut down了)。如果有同学成功了并且愿意分享的话,我不甚感激。
所以我的尝试以下:
思路为,通过shell监控lynis日志目录,如果有新日志以 时间命名的方式就存储在目标目录 /var/log/lynis/cp_logs中,再通过 Spooling Directory Sourceflume agent对目录进行监控。

实现功能:

shell脚本监控lynis日志目录:如果日志被修改将日志存储在指定目录下。
并通过flume的spooldir sources监控指定目录,利用hdfs sink将检测到的数据sink到hdfs上

配置文件

cd "flume目录"
  • 创建指定目录,以及文件
mkdir -p conf/Inventory/Lynis/
vim conf/Inventory/Lynis/lynis.properties
  • lynis.properties
#a1表示代理名称
a1.sources=s1
a1.sinks=k1
a1.channels=c1

# 配置source1  监控目录是否有文件数据生成
a1.sources.s1.type=spooldir
a1.sources.s1.spoolDir=/var/log/lynis/cp_logs
a1.sources.s1.channels=c1
a1.sources.s1.fileHeader = true
# a1.sources.s1.interceptors = i1
# a1.sources.s1.interceptors.i1.type = timestamp

#配置sink1  将检测到的数据sink到hdfs上
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path=hdfs://master:9000/flume/lynis/%Y-%m-%d-%H
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=TEXT
## 每隔60s或者文件大小超过100KB的时候产生新文件
# hdfs创建多长时间新建文件,0不基于时间
a1.sinks.k1.hdfs.rollInterval=10
# hdfs有多少条消息时新建文件,0不基于消息个数
a1.sinks.k1.hdfs.rollCount=0
# hdfs多大时新建文件,0不基于文件大小
a1.sinks.k1.hdfs.rollSize=0
# 当目前被打开的临时文件在该参数指定的时间(秒)内,没有任何数据写入,则将该临时文件关闭并重命名成目标文件
a1.sinks.k1.hdfs.idleTimeout=3
a1.sinks.k1.channel=c1
#时间参数一定要带上 true
a1.sinks.k1.hdfs.useLocalTimeStamp=true
# a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d
## 每五分钟生成一个目录:
# 是否启用时间上的”舍弃”,这里的”舍弃”,类似于”四舍五入”,后面再介绍。如果启用,则会影响除了%t的其他所有时间表达式
a1.sinks.k1.hdfs.round=true
# 时间上进行“舍弃”的值;
a1.sinks.k1.hdfs.roundValue=5
# 时间上进行”舍弃”的单位,包含:second,minute,hour
a1.sinks.k1.hdfs.roundUnit=hour

#通道是以文件方式存储
#配置channel1
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100

shell

# root权限下:
mkdir -p /var/log/lynis/tools
# 更改用户所有者
chown -R hadoop /var/log/lynis

/var/log/lynis/tools创建lynis_tool.sh

#!/bin/bash
fun(){
ls -lu /var/log/lynis.log > ./cur.log
diff ./cur.log ./pre.log > ./diff.log
if [ $? -ne 0 ]
then
cp -f /var/log/lynis.log  ./pre.log
file_name=`date "+%Y-%m-%d-%H:%M:%S"`
echo ${file_name}.log
cp -fp /var/log/lynis.log /var/log/lynis/cp_logs/${file_name}.log
fi	
}

if [ ! -d "/var/log/lynis/tools" ]
then
mkdir -p /var/log/lynis/tools
fi
if [ ! -d "/var/log/lynis/cp_logs" ]
then
mkdir -p /var/log/lynis/cp_logs
fi
echo "lynis tool started"
echo > ./pre.log
while true
do
    fun
    sleep 60
done

启动命令

  • 启动hdfs。
hadoop fs -mkdir -p /flume/lynis
  • 启动flume
bin/flume-ng agent --name a1  --conf conf  --conf-file conf/Inventory/Lynis/lynis.properties 
  • 其中--name参数对应,lynis.properties中的代理名称,--conf参数对应配置目录,--conf-file对应刚刚创建的配置文件的目录

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/106331320