php-logstash 日志文件监控转储开源项目

我要开发同款
匿名用户2016年01月01日
10阅读
开发技术PHP
所属分类程序开发、日志工具(Logging)
授权协议Apache

作品详情

php-logstashphp实现的轻量级日志文件监控转储脚本

说明通过这个轻巧的脚本可以很容易的将日志送到elasticsearch中,并且本地测试处理能力基本保持在接近1w/s的速度。

脚本主要实现两个功能,输入和输出。

输入phpagent.php--listen=case.log用来监听访问日志的变更

或者使用命令tail-Fcase.log|phpagent.php--listen来监听来自stdin的输入。

该功能会持续将监听到的变更记入Redis队列中同时格式化将要记录的Log。

输出phpagent.php--indexer用来建立索引,该脚本每秒约索引8千左右,也可开多个并行处理。

该功能会持续将Redis队列中的数据导入ElasticSearch数据库中。

调试phplogstash.php--build=1在本地生成的case.log中追加一条log。

依赖

PHP5.4.0+

redis扩展

curl扩展

使用方法说明输入方式phpagent.php--listen=从头读取文件并持续监听

tail-Fcase.log|phpagent.php--listen监听Stdin传入的数据

索引方式phpagent.php--indexer

可将以上命令放置在shell中执行

#/bin/bashnohup 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'           => 'https://127.0.0.1:9200'  # elastic search通信地址,支持数组,可配置多个随机访问                                                      # 支持密码 程序采用 http auth_basic 认证方式                                                      # 使用密码 https://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_for2、将如下置放在 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; +https://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; +https://www.google.com/bot.html)    [referer] => -    [status] => 200    [resquesturi] => /controller/action    [args] => Array        (            [arg1] => 1            [arg2] => 2.7.1        ))

查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论