跳到主要內容

FluentD 實作 Nginx Access Log 補充

FluentD 實作 Nginx Access Log 補充


前一篇針對 FluentD 安裝 及 Nginx Access log format 設定提供範例

本篇補充
1. 將 access_log 存入 MySQL
2. 針對Input 加工,ex 解析 Path 拆成不同欄位,在傳入 Output

延伸閱讀


FluentD 參數說明
FluentD 實作 Nginx Access Log

將 access_log 存入 MySQL


<worker 0>
  <source>
  
   ... 略

  </source>
  <match nginx.web.access>
    @type copy
     
     ... 略
  
    <store>
      @type sql
      host  ${MySQL Host address}
      port  ${MySQL Port}
      adapter mysql2
      database  ${MySQL Database}
      username  ${MySQL User Name}
      password  ${MySQL Password}
      <table>
        table  ${MySQL table}
        column_mapping 'logtime:logtime,method:method,path:path,code:code,size:size,resptime:resptime,token:token,path_url:path_url,timestamp:created_at'
      </table>
    </store>
  </match>
</worker>



針對Input 加工,ex 解析 Path 拆成不同欄位,在傳入 Output


情境: 以下 access log 範例,需要針對 Query Parameter 拆解並存入新欄位,以利分析.
[27/Dec/2019:07:14:10 +0000] "GET /path/to/url?out_id=somevalue&uri=somevalue HTTP/1.1" 302 608 0.057 "" "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.93 Mobile Safari/537.36 Line/9.22.2/IAB" "" "" "" "" "upstream: 127.0.0.1:32081"


<worker 0>
  <source>
  
   ... 略

  </source>
   
  <filter nginx.web.access>
    @type parser
    key_name path
    reserve_data true   # import! 將原本的 Input 一併傳到 Ouptput 
    <parse>
        @type regexp
        expression /^(?<path_url>([^\?]+))+(.out_id=(?<token>[^&#]*).uri=(?<target>[^#]*)|)$/
    </parse>
  </filter>

  <match nginx.web.access>
     
     ... 略
  
  </match>
</worker>

留言

這個網誌中的熱門文章

FluentD 實作 Error Log

FluentD 實作 Error Log 本篇將介紹使用 DotNet 專案 log4net 套件,紀錄的 log 針對 Error Level 的訊息透過FluentD 提取出來 在紀錄中 增加 trace ID 設入 MongoDB , 及加入 Slack 通知 延伸閱讀 FluentD 參數說明 FluentD 實作 Nginx Access Log FluentD 實作 Nginx Access Log 補充 log4net <?xml version="1.0" encoding="utf-8" ?> <configuration> <log4net> <appender name="All" type="log4net.Appender.RollingFileAppender"> <file value="/var/log/my.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <datePattern value="yyyy-MM-dd" /> <maximumFileSize value="5MB" /> <maxSizeRollBackups value="10" /> <staticLogFileName value="true" /> <PreserveLogFileNameExtension value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [...

申請免費 SSL,自動續訂

參考 acme.sh 搭配 GoDaddy 自動續期 Let's Encrypt 免費萬用憑證 使用 acme.sh + Cloudflare 申請免費 Wildcard SSL (Let’s Encrypt) 節略如下 安裝 acme.sh # 安裝 acme.sh ,安裝後重新登入 curl https://get.acme.sh | sh # 自動更新 acme.sh --upgrade --auto-upgrade acme.sh 設定存取 Goddy vi ~/.acme.sh/account.conf # Goddy API GD_Key="" GD_Secret="" acme.sh 設定存取 Cloudflare # Cloudflare API Keys # Global API Key [View] export CF_Key="" export CF_Email="" 申請網域(Domain)的萬用憑證,成功後會顯示憑證存放的路徑 $> acme.sh --issue --dns dns_gd -d ${domain} -d *.${domain} 安裝憑證 # 建立 /etc/nginx/ssl/${domain} 路徑 $> acme.sh --install-cert -d ${domain} --key-file /etc/nginx/ssl/${domain}/key.pem --fullchain-file /etc/nginx/ssl/${domain}/cert.pem --reloadcmd "sudo nginx -s reload"

Grafana Dashboard 建立

建立自己的 Dashboard # 由於 intelligent sense 相當不錯,輸入關鍵字他會帶出 metric label # 另外可參考 https://prometheus.io/docs/prometheus/latest/querying/basics/ Prometheus Query # 或是直接拿其他已建立的Dashboard 可複製到新的 Dashboard ex: node_memory_MemTotal_bytes # 取伺服器記憶體容量資料 # 過濾條件在{}加入 ex: node_memory_MemTotal_bytes{instance="${server 1}:9100"} # 要取特定伺服器資料 # Setting 中設定 Variables ex: node_memory_MemTotal_bytes{instance=~"$node"} # 變數名稱 node 建立 Alert .Visualization 必須是Graph