日志文件监控转储 php-logstash
- 授权协议: Apache
- 开发语言: PHP
- 操作系统: 跨平台
- 软件首页: https://github.com/anythink-wx/php-logstash
- 软件文档: https://github.com/anythink-wx/php-logstash/blob/master/README.md
软件介绍
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中执行
调试方式
程序提供了一个指令用来模拟日志写入
全部指令
程序默认使用如下Nginx的log_format,设置步骤如下
1、将如下 log_format 规则放置在 nginx 的 http 配置内
生成的日志格式入如下,默认build的也是这种格式
默认的 parser 会把 request 的请求分解成resquesturi与args,然后提交给elasticsearch方便汇总查看,如果不需要这么详细的拆分请直接使用request字段即可。
php-logstash
说明
输入
输出
调试
依赖
使用方法说明
输入方式
索引方式
#/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 副本数量
];
日志格式
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{
"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"
}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
)
)
释放潜能:平台型组织的进化路线图
穆胜 / 人民邮电出版社 / 2017-12 / 59.80元
传统的组织模式中,企业逃不出“员工动不起来”和“创新乏力”的宿命。互联网改变商业逻辑的同时也改变了组织逻辑。平台型组织是匹配互联网商业逻辑的组织模式,它赋予了基层员工更多的责权利,能够在需求侧灵敏获取用户刚需、在供给侧灵活整合各类资源、用“分好钱”的机制激活个体去整合各类资源满足用户刚需,形 成供需之间的高效连接。 打造平台型组织有两大主题:一是通过设计精巧的激励机制让每个人都能感受到市场的压力,......一起来看看 《释放潜能:平台型组织的进化路线图》 这本书的介绍吧!
