ELK与Grafana联合打造可视化监控来分析nginx日志

来自:网络
时间:2023-01-06
阅读:
目录

打造一个帅气的监控需要什么:

  • Grafana 前端数据分析平台
  • Elasticsearch 全文检索引擎
  • Logstash 日志收集处理框架
  • dashboard 监控面板出处

ELK与Grafana联合打造可视化监控来分析nginx日志

前提是elk集群和Grafana安装完毕,google上请自行搜索安装,这里不写了。

修改nginx打印日志格式

log_format  main  '{"@timestamp":"$time_iso8601",'
                  '"@source":"$server_addr",'
                  '"hostname":"$hostname",'
                  '"ip":"$remote_addr",'
                  '"client":"$remote_addr",'
                  '"request_method":"$request_method",'
                  '"scheme":"$scheme",'
                  '"domain":"$server_name",'
                  '"referer":"$http_referer",'
                  '"request":"$request_uri",'
                  '"args":"$args",'
                  '"size":$body_bytes_sent,'
                  '"status": $status,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamaddr":"$upstream_addr",'
                  '"http_user_agent":"$http_user_agent",'
                  '"https":"$https"'
                  '}';

安装logstash后,修改配置文件

[xxx@localhost ~]# cat  /etc/logstash/conf.d/nginx_access.conf 
input {
    file {
        ## 修改你环境nginx日志路径
        path => "/var/logs/xxx/access/*.log"
        ignore_older => 0 
    codec => json
    }
}

filter {
    mutate {
      convert => [ "status","integer" ]
      convert => [ "size","integer" ]
      convert => [ "upstreatime","float" ]
      convert => ["[geoip][coordinates]", "float"]
      remove_field => "message"
    }
#    grok {
#        patterns_dir => [ "/etc/logstash/patterns.d" ]
#        match => { "message" => "%{NGINXACCESS}"}
#    }
    date {
        match => [ "timestamp" ,"dd/MMM/YYYY:HH:mm:ss Z" ]
    }
    geoip {
      source => "client"  ##日志格式里的ip来源,这里是client这个字段(client":"$remote_addr")
      target => "geoip"
      database =>"/usr/share/GeoIP/GeoLite2-City.mmdb"   ##### 下载GeoIP库
      add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
      add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
    }
    mutate {
      remove_field => "timestamp"
    }
    if "_geoip_lookup_failure" in [tags] { drop { } } ### 如果解析的地址是内网IP geoip解析将会失败,会生成_geoip_lookup_failure字段,这段话的意思是如果内网地址 drop掉这个字段。
}

output {
        elasticsearch {
        hosts => ["xxx:9200","xxxx:9200","xxxx:9200"]
        index => "logstash-nginx-test-xxxx_%{+YYYY-MM}"
        user => xxxx
        password => xxxx
        }
        stdout { codec => rubydebug }
}

配置解析:

Logstash 分为 Input、Output、Filter、Codec 等多种plugins。

Input:数据的输入源也支持多种插件,如elk官网的beats、file、graphite、http、kafka、redis、exec等等等、、、

Output:数据的输出目的也支持多种插件,如本文的elasticsearch,当然这可能也是最常用的一种输出。以及exec、stdout终端、graphite、http、zabbix、nagios、redmine等等、、、

Filter:使用过滤器根据日志事件的特征,对数据事件进行处理过滤后,在输出。支持grok、date、geoip、mutate、ruby、json、kv、csv、checksum、dns、drop、xml等等、、

Codec:编码插件,改变事件数据的表示方式,它可以作为对输入或输出运行该过滤。和其它产品结合,如rubydebug、graphite、fluent、nmap等等。
具体以上插件的细节可以去官网,介绍的挺详细的。下面说下该篇中的配置文件的含义:

input段:

file:使用file 作为输入源

path: 日志的路径,支持/var/log*.log,及[ “/var/log/messages”, “/var/log/*.log” ] 格式 

start_position: 从文件的开始读取事件。另外还有end参数 

ignore_older : 忽略早于24小时(默认值86400)的日志,设为0,即关闭该功能,以防止文件中的事件由于是早期的被logstash所忽略。

filter段:

grok:数据结构化转换工具

match:匹配条件格式,将nginx日志作为message变量,并应用grok条件NGINXACCESS进行转换 

geoip:该过滤器从geoip中匹配ip字段,显示该ip的地理位置  

source:ip来源字段,这里我们选择的是日志文件中的最后一个字段,如果你的是默认的nginx日志,选择第一个字段即可(注:这里写的字段是/opt/logstash/patterns/nginx 里面定义转换后的)  

target:指定插入的logstash字断目标存储为geoip 

database:geoip数据库的存放路径  

add_field: 增加的字段,坐标经度  

add_field: 增加的字段,坐标纬度 

mutate: 数据的修改、删除、类型转换  

convert: 将坐标转为float类型  

convert: http的响应代码字段转换成 int  

convert: http的传输字节转换成int  

replace: 替换一个字段

remove_field: 移除message 的内容,因为数据已经过滤了一份,这里不必在用到该字段了。不然会相当于存两份

date: 时间处理,该插件很实用,主要是用你日志文件中事件的事件来对timestamp进行转换,导入老的数据必备!在这里曾让我困惑了很久哦。别再掉坑了  

match:匹配到timestamp字段后,修改格式为dd/MMM/yyyy:HH:mm:ss Z

mutate:数据修改 

remove_field: 移除timestamp字段。

output段:

elasticsearch:输出到es中  

host: es的主机ip+端口或者es 的FQDN+端口  

index: 为日志创建索引logstash-nginx-access-*,这里也就是kibana那里添加索引时的名称

GeoIP过滤器的版本4.0.0和更高版本使用MaxMind GeoLite2数据库并支持IPv4和IPv6查找。 4.0.0之前的版本使用传统的MaxMind GeoLite数据库,仅支持IPv4查找。

安装GeoIP:

cd /usr/local/src/
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
[root@localhost src]# cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

yum makecache
yum repolist
yum install geoipupdate
 vim  /etc/GeoIP.conf
ProductIds GeoLite2-City
mkdir /usr/share/GeoIP
geoipupdate 
ll /usr/share/GeoIP

启动logstash

nohup  /usr/share/logstash/bin/logstash -f  /etc/logstash/conf.d/nginx_access.conf  &

安装Grafana

配置Grafana数据源

1.进grafana面板,type选择elasticsearch

2.url填写http://127.0.0.1:9200, access选proxy

3.index-name写上之前配置文件里的索引名称

4.version选5.x

ELK与Grafana联合打造可视化监控来分析nginx日志

配置Grafana 画图模版

导入模版 搜索模版 

ELK与Grafana联合打造可视化监控来分析nginx日志

ELK与Grafana联合打造可视化监控来分析nginx日志

ELK与Grafana联合打造可视化监控来分析nginx日志

等一会就会出来文章开头的第一个图了 ~~

以上就是ELK Grafana打造可视化监控来分析nginx日志 的详细内容,更多关于ELK Grafana打造可视化监控来分析nginx日志 的资料请关注其它相关文章!

返回顶部
顶部