php實現的輕量級日志文件監控腳本配合ElasticSearch

jopen 9年前發布 | 22K 次閱讀 PHP開發

php-logstash

php實現的輕量級日志文件監控轉儲腳本

說明

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

腳本主要實現兩個功能,輸入和輸出。

輸入

php agent.php --listen=case.log 用來監聽訪問日志的變更

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

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

輸出

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

該功能會持續將Redis隊列中的數據導入 ElasticSearch 數據庫中。

調試

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

依賴

  • PHP 5.4.0 +

  • redis 擴展

  • curl 擴展

使用方法說明

輸入方式

php agent.php --listen= 從頭讀取文件并持續監聽

tail -F case.log | php agent.php --listen 監聽 Stdin 傳入的數據

索引方式

php agent.php --indexer

可將以上命令放置在shell中執行

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

調試方式

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

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

全部指令

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

配置文件

全部配置文件如下,默認均有默認值
[
     'redis'             => 'tcp://127.0.0.1:6379',   # redis地址,支持認證不支持數組。認證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               # 輸入信息到達指定內存后同步
     'input_sync_second' => 5                         # 輸入信息等待超過指定秒數后同步,以上2個條件共同觸發
     'parser'            => [$this,'parser']          # 自定義輸入端日志的處理格式,默認與程序提供的logformat json一致

     'elastic'           => 'http://127.0.0.1:9200'  # elastic search通信地址,支持數組,可配置多個隨機訪問
                                                      # 支持密碼 程序采用 http auth_basic 認證方式
                                                      # 使用密碼 http://user:pssword@127.0.0.1:9200
     'prefix'            => 'phplogstash',            # es 默認索引前綴名字為 phplogstash-2015.12.12 
     'shards'            => '5',                      # es 分片數量
     'replicas'          => '2',                      # es 副本數量
];

日志格式

程序默認使用如下Nginx的log_format,設置步驟如下

1、將如下 log_format 規則放置在 nginx 的 http 配置內

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"}';

如果是內網機器需要使用該變量獲取真實IP     $http_x_forwarded_for

2、將如下置放在 server 的配置內。

access_log web_accesslog.json json

生成的日志格式入如下,默認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"
}

默認的 parser 會把 request 的請求分解成resquesturi與args,然后提交給elasticsearch方便匯總查看,如果不需要這么詳細的拆分請直接使用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
        )
)

官方網站:http://www.baiduhome.net/lib/view/home/1451625996745

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!