您的位置:首页 > 其它

基于淘宝Tengine和Scribe的WEB日志收集方案

2011-12-23 19:21 330 查看
公司在迁移hadoop到新的集群,涉及到原数据流的迁移,以前是用syslog-ng去push日志,但是配置起来比较麻烦。正好淘宝开发了一个新的tengine,拿过来尝尝鲜,正好利用其pipe功能,可以将日志发送到scribe进行汇总收集,然后就可以舍弃syslog-ng了。反正syslog也是要被Linux抛弃的。
按说应该是直接把Scribe的API直接嵌套在程序中,但由于scribe是刚刚开始推广应用,而以前的日志分析都是基于weblog,大规模改造程序,成本太大,需要逐步推进,为了快速切换,只能这样用了。
安装scribe以前已经写过两个文章了,不再赘述。这次只讲讲是如何将nginx的日志通过scribe收集起来的。
说明:配置tengine的服务器IP是192.168.1.14,远端的收集服务器IP是192.168.1.33。
scribe在192.168.1.14上的配置是发送一份日志到远端,再保存一份日志在本机。scribe在远端服务器的配置就是默认就可以了。
Tengine在http://tengine.taobao.org/这里下载。然后安装配置
./configure --prefix=/opt/modules/tengine --with-file-aio --with-syslog --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre && make && make install
加上--with-syslog主要是为了将syslog作为备用方案,万一scribe全挂了,可以用syslog接上。
如果是用我以前发布的linux rpm在centos下安装scribe,默认是不好用的,所以需要在centos下改一下。
chkconfig --del scribeecho "scribed -c /etc/scribe.conf &" >> /etc/rc.local然后到/opt/modules/tengine下编辑conf/nginx.conf
server {
listen 80;
server_name 192.168.1.14;

access_log "pipe:/opt/modules/tengine/sbin/scribe_ngx -h 192.168.1.14:1463 collect-vv-175" main;

}
红色部分,是淘宝新开发出的pipe日志的功能。用这种办法,在nginx进程里调用一个收集用户输入的脚本
然后是scribe_ngx,其实就是scribe自带的scribe_cat改造了一下,从获取stdin改成了获取用户输入。但是用户输入是放到一个死循环里的,只要nginx不退出,scribe_ngx进程就不会退出。
#!/usr/bin/python
import sys
from scribe import scribe
from thrift.transport import TTransport, TSocket
from thrift.protocol import TBinaryProtocol

if len(sys.argv) == 2:
category = sys.argv[1]
host = '127.0.0.1'
port = 1463
elif len(sys.argv) == 4 and sys.argv[1] == '-h':
category = sys.argv[3]
host_port = sys.argv[2].split(':')
host = host_port[0]
if len(host_port) > 1:
port = int(host_port[1])
else:
port = 1463
else:
sys.exit('usage (message is stdin): scribe_cat [-h host[:port]] category')

socket = TSocket.TSocket(host=host, port=port)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
transport.open()

#log_entry = scribe.LogEntry(category=category, message=sys.stdin.read())
message = '%s'%raw_input()
while True:
log_entry = scribe.LogEntry(category=category, message=message)

client = scribe.Client(iprot=protocol, oprot=protocol)

result = client.Log(messages=[log_entry])
message = '%s'%raw_input()
然后将程序放到/opt/modules/tengine/sbin,然后启动nginx,你将会在进程里看到一个python的进程。
nobody 23093 0.0 0.0 111044 4460 ? S 18:27 0:00 /usr/bin/python /opt/modules/tengine/sbin/scribe_ngx -h 192.168.1.14:1463 collect-vv-175
然后我们来配置192.168.1.14:/etc/scribe.conf
port=1463
max_msg_per_second=2000000
check_interval=3

# DEFAULT - forward all messages to Scribe on port 1463
<store>
category=default
type=multi
#注意这里,用multi存储,而不是默认中的buffer

target_write_size=20480
max_write_interval=1
buffer_send_rate=1
retry_interval=30
retry_interval_range=10

<store0>
type=network
remote_host=192.168.1.33
#远端收集服务器,主机名或IP
remote_port=1463
</store0>

<store1>
type=file
fs_type=std
file_path=/data1/scribe
#再存一份本地文件,更加保险。
base_filename=thisisoverwritten
max_size=100000000000
#日志文件最大大小,超过将自动切分而无视时间设置
add_newlines=1
#每行增加一个回车,比较重要
rotate_period=hourly
#每小时切分日志
rotate_minute=0
rotate_hour=0
</store1>
</store>

然后用scribed -c /etc/scribe.conf &启动scribe,当然,收集服务器192.168.1.33也需要启动scribe。收集服的scribe只要按照默认启动就可以了,我在收集服配置的日志路径是/data/scribe
然后通过http访问192.168.1.14,你将在192.168.1.14:/data1/scribe和192.168.1.33:/data/scribe下都看到日志文件被创建起来,并随着访问而不断增大。
从整个描述过程中,可以看出来,在webserver上,我实际上是吧tengine和scribe放到了同一台服务器上。其实这样会降低服务器的web负载效率,毕竟还有个scribe在占用进程和文件描述符。不过,这样放置的目的主要是为了在本机也要保存日志,而tengine或者nginx默认不能同时配置两个access_log,如果不涉及同步保存,可以只发送日志到远端服务器,把pipe里面的IP改成远端的IP,然后把scribe设置成buffer存储,设置primary和secondary就可以了。
最后,感谢淘宝对开源做出的贡献。如果没有pipe功能,恐怕我就需要去修改nginx的源代码了。
顺便给自己做个广告。

EasyHadoop1.0发布,主要是为了给没有经验的新手提供快速安装的方法。基于脚本编写,其实只是将需要的包整理了一下方便大家下载安装。

访问easyhadoop.com下载。
目前该脚本仅提供单机版测试安装,配置文件稍作修改可改为真分布式。
2012-06-20修改python脚本

本文出自 “实践检验真理” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: