目錄
- 修改nginx打印日志格式
- 安裝logstash后,修改配置文件
- 配置解析:
- input段:
- filter段:
- output段:
- 安裝GeoIP:
- 安裝Grafana
- 配置Grafana數(shù)據(jù)源
- 配置Grafana 畫圖模版
打造一個(gè)帥氣的監(jiān)控需要什么:
- Grafana 前端數(shù)據(jù)分析平臺(tái)
- Elasticsearch 全文檢索引擎
- Logstash 日志收集處理框架
- dashboard 監(jiān)控面板出處
前提是elk集群和Grafana安裝完畢,google上請(qǐng)自行搜索安裝,這里不寫了。
修改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來(lái)源,這里是client這個(gè)字段(client":"$remote_addr") target => "geoip" database =>"/usr/share/GeoIP/GeoLite2-City.mmdb" ##### 下載GeoIP庫(kù) 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解析將會(huì)失敗,會(huì)生成_geoip_lookup_failure字段,這段話的意思是如果內(nèi)網(wǎng)地址 drop掉這個(gè)字段。 } 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
:使用過(guò)濾器根據(jù)日志事件的特征,對(duì)數(shù)據(jù)事件進(jìn)行處理過(guò)濾后,在輸出。支持grok、date、geoip、mutate、ruby、json、kv、csv、checksum、dns、drop、xml等等、、
Codec
:編碼插件,改變事件數(shù)據(jù)的表示方式,它可以作為對(duì)輸入或輸出運(yùn)行該過(guò)濾。和其它產(chǎn)品結(jié)合,如rubydebug、graphite、fluent、nmap等等。
具體以上插件的細(xì)節(jié)可以去官網(wǎng),介紹的挺詳細(xì)的。下面說(shuō)下該篇中的配置文件的含義:
input段:
file
:使用file 作為輸入源
path
: 日志的路徑,支持/var/log*.log,及[ “/var/log/messages”, “/var/log/*.log” ] 格式
start_position
: 從文件的開(kāi)始讀取事件。另外還有end參數(shù)
ignore_older
: 忽略早于24小時(shí)(默認(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
:該過(guò)濾器從geoip中匹配ip字段,顯示該ip的地理位置
source
:ip來(lái)源字段,這里我們選擇的是日志文件中的最后一個(gè)字段,如果你的是默認(rèn)的nginx日志,選擇第一個(gè)字段即可(注:這里寫的字段是/opt/logstash/patterns/nginx 里面定義轉(zhuǎn)換后的)
target
:指定插入的logstash字?jǐn)嗄繕?biāo)存儲(chǔ)為geoip
database:geoip
數(shù)據(jù)庫(kù)的存放路徑
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
: 替換一個(gè)字段
remove_field
: 移除message 的內(nèi)容,因?yàn)閿?shù)據(jù)已經(jīng)過(guò)濾了一份,這里不必在用到該字段了。不然會(huì)相當(dāng)于存兩份
date
: 時(shí)間處理,該插件很實(shí)用,主要是用你日志文件中事件的事件來(lái)對(duì)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那里添加索引時(shí)的名稱
GeoIP過(guò)濾器的版本4.0.0和更高版本使用MaxMind GeoLite2數(shù)據(jù)庫(kù)并支持IPv4和IPv6查找。 4.0.0之前的版本使用傳統(tǒng)的MaxMind GeoLite數(shù)據(jù)庫(kù),僅支持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
啟動(dòng)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)入模版 搜索模版
等一會(huì)就會(huì)出來(lái)文章開(kāi)頭的第一個(gè)圖了 ~~
以上就是ELK Grafana打造可視化監(jiān)控來(lái)分析nginx日志 的詳細(xì)內(nèi)容,更多關(guān)于ELK Grafana打造可視化監(jiān)控來(lái)分析nginx日志 的資料請(qǐng)關(guān)注其它相關(guān)文章!