" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ELK與Grafana聯(lián)合打造可視化監(jiān)控來分析nginx日志

 更新時間:2022年03月22日 08:35:13   作者:GeekXuShuo  
這篇文章主要為大家介紹了ELK與Grafana的聯(lián)合打造可視化監(jiān)控來分析nginx日志,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

打造一個帥氣的監(jiān)控需要什么:

  • Grafana 前端數(shù)據(jù)分析平臺
  • Elasticsearch 全文檢索引擎
  • Logstash 日志收集處理框架
  • dashboard 監(jiān)控面板出處

在這里插入圖片描述

前提是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 {
        ## 修改你環(huán)境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 { } } ### 如果解析的地址是內(nèi)網(wǎng)IP geoip解析將會失敗,會生成_geoip_lookup_failure字段,這段話的意思是如果內(nèi)網(wǎng)地址 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:數(shù)據(jù)的輸入源也支持多種插件,如elk官網(wǎng)的beats、file、graphite、http、kafka、redis、exec等等等、、、

Output:數(shù)據(jù)的輸出目的也支持多種插件,如本文的elasticsearch,當(dāng)然這可能也是最常用的一種輸出。以及exec、stdout終端、graphite、http、zabbix、nagios、redmine等等、、、

Filter:使用過濾器根據(jù)日志事件的特征,對數(shù)據(jù)事件進(jìn)行處理過濾后,在輸出。支持grok、date、geoip、mutate、ruby、json、kv、csv、checksum、dns、drop、xml等等、、

Codec:編碼插件,改變事件數(shù)據(jù)的表示方式,它可以作為對輸入或輸出運行該過濾。和其它產(chǎn)品結(jié)合,如rubydebug、graphite、fluent、nmap等等。
具體以上插件的細(xì)節(jié)可以去官網(wǎng),介紹的挺詳細(xì)的。下面說下該篇中的配置文件的含義:

input段:

file:使用file 作為輸入源

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

start_position: 從文件的開始讀取事件。另外還有end參數(shù) 

ignore_older : 忽略早于24小時(默認(rèn)值86400)的日志,設(shè)為0,即關(guān)閉該功能,以防止文件中的事件由于是早期的被logstash所忽略。

filter段:

grok:數(shù)據(jù)結(jié)構(gòu)化轉(zhuǎn)換工具

match:匹配條件格式,將nginx日志作為message變量,并應(yīng)用grok條件NGINXACCESS進(jìn)行轉(zhuǎn)換 

geoip:該過濾器從geoip中匹配ip字段,顯示該ip的地理位置  

source:ip來源字段,這里我們選擇的是日志文件中的最后一個字段,如果你的是默認(rèn)的nginx日志,選擇第一個字段即可(注:這里寫的字段是/opt/logstash/patterns/nginx 里面定義轉(zhuǎn)換后的)  

target:指定插入的logstash字?jǐn)嗄繕?biāo)存儲為geoip 

database:geoip數(shù)據(jù)庫的存放路徑  

add_field: 增加的字段,坐標(biāo)經(jīng)度  

add_field: 增加的字段,坐標(biāo)緯度 

mutate: 數(shù)據(jù)的修改、刪除、類型轉(zhuǎn)換  

convert: 將坐標(biāo)轉(zhuǎn)為float類型  

convert: http的響應(yīng)代碼字段轉(zhuǎn)換成 int  

convert: http的傳輸字節(jié)轉(zhuǎn)換成int  

replace: 替換一個字段

remove_field: 移除message 的內(nèi)容,因為數(shù)據(jù)已經(jīng)過濾了一份,這里不必在用到該字段了。不然會相當(dāng)于存兩份

date: 時間處理,該插件很實用,主要是用你日志文件中事件的事件來對timestamp進(jìn)行轉(zhuǎn)換,導(dǎo)入老的數(shù)據(jù)必備!在這里曾讓我困惑了很久哦。別再掉坑了  

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

mutate:數(shù)據(jù)修改 

remove_field: 移除timestamp字段。

output段:

elasticsearch:輸出到es中  

host: es的主機(jī)ip+端口或者es 的FQDN+端口  

index: 為日志創(chuàng)建索引logstash-nginx-access-*,這里也就是kibana那里添加索引時的名稱

GeoIP過濾器的版本4.0.0和更高版本使用MaxMind GeoLite2數(shù)據(jù)庫并支持IPv4和IPv6查找。 4.0.0之前的版本使用傳統(tǒng)的MaxMind GeoLite數(shù)據(jù)庫,僅支持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數(shù)據(jù)源

1.進(jìn)grafana面板,type選擇elasticsearch

2.url填寫http://127.0.0.1:9200, access選proxy

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

4.version選5.x

在這里插入圖片描述

配置Grafana 畫圖模版

導(dǎo)入模版 搜索模版 

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

等一會就會出來文章開頭的第一個圖了 ~~

以上就是ELK Grafana打造可視化監(jiān)控來分析nginx日志 的詳細(xì)內(nèi)容,更多關(guān)于ELK Grafana打造可視化監(jiān)控來分析nginx日志 的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論