influxdb+telegraf+grafana實現nginx監控

1、背景

公司業務某些頁面加載很慢,簡單經過日誌並不能確認究竟是哪些接口慢致使的,最近又在研究influxdb,因而計劃經過influxdb+telegraf+grafana實現一套nginx監控,用來分析接口的耗時。nginx

2、安裝

個人開發機是centos7,如下安裝命令都是在centos上的操做,其餘操做系統請參考官方文檔。apache

2.一、influxdb

wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.0.x86_64.rpm
sudo yum localinstall influxdb-1.8.0.x86_64.rpm
sudo systemctl start influxdb

這裏由於咱們只是測試驗證,因此使用默認配置便可,若是現網環境,仍是建議至少設置用戶、密碼等信息,默認influxdb的配置文件在/etc/influxdb/infludb.conf。centos

安裝啓動以後,能夠直接在終端輸入influx來嘗試登錄下看看。cookie

2.二、telegraf

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.14.1-1.x86_64.rpm
sudo yum localinstall telegraf-1.14.1-1.x86_64.rpm
sudo systemctl start telegraf

這裏telegraf也先使用默認的配置啓動起來,後續配置在下一節詳細討論。app

2.三、grafana

1 wget https://dl.grafana.com/oss/release/grafana-6.7.2-1.x86_64.rpm
2 sudo yum localinstall grafana-6.7.2-1.x86_64.rpm
3 sudo systemctl start grafana-server

grafana也是使用默認的配置,啓動以後就能夠經過http://your-ip:3000來訪問grafana了。ide

 

 

 

3、配置

3.一、nginx log_format配置

本次監控的思路是經過telegraf抓取nginx的日誌,並將信息導入到influxdb中,而後經過grafana將耗時數據展現出來,因此第一步咱們須要先配置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 $host '
                      '"$upstream_addr" $upstream_status $request_time '
                      '$upstream_response_time "$cookie_uin" "$cookie_luin" "$cookie_username"';

雖然其中許多對於這裏的監控不是必須的,可是因爲要和現網保持一致,因此若是要跟隨這篇文檔來配置監控的話,建議也使用這個配置,由於這個日誌的配置還會影響到telegraf日誌解析的配置,這個地方有點麻煩,因此建議使用一樣的配置,避免遇到奇葩的問題,待整個流程跑通了以後,能夠再去熟悉telegraf日誌解析的格式,進而根據須要調整本身的log_format。ui

3.二、telegraf input插件配置

這裏使用了logparser插件來抓取日誌,並使用grok格式來解析日誌,一下是配置文件:centos7

 1 # Stream and parse log file(s).
 2 [[inputs.logparser]]
 3   ## Log files to parse.
 4   ## These accept standard unix glob matching rules, but with the addition of
 5   ## ** as a "super asterisk". ie:
 6   ##   /var/log/**.log     -> recursively find all .log files in /var/log
 7   ##   /var/log/*/*.log    -> find all .log files with a parent dir in /var/log
 8   ##   /var/log/apache.log -> only tail the apache log file
 9   files = ["/var/log/nginx/access.log"]
10 
11   ## Read files that currently exist from the beginning. Files that are created
12   ## while telegraf is running (and that match the "files" globs) will always
13   ## be read from the beginning.
14   from_beginning = false
15 
16   ## Method used to watch for file updates.  Can be either "inotify" or "poll".
17   # watch_method = "inotify"
18 
19   ## Parse logstash-style "grok" patterns:
20   [inputs.logparser.grok]
21     ## This is a list of patterns to check the given log file(s) for.
22     ## Note that adding patterns here increases processing time. The most
23     ## efficient configuration is to have one pattern per logparser.
24     ## Other common built-in patterns are:
25     ##   %{COMMON_LOG_FORMAT}   (plain apache & nginx access logs)
26     ##   %{COMBINED_LOG_FORMAT} (access logs + referrer & agent)
27     # patterns = ["%{COMMON_LOG_FORMAT}"]
28     patterns = ["%{NGINX_ACCESS_LOG}"]
29 
30     ## Name of the outputted measurement name.
31     measurement = "nginx_access_log"
32 
33     ## Full path(s) to custom pattern files.
34     custom_pattern_files = []
35 
36     ## Custom patterns can also be defined here. Put one pattern per line.
37     custom_patterns = '''
38     NGINX_ACCESS_LOG %{IP:remote_addr} - (-|%{WORD:remote_user}) \[%{HTTPDATE:time_local}\] %{QS:request} %{NUMBER:status:int} %{NUMBER:body_bytes_sent:int} %{QS:referrer} %{QS:agent} %{IPORHOST:xforwardedfor} %{IPORHOST:host} %{QS:upstream_addr} (-|%{NUMBER:upstream_status:int}) %{BASE10NUM:request_time:float} (-|%{BASE10NUM:upstream_response_time:float}) %{QS:cookie_uin} %{QS:cookie_luin}
39     '''
40 
41     ## Timezone allows you to provide an override for timestamps that
42     ## don't already include an offset
43     ## e.g. 04/06/2016 12:41:45 data one two 5.43µs
44     ##
45     ## Default: "" which renders UTC
46     ## Options are as follows:
47     ##   1. Local             -- interpret based on machine localtime
48     ##   2. "Canada/Eastern"  -- Unix TZ values like those found in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
49     ##   3. UTC               -- or blank/unspecified, will return timestamp in UTC
50     # timezone = "Canada/Eastern"
51 
52     ## When set to "disable", timestamp will not incremented if there is a
53     ## duplicate.
54     # unique_timestamp = "auto"

其中最核心的地方是NGINX_ACCESS_LOG的配置,它決定了如何去解析nginx的日誌,這裏使用到了grok的語法,更多關於grok的信息見參考資料。spa

修改以後記得使用systemctl restart telegraf命令使改動生效。

3.三、grafana的配置

grafana須要先配置influxdb數據源,具體操做能夠看下grafana的文檔,這裏說一下如何配置從influxdb查詢數據並將數據展現出來,這裏主要用到了日誌中解析出來的request_time。

 

 

4、效果

 

 

 這裏能夠看到,很清晰明瞭的把nginx的調用耗時展現了出來,可是到這裏並無結束,由於並無達到咱們的目的:定位哪些接口查詢較慢,目前僅僅實現了一個全局的視圖,可以知道目前有沒有接口很慢,可是不知道哪一個接口慢(除非直接查influxdb),我思考了一下,這是由於其實grafana只是用來展現metric的,metric更多的是關注統計,而哪一個接口慢更多的是須要關注個體,這裏是grafana很難實現的(除非一個接口一個query,那配置起來就太麻煩了),因此要解決咱們的問題實際上是須要引入全鏈路追蹤系統的,這個目前還在調研中,待部署上線了再寫篇文章介紹一下全鏈路追蹤。

5、參考資料

一、grok patterns:http://grokdebug.herokuapp.com/patterns#二、grok debug:http://grokdebug.herokuapp.com/三、telegraf文檔:https://docs.influxdata.com/telegraf/v1.14/四、grok input data format:https://docs.influxdata.com/telegraf/v1.14/data_formats/input/grok/

相關文章
相關標籤/搜索