通用技术 ELK-kibana 来源热点图

xinxi · 2020年02月05日 · 最后由 活着丶 回复于 2020年03月20日 · 4012 次阅读
本帖已被设为精华帖!

介绍

通常我们拿到 nignx 日志里边有很多请求相关日志,比如设备、设备 ip、agent 等参数.

统计设备 ip 在全国分布次数,大致能分支大部分 app 应用在全国各城市及地区的使用情况.

geoip

Geolocation IP,基于 IP 查询的地理位置的意思

geoip 数据库

https://www.maxmind.com/zh/geoip2-city

下载 GeoLite2-City.mmdb 内置数据,可以使用 GeoLite2-City.mmdb 数据库把 ip 换成经纬度.

python 库 geoip2

python 也提供了 geoip2 库,可以快速查询一个 ip 的地理位置

安装

pip install geoip2

脚本

import json
import geoip2.database

reader = geoip2.database.Reader('./static/GeoLite2-City.mmdb')

response = reader.city('118.254.223.207')

print(response.country.iso_code)
print(response.city.name)
print(response.location.latitude)
print(response.location.longitude)

image

数据准备

数据是从网关日志中提取最近 5 分钟的请求.

json 格式,其中里边包含了"client_ip"客户端 ip.

通过 logstash 中自带的 geoip 插件,把 ip 转换成经纬度.

image

logstash 配置

从本地加载一个 json 日志

input {

  file {
        path => "/Users/xinxi/Documents/sndd/crawlerfeedback/static/downloaded_data.log"
        start_position => beginning
        codec => "json"
  }
}

filter {
    geoip {
            source => "client_ip"
            # 指定需要的字段
            # fields => ["country_name", "continent_code", "region_name", "city_name", "latitude", "longitude"]

    }
}


output {
  elasticsearch {
        hosts => ["192.168.1.232:9200"]
        index => "logstash-nginx-json-%{+YYYY.MM.dd}"
  }    
  # 输出到elk中
  stdout {
    codec => rubydebug
    # 输出到屏幕上
  }
}

执行命令

-e:在命令行执行;input 输入,stdin 标准输入,是一个插件;output 输出,stdout:标准输出。默认输出格式是使用 rubudebug 显示详细输出,codec 为一种编解码器

logstash -e 'input {stdin{}} output {stdout{}}'

将屏幕输入的字符串输出到 elasticsearch 服务中

logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["127.0.0.1:9200"] } }'

增加一个 filter 过滤去

logstash -e 'input { stdin {} } filter{ geoip { source=>"message" fields => ["country_name","region_name","city_name","location"] } } output { elasticsearch {  hosts => ["192.168.1.232:9200"] index => "logstash-geoip-data-%{+YYYY.MM.dd}"  } stdout { codec=>rubydebug } }'

image

执行配置文件

/data/logstash-6.5.1/bin/logstash -f logstah_agent.conf --path.data=/logstash3

这是通过 geoip 解析出来的位置

image

kibana 中配置 MAP

使用 CoordinateMap

image

配置数据

使用 Genhash 聚合,geoiplocation 作为字段

image

image

参考

Logstash Geoip 插件使用

https://www.jianshu.com/p/1b4104b28f70

logstash geoip 库测试

https://www.jianshu.com/p/4deeecd5bff8

ELK 日志分析系统之 logstash7.x 最新版安装与配置

https://www.cnblogs.com/eeexu123/p/11607422.html

ELK-基于用户访问 IP 做用户热力地图展示

https://blog.51cto.com/183530300/2309898

ELK-kibana 来源热点图

https://blog.csdn.net/Gmoon23/article/details/79261706

共收到 5 条回复 时间 点赞

👍🏻

恒温 将本帖设为了精华贴 02月06日 10:59

高产王

这个技术真不错

很高大上,我也玩玩

库是收费的吗

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册