logs滚动导入到flume+kafka

1 centeros7时间同步

删除本地时区

#rm -rf /etc/localtime

设置时区

cp /usr/share/zoneinfo/Asia/Shanghai/etc/localtime

安装ntp,同步时间

yum install -y ntp

同步时间

ntpdate time.nist.gov

格式化查看时间

 date "+%Y/%m/%d %H:%M:%S"

2 使用cron执行任务计划

通过查看系统提供的crontab文件,查看cron命令

cat /etc/crontab 

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

启动cron服务
systemctl stop crond
停止cron服务
systemctl restart crond
重启cron服务

systemctl enable crond.service

设置cron开机启动

查看当前crontab,输入 crontab -l

编辑当前crontab,输入 crontab -e

删除当前crontab,输入 crontab  -r

crontab文件的例子:
30 21 *  *  *  systemctl restart httpd.service
表示每晚21:30重启apache。

45 4 1,10,22  *  * systemctl restart httpd.service
表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 systemctl restart httpd.service
表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * systemctl restart httpd.service
表示在每天18 : 00至23 : 00每隔30分钟重启apache。

0 23 * * 6 systemctl restart httpd.service
表示星期六晚上11点重启apache。

* */1 * * * systemctl restart httpd.service
每一小时重启apache

* 23-7/1 * * * systemctl restart httpd.service
晚上11点到早上7点之间,每隔一小时重启apache

0 11 4 * mon-wed systemctl restart httpd.service
每月的4号与每周一到周三的11点重启apache

0 4 1 jan * systemctl restart httpd.service
表示一月一号的4点重启apache

3  完成nginx日志切割

nginx日志形式

1:remote_addr 远程访问ip地址---[192.168.1.5]
2:remote_user 远程访问用户
3:timelocal 访问时间---[02/Dec/2017:14:32:35+xxxx]
4:request 访问方式以及协议---[GET/HTTIP1.1]
5:status 返回状态---[304]
6:body_bytes_sent 发送给客户端的字节数
7:http_referer 从哪个页面链接过 来的
8:http_user_agent 用户代理/蜘蛛 ,被转发的请求的原始IP
9:http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP
建一个shell脚本runlog.sh,位于/bin/bash /usr/local/nginx/data下。

这个脚本的作用是将日志文件备份并新建日志文件,通知nginx使用新的日志文件

#!/bin/bash

dateformat=`date +%Y-%m-%d-%H-%M`
ngdir=/usr/local/nginx/logs
cp $ngdir/access.log $ngdir/access_$dateformat.log
host=`hostname`
sed -i 's/^/'${host}',&/g' ${ngdir}/access_$dateformat.log
lines=`wc -l < ${ngdir}/access_$dateformat.log`
mv ${ngdir}/access_$dateformat.log ${ngdir}/flume

sed -i '1,'${lines}'d' ${ngdir}/access.log

#通知nginx使用新的日志文件

kill -USR1 `cat ${ngdir}/nginx.pid`


3 为flume添加配置文件 glog.conf

a1.sources = r1
a1.sinks = k1
a1.channels = c1


# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /usr/local/nginx/logs/flume
a1.sources.r1.fileHeader = true


# Describe the sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = ping
a1.sinks.k1.kafka.bootstrap.servers = master:9092 slavea:9092 slaveb:9092


# Use a channel which buffers events in memory
a1.channels.c1.type = memory


# Bind the source and sink to the channel
a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

将kafka的logs目录修改权限,允许flume重命名

chmod  777 /usr/local/nginx/logs

4 启动kafka集群

  A:启动zookeeper集群;

   zkServer.sh start 

 B:启动kafka集群

  kafka-server-start.sh -daemon kafka_2.11/config/server.properties

C: 为kafka创建主题

kafka-topics.sh --zookeeper master:2181 --topic ping --create --partitions 1 --replication-factor 1

D:查看主题

kafka-topics.sh --zookeeper master:2181 --topic ping --list

E:启动flume,将日志信息收集到kafka

flume-ng agent -f asdf.conf -n a1 &

F:启动hdfs集群

start-dfs.sh



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值