<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          不好意思,ELK 該換了!

          共 14435字,需瀏覽 29分鐘

           ·

          2024-04-12 03:19

          最近客戶(hù)有個(gè)新需求,就是想查看網(wǎng)站的訪問(wèn)情況,由于網(wǎng)站沒(méi)有做google的統(tǒng)計(jì)和百度的統(tǒng)計(jì),所以訪問(wèn)情況,只能通過(guò)日志查看,通過(guò)腳本的形式給客戶(hù)導(dǎo)出也不太實(shí)際,給客戶(hù)寫(xiě)個(gè)簡(jiǎn)單的頁(yè)面,咱也做不到

          成熟的日志解決方案,那就是ELK,還有現(xiàn)在比較火的Loki,(當(dāng)然還有很多其他解決方案,比如Splunk、Datadog等),那我們這個(gè)小網(wǎng)站,小體量,必然是選擇Loki來(lái)做。

          所以這次就采用 Nginx+Promtail+Loki+Grafana 來(lái)做一個(gè)簡(jiǎn)單的Nginx日志展示

          5943aa2746420e073d75d9915c8c8529.webp

          Nginx的安裝不多說(shuō),Promtail和Loki都選用二進(jìn)制的方式進(jìn)行安裝,直接下載對(duì)應(yīng)版本的二進(jìn)制文件,解壓后指定配置文件啟動(dòng)即可。

          其中promtail配置文件如下配置:

              
                
                server:
            http_listen_port: 9080
            grpc_listen_port0

          positions:
            filename: /tmp/positions.yaml

          clients:
            - url: http://localhost:3100/loki/api/v1/push

          scrape_configs:
          - job_name: nginx
            pipeline_stages:
            - replace:
                expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})'
                replace'***'
            static_configs:
            - targets:
                - localhost
              labels:
                job: nginx_access_log
                host: expatsxxxxs
                agent: promtail
                __path__/var/log/nginx/expatshxxxxs.access.log

          日志收集工作完成后,在Nginx中,需要修改日志格式,修改Nginx的日志格式為Json格式,配置如下:

              
                
                log_format json_analytics escape=json '{'
                                      '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                                      '"connection": "$connection", ' # connection serial number
                                      '"connection_requests": "$connection_requests", ' # number of requests made in connection
                              '"pid": "$pid", ' # process pid
                              '"request_id": "$request_id", ' # the unique request id
                              '"request_length": "$request_length", ' # request length (including headers and body)
                              '"remote_addr": "$remote_addr", ' # client IP
                              '"remote_user": "$remote_user", ' # client HTTP username
                              '"remote_port": "$remote_port", ' # client port
                              '"time_local": "$time_local", '
                              '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                              '"request": "$request", ' # full path no arguments if the request
                              '"request_uri": "$request_uri", ' # full path and arguments if the request
                              '"args": "$args", ' # args
                              '"status": "$status", ' # response status code
                              '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                              '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                              '"http_referer": "$http_referer", ' # HTTP referer
                              '"http_user_agent": "$http_user_agent", ' # user agent
                              '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                              '"http_host": "$http_host", ' # the request Host: header
                              '"server_name": "$server_name", ' # the name of the vhost serving the request
                              '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                              '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                              '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                              '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                              '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                              '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                              '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                              '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                              '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                              '"scheme": "$scheme", ' # http or https
                              '"request_method": "$request_method", ' # request method
                              '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                              '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                              '"gzip_ratio": "$gzip_ratio", '
                              '"http_cf_ray": "$http_cf_ray",'
                              '"geoip_country_code": "$geoip_country_code"'
                              '}';

          配置文件中,需要注意的是geoip的配置,需要在nginx中添加geoip模塊

          首先通過(guò)yum安裝geoip及l(fā)ib庫(kù)

              
              yum -y install GeoIP GeoIP-data GeoIP-devel

          之后,重新編譯Nginx,通過(guò)--with-http_geoip_module添加nginx的geoip模塊

          編譯完成后,在objs目錄下生成新的Nginx可執(zhí)行文件,替換原先的,通過(guò)kill -USR2信號(hào),升級(jí)Nginx

          此時(shí)配置的log_format不會(huì)再報(bào)錯(cuò)geoip指令找不到,配置完成后,配置網(wǎng)站的access日志,引用剛才配置的json格式的log_format。

          查看此時(shí)的日志格式

              
              {"msec""1633430998.322""connection""4""connection_requests""1""pid""29887""request_id""40770fec38c2e5a68714df5f7a67283d""request_length""392""remote_addr""106.19.96.55""remote_user""""remote_port""43746""time_local""05/Oct/2021:18:49:58 +0800""time_iso8601""2021-10-05T18:49:58+08:00""request""GET / HTTP/2.0""request_uri""/""args""""status""200""body_bytes_sent""60949""bytes_sent""61222""http_referer""https://cn.bing.com/search?q=expat+tourism+agent+in+china&go=Search&qs=n&form=QBRE&sp=-1&pq=expat+tourism+agent+in+chi&sc=0-26&sk=&cvid=8BD2D4B79B3A4FA682571CB5BC7334D4""http_user_agent""Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1""http_x_forwarded_for""""http_host""www.expatsholidays.com""server_name""www.expatsholidays.com""request_time""0.003""upstream""127.0.0.1:9000""upstream_connect_time""0.000""upstream_header_time""0.002""upstream_response_time""0.002""upstream_response_length""60991""upstream_cache_status""""ssl_protocol""TLSv1.2""ssl_cipher""ECDHE-RSA-AES128-GCM-SHA256""scheme""https""request_method""GET""server_protocol""HTTP/2.0""pipe"".""gzip_ratio""""http_cf_ray""","geoip_country_code"""}
          {"msec""1633430998.709""connection""4""connection_requests""2""pid""29887""request_id""430fd53a457ea580c47e9b055da2b4d0""request_length""56""remote_addr""106.19.96.55""remote_user""""remote_port""43746""time_local""05/Oct/2021:18:49:58 +0800""time_iso8601""2021-10-05T18:49:58+08:00""request""GET /maps/api/js?ver=5.6.1 HTTP/2.0""request_uri""/maps/api/js?ver=5.6.1""args""ver=5.6.1""status""200""body_bytes_sent""41262""bytes_sent""41406""http_referer""https://www.expatsholidays.com/""http_user_agent""Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1""http_x_forwarded_for""""http_host""www.expatsholidays.com""server_name""www.expatsholidays.com""request_time""0.000""upstream""""upstream_connect_time""""upstream_header_time""""upstream_response_time""""upstream_response_length""""upstream_cache_status""""ssl_protocol""TLSv1.2""ssl_cipher""ECDHE-RSA-AES128-GCM-SHA256""scheme""https""request_method""GET""server_protocol""HTTP/2.0""pipe"".""gzip_ratio""3.06""http_cf_ray""","geoip_country_code"""}

          已經(jīng)轉(zhuǎn)為JSON格式,接著安裝grafana,最快捷的方式,通過(guò)docker直接起一個(gè)

              
              docker run -d -p 3000:3000 grafana/grafana

          啟動(dòng)后,通過(guò)admin/admin默認(rèn)用戶(hù)名密碼登錄,登陸后提示重置密碼,之后進(jìn)入grafana界面

          我們直接添加數(shù)據(jù)源loki

          5e7ff3f90bf11925c174559d77463ab1.webp

          先通過(guò)explore查詢(xún),日志是否存入loki

          323ab6bfc551afe780500e994580be99.webp

          可以看到,日志已經(jīng)存入loki

          接著添加Dashboard,通過(guò)ID導(dǎo)入

          6867fcf6b05ee0786aedfaeec2c5554b.webp

          導(dǎo)入如下

          d52e458e3299ad2dec3174a6a1c7fd12.webp

          0ce798bb05f7ef9762690e22f2d577bd.webp

          可以看到,這里地圖沒(méi)展示,缺少grafana-worldmap-panel插件,我們裝一下,通過(guò)命令行

              
              grafana-cli plugins install grafana-worldmap-panel

          安裝完成后,重啟grafana,重新查看dashboard

          5943aa2746420e073d75d9915c8c8529.webp

          地圖可以展示,這里因?yàn)榈貓D的地址是國(guó)外的地址,所以地圖的圖片無(wú)法加載,可以通過(guò)反代的方式解決

          ok,今天內(nèi)容就這些,通過(guò)簡(jiǎn)單快速的方式,為客戶(hù)呈現(xiàn)網(wǎng)站的詳細(xì)訪問(wèn)情況,Loki+Grafana是絕佳的組合。

          瀏覽 69
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中文字幕青娱乐 | 精品成人Av一区二区三区 | 能免费看的黄色电影 | 视频高清123区 | 亚洲视频ⅴ√ |