<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>

          php-logstash日志文件監(jiān)控轉(zhuǎn)儲

          聯(lián)合創(chuàng)作 · 2023-09-30 00:17

          php-logstash

          php實(shí)現(xiàn)的輕量級日志文件監(jiān)控轉(zhuǎn)儲腳本

          說明

          通過這個輕巧的腳本可以很容易的將日志送到 elasticsearch 中,并且本地測試處理能力基本保持在接近1w/s的速度。

          腳本主要實(shí)現(xiàn)兩個功能,輸入和輸出。

          輸入

          php agent.php --listen=case.log 用來監(jiān)聽訪問日志的變更

          或者使用命令 tail -F case.log | php agent.php --listen 來監(jiān)聽來自 stdin 的輸入。

          該功能會持續(xù)將監(jiān)聽到的變更記入Redis隊列中同時格式化將要記錄的Log。

          輸出

          php agent.php --indexer 用來建立索引,該腳本每秒約索引8千左右,也可開多個并行處理。

          該功能會持續(xù)將Redis隊列中的數(shù)據(jù)導(dǎo)入 ElasticSearch 數(shù)據(jù)庫中。

          調(diào)試

          php logstash.php --build=1 在本地生成的 case.log 中追加一條log。

          依賴

          • PHP 5.4.0 +

          • redis 擴(kuò)展

          • curl 擴(kuò)展

          使用方法說明

          輸入方式

          php agent.php --listen= 從頭讀取文件并持續(xù)監(jiān)聽

          tail -F case.log | php agent.php --listen 監(jiān)聽 Stdin 傳入的數(shù)據(jù)

          索引方式

          php agent.php --indexer

          可將以上命令放置在shell中執(zhí)行

          #/bin/bash
          nohup tail -F access.log | php agent.php --listen &
          nohup php agent.php --listen=case.log & 
          nohup php agent.php --indexer &

          調(diào)試方式

          程序提供了一個指令用來模擬日志寫入

          php logstash.php --build=<log_number> #生成的log條目數(shù),默認(rèn)20萬條
          文件保存為case.log并且在同級目錄下,可用命令
          tail -F case.log | php agent.php --listen 或
          php agent.php --listen=case.log
          測日志監(jiān)聽狀態(tài),并從redis中查看結(jié)果,或重新定義parser方法在內(nèi)部中斷調(diào)試日志解析過程

          全部指令

          agent.php --listen=<file_path> #將腳本設(shè)置為輸入模式,用來監(jiān)聽日志文件輸入
          agent.php --listen  #不指定文件將監(jiān)聽來自 stdin 的輸入
          agent.php --indexer #將腳本設(shè)置為索引模式,用來將隊列的數(shù)據(jù)發(fā)送到 ElasticSearch 服務(wù)器
          agent.php --status #查看隊列情況和處理速度

          配置文件

          全部配置文件如下,默認(rèn)均有默認(rèn)值
          [
               'redis'             => 'tcp://127.0.0.1:6379',   # redis地址,支持認(rèn)證不支持?jǐn)?shù)組。認(rèn)證tcp://auth:密碼@127.0.0.1:6379
               'type'              => 'log'                     # redis 隊列key,及es的index type
               'agent_log'         => __DIR__ .'/agent.log',    # 日志保存地址
               'input_sync_memory' => 5*1024*1024               # 輸入信息到達(dá)指定內(nèi)存后同步
               'input_sync_second' => 5                         # 輸入信息等待超過指定秒數(shù)后同步,以上2個條件共同觸發(fā)
               'parser'            => [$this,'parser']          # 自定義輸入端日志的處理格式,默認(rèn)與程序提供的logformat json一致
          
               'elastic'           => 'http://127.0.0.1:9200'  # elastic search通信地址,支持?jǐn)?shù)組,可配置多個隨機(jī)訪問
                                                                # 支持密碼 程序采用 http auth_basic 認(rèn)證方式
                                                                # 使用密碼 http://user:[email protected]:9200
               'prefix'            => 'phplogstash',            # es 默認(rèn)索引前綴名字為 phplogstash-2015.12.12 
               'shards'            => '5',                      # es 分片數(shù)量
               'replicas'          => '2',                      # es 副本數(shù)量
          ];

          日志格式

          程序默認(rèn)使用如下Nginx的log_format,設(shè)置步驟如下

          1、將如下 log_format 規(guī)則放置在 nginx 的 http 配置內(nèi)

          log_format json '{"timestamp":"$time_iso8601",'
                         '"host":"$server_addr",'
                         '"server":"$server_name",'
                         '"client":"$http_x_forwarded_for",'
                         '"size":$body_bytes_sent,'
                         '"responsetime":$upstream_response_time,'
                         '"domain":"$host",'
                         '"method":"$request_method",'
                         '"url":"$uri",'
                         '"requesturi":"$request_uri",'
                         '"via":"$server_protocol",'
                         '"request":"$request",'
                         '"uagent":"$http_user_agent",'
                         '"referer":"$http_referer",'
                         '"status":"$status"}';
          
          如果是內(nèi)網(wǎng)機(jī)器需要使用該變量獲取真實(shí)IP     $http_x_forwarded_for
          
          2、將如下置放在 server 的配置內(nèi)。
          
          access_log web_accesslog.json json

          生成的日志格式入如下,默認(rèn)build的也是這種格式

          {
            "timestamp": "2015-12-18T14:24:26+08:00",
            "host": "10.10.23.139",
            "message": "0",
            "server": "localhost",
            "client": "127.0.0.1",
            "size": 197,
            "responsetime": 0.010,
            "domain": "www.localhost.com",
            "method": "GET",
            "url": "/index.php",
            "requesturi": "/controller/action?arg1=1&arg2=2",
            "via": "HTTP/1.1",
            "request": "GET /controller/action?arg1=1&arg2=2 HTTP/1.1",
            "uagent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
            "referer": "-",
            "status": "200"
          }

          默認(rèn)的 parser 會把 request 的請求分解成resquesturi與args,然后提交給elasticsearch方便匯總查看,如果不需要這么詳細(xì)的拆分請直接使用request字段即可。

          Array
          (
              [timestamp] => 2015-12-18T14:24:26+08:00
              [host] => 10.10.23.139
              [message] => 0
              [server] => localhost
              [client] => 127.0.0.1
              [size] => 197
              [responsetime] => 0.01
              [domain] => www.localhost.com
              [method] => GET
              [url] => /index.php
              [requesturi] => /controller/action?arg1=1&arg2=2
              [via] => HTTP/1.1
              [uagent] => Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
              [referer] => -
              [status] => 200
              [resquesturi] => /controller/action
              [args] => Array
                  (
                      [arg1] => 1
                      [arg2] => 2.7.1
                  )
          )

          瀏覽 24
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          <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>
                  波多野结衣大香蕉 | 无码操穴 | 骚屄激情在线 | 五月天综合| 高清无码一线逼美女系列 |