流程如下:
- 先使用 Filebeat 把日志收集起来,然后把数据再传给 Logstash。
- 通过 Logstash 强大的数据清洗功能。
- 最终把数据写入到 Elasticsearch 中。
- 并由 Kibana 进行可视化
参考文档:
https://www.51cto.com/article/707776.html
踩的坑:
- 指定配置文件启动
安装nc
sudo dnf install -y nc
nc -h
bin/logstash -f /logstash-7.7.1/streamconf/weblog.conf
- 脚本文件
先转化为unix系统的脚本文件
sudo yum install dos2unix # 对于 CentOS 或 RHEL 系统
dos2unix start.sh
# nohup bin/logstash -f streamconf/weblog.conf &
#!/bin/bash
# Logstash 配置文件路径
CONFIG_PATH="streamconf/weblog.conf"
# 查找 Logstash 进程的 PID
PID=$(ps aux | grep "logstash" | grep -v "grep" | awk '{print $2}')
# 如果有 Logstash 进程在运行,则先杀死它
if [ ! -z "$PID" ]; then
echo "发现 Logstash 进程 (PID: $PID),正在停止..."
kill -9 $PID
echo "Logstash 进程已停止。"
else
echo "没有发现 Logstash 进程。"
fi
# 启动 Logstash
echo "启动 Logstash..."
nohup bin/logstash -f $CONFIG_PATH &
echo "Logstash 启动命令已执行,后台运行中..."
tail -f -n 30 nohup.out
配置文件
logstash
weblog.conf
input {
beats {
port => "9900" # 接收 Filebeat 日志的端口
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
mutate {
convert => { "bytes" => "integer" }
}
geoip {
source => "clientip"
}
useragent {
source => "user_agent"
target => "useragent"
}
date {
match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
target => "@timestamp"
}
}
output {
# 输出到 Elasticsearch,根据 index_prefix 动态生成索引
elasticsearch {
hosts => ["localhost:9200"]
index => "%{index_prefix}" # 动态索引
}
# 输出到控制台(调试用,可选)
stdout {
codec => rubydebug
}
}
filebeat
filebeat_apache.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /xx/xx/xx/redmoon/log/**/*.log
fields:
index_prefix: "test-wn-redmoon" # 设置 Index 前缀
fields_under_root: true # 将自定义字段放到顶层
ignore_older: 360h # 忽略超过 24 小时的日志(根据需求调整)
- type: log
enabled: true
paths:
- /xx/xx/xx-api/log/*.log
fields:
index_prefix: "test-wn-user-api" # 设置 Index 前缀
fields_under_root: true
ignore_older: 360h
- type: log
enabled: true
paths:
- /xx/xx/xx/admin/log/**/*.log
fields:
index_prefix: "test-wn-admin" # 设置 Index 前缀
fields_under_root: true
ignore_older: 360h
# output.elasticsearch:
# hosts: ["http://ip:9200"]
output.logstash:
hosts: ["ip:9900"] # 将日志发送到 Logstash
评论区