ELK與Grafana聯(lián)合打造可視化監(jiān)控來分析nginx日志
打造一個帥氣的監(jiān)控需要什么:
- Grafana 前端數據分析平臺
- 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 { } } ### 如果解析的地址是內網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等等。
具體以上插件的細節(jié)可以去官網,介紹的挺詳細的。下面說下該篇中的配置文件的含義:
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的傳輸字節(jié)轉換成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: 為日志創(chuàng)建索引logstash-nginx-access-*,這里也就是kibana那里添加索引時的名稱
GeoIP過濾器的版本4.0.0和更高版本使用MaxMind GeoLite2數據庫并支持IPv4和IPv6查找。 4.0.0之前的版本使用傳統(tǒng)的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

配置Grafana 畫圖模版



等一會就會出來文章開頭的第一個圖了 ~~
以上就是ELK Grafana打造可視化監(jiān)控來分析nginx日志 的詳細內容,更多關于ELK Grafana打造可視化監(jiān)控來分析nginx日志 的資料請關注腳本之家其它相關文章!
相關文章
Windows安裝nginx1.10.1反向代理訪問IIS網站
這篇文章主要為大家詳細介紹了Windows安裝nginx1.10.1反向代理訪問IIS網站的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Nginx使用limit_req_zone對同一IP訪問進行限流的方法
今天小編就為大家分享一篇Nginx使用limit_req_zone對同一IP訪問進行限流的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

