引言
最近公司项目要讲日志接入已经搭建好的ELK中,本片文章不讲解ELK搭建。本来想这在项目中使用logstash-logback-encoder直接将日志吐到logstash中,但是这个需要改动代码,处于程序员的懒惰,我并没有做这个配置,基于现有配置用filebeat将日志文件抛到ElasticSearch中。本文采用的5.5.2已为老版本新版配置略有变化
以下两个为暂时使用的配置
# https://www.elastic.co/guide/en/beats/filebeat/index.html
filebeat:
spool_size: 1024 # 最大可以攒够 1024 条数据一起发送出去
idle_timeout: "5s" # 否则每 5 秒钟也得发送一次
registry_file: ".filebeat" # 文件读取位置记录文件,会放在当前工作目录下。所以如果你换一个工作目录执行 filebeat 会导致重复传输!
prospectors:
#- input_type: log
-
ignore_older: "24h" # 超过 24 小时没更新内容的文件不再监听。在 windows 上另外有一个配置叫 force_close_files,只要文件名一变化立刻关闭文件句柄,保证文件可以被删除,缺陷是可能会有日志还没读完
scan_frequency: "10s" # 每 10 秒钟扫描一次目录,更新通配符匹配上的文件列表
tail_files: false # 是否从文件末尾开始读取
harvester_buffer_size: 16384 # 实际读取文件时,每次读取 16384 字节
backoff: "1s" # 每 1 秒检测一次文件是否有新的一行内容需要读取
#json.message_key: message # json 信息中的message字段的Key
#json.keys_under_root: true # 不输出在json层级下
encoding: utf-8
#json.overwrite_keys: true # 是否使用json中的value覆盖掉同名key的值
paths:
- "/data/log/horder/${serviceName}/access*.log" # 可以使用通配符
#exclude_files: ["/var/log/apache/error.log"]
exclude_lines: ["^#"]
fields:
domain: ${serviceName}
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
#Array of hosts to connect to.
hosts: ["http://172.20.94.91:9200","http://172.20.94.92:9200","http://172.20.94.93:9200"]
#hosts: ["http://172.22.0.17:9200"]
#Optional protocol and basic auth credentials.
#protocol: "https"
#username: "elastic"
#password: "changeme"
index: "tomcatlog-%{+yyyy.MM.dd}"
bulk_max_size: 100
timeout: 90
flush_interval: 5s
pipeline: "tomcatlog"
#proxy_url: http://127.0.0.1:8888
上面的那个比较麻烦,可以简单点
#=========================== Filebeat prospectors =============================
filebeat:
prospectors:
-
paths:
- /data/log/horder/*/accesslog/*.log
input_type: log
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
hosts: ["http://172.20.94.91:9200","http://172.20.94.92:9200","http://172.20.94.93:9200"]
index: "tomcatlog-%{+yyyy.MM.dd}"
bulk_max_size: 100
timeout: 90
flush_interval: 5s
pipeline: "tomcatlog"
username: logstash
password: changeme
以上可以实现基本功能,当然filebeat有很多配置项,大家喜欢的可以自己往上配。
2019年6月5日10:07:46
补充更新 filebeat.sh
#!/etc/bash
#---------------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------------
#-------------------------------- filebeat启停通用脚本---------------------------------------------------
#--------------------如有疑问请咨询 [email protected]
#-------执行脚本.sh 配置文件名称.xml [start|stop|restart|status] 日志名称(可选)-------------
#--------------------------适应前请先执行 chmod u+x filebeat.sh--------------------------------------
FILE_NAME=$1
LOG_NAME=$3
COUNT=$#
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $FILE_NAME |grep -v grep|awk '{print $2}' `
: #如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo ">>> ${FILE_NAME} is already running PID=${pid} <<<"
else
if [ $COUNT -eq 3 ];
then
if [ ! -d "logs" ];then
mkdir ./logs
fi
nohup ./filebeat -e -c $FILE_NAME > ./logs/$LOG_NAME.log 2>&1 &
echo ">>> start $FILE_NAME successed PID=$! > ./logs/$LOG_NAME.log 2>&1 <<<"
else
nohup ./filebeat -e -c $FILE_NAME >/dev/null 2>&1 &
echo ">>> start $FILE_NAME successed PID=$! <<<"
fi
fi
}
#使用说明,用来提示输入参数
usage() {
echo ">>> Usage: 执行脚本.sh 配置文件名称.xml [start|stop|restart|status] <<<"
exit 1
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
echo ">>> PID = $pid begin kill -9 $pid <<<"
kill -9 $pid
sleep 2
echo ">>> ${FILE_NAME} process stopped <<<"
else
echo ">>> ${FILE_NAME} is not running <<<"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo ">>> ${FILE_NAME} is running PID is ${pid} <<<"
else
echo ">>> ${FILE_NAME} is not running <<<"
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
if [ $COUNT -eq 2 ] || [ $COUNT -eq 3 ];
then
if [ -f $FILE_NAME ];
then
case "$2" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
else
echo ">>> 输入配置文件不存在 <<<"
fi
else
echo ">>> 请输入正确参数 <<<"
usage
fi
exit 0
参考链接: