ELK收集nginx日志并用高德地图展示出IP
admin
2023-03-19 12:21:29
0

(一)测试的环境

agentd:192.168.180.22

ES:192.168.180.23

kibana:192.168.180.23

采用的拓扑:logstash -->ES-->kibana


(二)实施步骤:

   (1)logstsh具体配置:

1,配置nginx日志格式,采用log_format格式:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

2,在logstash服务器下载IP地址归类查询库

[root@localhost config]# cd /usr/local/logstash/config/
[root@localhost config]#  wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.
gz

3,配置logstash客户端

[root@localhost config]# vim /usr/local/logstash/config/nginx-access.conf 
input {
        file {
                path => "/opt/access.log"
                type => "nginx"
                start_position => "beginning"
              }
            }
filter {
          grok {
                match => { "message" => "%{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] \"%{WO
RD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}\" %{INT:status} %{INT:body_bytes_sent} %
{QS:http_referer} %{QS:http_user_agent}"
                                }
                     }
 geoip {
     source => "remote_addr"
     target => "geoip"
     database => "/usr/local/logstash/config/GeoLite2-City.mmdb"
     add_field => ["[geoip][coordinates]","%{[geoip][longitude]}"]
     add_field => ["[geoip][coordinates]","%{[geoip][latitude]}"]
                }
   }
   
output {
                elasticsearch {
                        hosts => ["192.168.180.23:9200"]
                        manage_template => true                                     
                        index => "logstash-map-%{+YYYY-MM}"                         
                          }                                                                   
             }

备注:

  • geoip: IP查询插件

  • source: 需要通过geoip插件处理的field,一般为ip,这里因为通过控制台手动输入的是ip所以直接填message,生成环境中如果查询nginx访问用户,需先将客户端ip过滤出来,然后这里填remote_addr即可

  • target: 解析后的Geoip地址数据,应该存放在哪一个字段中,默认是geoip这个字段

  • database: 指定下载的数据库文件

  • add_field: 这里两行是添加经纬度,地图中地区显示是根据经纬度来识

。如果启动正常的话,可以在kibana看到geoip相关的字段,如下图:

ELK收集nginx日志并用高德地图展示出IP

3,启动logstash客户端并加载刚才的配置文件。

