安装GeoIP数据库
cd /usr/local/logstash/etc
curl -O "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
gunzip GeoLiteCity.dat.gz
如果 gunzip 不存在 安装方法 : yum -y install gzip gunzip(centos) (apt-get install gzip gunzip)
filter {
geoip {
source => "cientIp" #设置解析IP地址的字段
target => "geoip" #将geoip数据保存到一个字段内
database => "/usr/local/logstash-2.3.2/etc/GeoLiteCity.dat" #IP地址数据库 可以不指定 使用默认
}
}
geoip 插件的 "source" 字段可以是任一处理后的字段,比如 "client_ip",但是字段内容却需要小心!geoip 库内只存有公共网络上的 IP 信息,查询不到结果的,会直接返回 null,而 logstash 的 geoip 插件对 null 结果的处理是:不生成对应的 geoip.字段。
input{
file {
path => "/usr/local/logs/access.log" #你的日志文件
start_position => beginning
ignore_older => 0
}
}
filter{
geoip{
source=>"ip"
}
}
output{
elasticsearch{
hosts=>"localhost"
index=>"domain"
document_id => "%{id}" #指定 对应 的 _id 防止 重复插入数据
}
}
{
"mappings": {
"my_type": {
"properties": {
"id": {
"type": "string"
},
"ip": {
"type": "string"
}
}
}
}
}
案例 1 自定义 字段转换 读取文件内容
input{
file {
path => "/usr/local/logs/access.log" #你的日志文件
start_position => beginning
ignore_older => 0
}
}
filter{
mutate{
gsub=>["message","\r",""]
}
mutate{
split=>["message",","]
}
mutate{
add_field=>{
"id"=>"%{[message][0]}"
"ip"=>"%{[message][1]}"
}
}
mutate{
remove_field=>"message"
remove_field=>"host"
remove_field=>"path"
}
geoip{
source=>"ip"
}
}
output{
elasticsearch{
hosts=>"localhost"
index=>"domain"
}
}
// 执行 echo '50,121.xx.xx.6' > access.log 或者 echo '50,121.xx.xx.6' >> access.log
案例 2 直接操作 logstash
input{
stdin{}
}
filter{
mutate{
gsub=>["message","\r",""]
}
mutate{
split=>["message",","]
}
mutate{
add_field=>{
"id"=>"%{[message][0]}"
"ip"=>"%{[message][1]}"
}
}
mutate{
remove_field=>"message"
remove_field=>"host"
remove_field=>"path"
}
geoip{
source=>"ip"
}
}
output{
elasticsearch{
hosts=>"localhost"
index=>"domain2"
}
}
// 运行 ./bin/logstsh
输入 121.xx.xx.xx,10(这个和logstsh 存储顺序相反)
GeoIp elasticsearch 搭建 继续上一步ELK 搭建
最新推荐文章于 2024-04-03 10:55:33 发布