GeoIp elasticsearch 搭建 继续上一步ELK 搭建

安装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 存储顺序相反)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值