[root@localhost config]# /usr/local/logstash/bin/logstash -f nginx-access.conf  
Sending Logstash's logs to /usr/local/logstash/logs which is now configured via log4j2.properties
[2017-06-20T22:55:23,801][INFO ][logstash.outputs.elasticsearch] Elasticsearch pool URLs updated {:changes=>{:removed=>[], :added=>[http://192.168.180.23:9200/]}}
[2017-06-20T22:55:23,805][INFO ][logstash.outputs.elasticsearch] Running health check to see if an Elasticsearch connection is working {:healthcheck_url=>http://192.168.180.23:9200/, :path=>"/"}
[2017-06-20T22:55:23,901][WARN ][logstash.outputs.elasticsearch] Restored connection to ES instance {:url=>#}
[2017-06-20T22:55:23,909][INFO ][logstash.outputs.elasticsearch] Using mapping template from {:path=>nil}
[2017-06-20T22:55:23,947][INFO ][logstash.outputs.elasticsearch] Attempting to install template {:manage_template=>{"template"=>"logstash-*", "version"=>50001, "settings"=>{"index.refresh_interval"=>"5s"}, "mappings"=>{"_default_"=>{"_all"=>{"enabled"=>true, "norms"=>false}, "dynamic_templates"=>[{"message_field"=>{"path_match"=>"message", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false}}}, {"string_fields"=>{"match"=>"*", "match_mapping_type"=>"string", "mapping"=>{"type"=>"text", "norms"=>false, "fields"=>{"keyword"=>{"type"=>"keyword"}}}}}], "properties"=>{"@timestamp"=>{"type"=>"date", "include_in_all"=>false}, "@version"=>{"type"=>"keyword", "include_in_all"=>false}, "geoip"=>{"dynamic"=>true, "properties"=>{"ip"=>{"type"=>"ip"}, "location"=>{"type"=>"geo_point"}, "latitude"=>{"type"=>"half_float"}, "longitude"=>{"type"=>"half_float"}}}}}}}}
[2017-06-20T22:55:23,955][INFO ][logstash.outputs.elasticsearch] New Elasticsearch output {:class=>"LogStash::Outputs::ElasticSearch", :hosts=>[#]}
[2017-06-20T22:55:24,065][INFO ][logstash.filters.geoip   ] Using geoip database {:path=>"/usr/local/logstash/config/GeoLite2-City.mmdb"}
[2017-06-20T22:55:24,094][INFO ][logstash.pipeline        ] Starting pipeline {"id"=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>500}
[2017-06-20T22:55:24,275][INFO ][logstash.pipeline        ] Pipeline main started
[2017-06-20T22:55:24,369][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}

(2)Kibana配置.

1,编辑修改kibana的配置文件kibana.yml在最后添加如下:

# The default locale. This locale can be used in certain circumstances to substitute any missing
# translations.
#i18n.defaultLocale: "en"

tilemap.url: 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&
z={z}'

2,重启kibana服务。

[root@localhost bin]# /usr/local/kibana/bin/kibana &
[1] 10631
[root@localhost bin]# ps -ef|grep kibana            
root     10631  7795 21 10:52 pts/0    00:00:02 /usr/local/kibana/bin/../node/bin/node --no-warnings /usr/local/kibana/bin/../src/cli
root     10643  7795  0 10:52 pts/0    00:00:00 grep --color=auto kibana
[root@localhost bin]#   log   [02:52:59.297] [info][status][plugin:kibana@5.4.0] Status changed from uninitialized to green - Ready
  log   [02:52:59.445] [info][status][plugin:elasticsearch@5.4.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
  log   [02:52:59.482] [info][status][plugin:console@5.4.0] Status changed from uninitialized to green - Ready
  log   [02:52:59.512] [info][status][plugin:elasticsearch@5.4.0] Status changed from yellow to green - Kibana index ready
  log   [02:52:59.513] [info][status][plugin:metrics@5.4.0] Status changed from uninitialized to green - Ready
  log   [02:53:00.075] [info][status][plugin:timelion@5.4.0] Status changed from uninitialized to green - Ready
  log   [02:53:00.080] [info][listening] Server running at http://192.168.180.23:5601
  log   [02:53:00.081] [info][status][ui settings] Status changed from uninitialized to green - Ready

3,创建nginx的访问索引lostash-map*。具体步骤如下:ip:5601--->Management--Index Patterns--->+--->在Index name or pattern中添加logstash-map*--->create。具体如下图:

ELK收集nginx日志并用高德地图展示出IP

4,创建Visualize。具体步骤如下:Visalize--->+--->Maps(Tile Maps)

ELK收集nginx日志并用高德地图展示出IPELK收集nginx日志并用高德地图展示出IP

已上就是简单的步骤。

相关内容

热门资讯

AI开源公开信签署企业增至50... IT之家 7 月 26 日消息,英伟达、微软、IBM、Meta 等 25 家美国科技企业本周(7 月...
南阳快手短视频代运营企业的服务... 在短视频平台快速发展的背景下,企业通过快手等渠道进行内容运营已成为常见的市场动作。对于希望借助短视频...
普通人该如何跟上科技发展的步伐 这个被称作科技的词汇, 听起来既有宏大之感, 又颇为遥远, 好似仅仅归属于在实验室里专心致志钻研的科...
原创 王... 菲尔兹奖名单一出,邓煜和王虹两位华人数学家共同获奖,本应是一件令人振奋的喜讯。然而,王虹那段仅3分1...
《万物》杂志首个阅读空间亮相正... 26日,由国内知名的青少年家庭科普期刊《万物》杂志与广州正佳自然科学探索中心联合打造的阅读互动空间—...
从康复机器人到脑机接口 智能康... 天津北方网讯:对脑卒中患者来说,重新抬起手臂、迈出一步,往往需要持续的康复训练,逐步恢复运动功能;对...
原创 苹... 当苹果、谷歌、斯坦福、麻省理工都无法留住一位年轻的中国AI天才时,究竟是什么力量吸引他毅然回国?为何...
为什么新能源车供应链要本地化? 当前汽车产业变革加速,电动化成为核心发展方向。车企为了在激烈市场中站稳脚跟,必须优化生产结构。其中,...
携程罚单的真正影响,不是酒店变... 文 | 听风译码 7月25日,市场监管总局对携程开出51.79亿元天价罚单。 全网叫好。 但如果你...
自己管理自己,我国成功发射两颗... 7月24日消息,力箭一号遥十五运载火箭今日在东风商业航天创新试验区成功发射,以“一箭5星”方式将5颗...