常规的日志收集方案中Client端都需要额外安装一个Agent来收集日志,例如logstash、filebeat等,额外的程序也就意味着环境的复杂,资源的占用,有没有一种方式是不需要额外安装程序就能实现日志收集呢?Rsyslog就是你要找的答案!
Rsyslog是高速的日志收集处理服务,它具有高性能、安全可靠和模块化设计的特点,能够接收来自各种来源的日志输入(例如:file,tcp,udp,uxsock等),并通过处理后将结果输出的不同的目的地(例如:mysql,mongodb,elasticsearch,kafka等),每秒处理日志量能够超过百万条。
Rsyslog作为syslog的增强升级版本已经在各linux发行版默认安装了,无需额外安装。
ELK通过Rsyslog收集日志流程图如下:
rsyslogd -v
命令查看rsyslog版本,如果版本较低则需要升级1.添加rsyslog源的key
# apt-key adv --recv-keys --keyserver keys.gnupg.net AEF0CF8E
2.添加rsyslog源地址
echo "deb http://debian.adiscon.com/v8-stable wheezy/" >> /etc/apt/sources.list
echo "deb-src http://debian.adiscon.com/v8-stable wheezy/" >> /etc/apt/sources.list
3.升级rsyslog服务
# apt-get update && apt-get -y install rsyslog
1.安装编译工具,下边autoreconf需要用到,不然无法生成configure文件
# apt-get -y install pkg-config autoconf automake libtool unzip
2.omkafka需要安装一堆的依赖包
# apt-get -y install libdbi-dev libmysqlclient-dev postgresql-client libpq-dev libnet-dev librdkafka-dev libgrok-dev libgrok1 libgrok-dev libpcre3-dev libtokyocabinet-dev libglib2.0-dev libmongo-client-dev libhiredis-dev
# apt-get -y install libestr-dev libfastjson-dev uuid-dev liblogging-stdlog-dev libgcrypt-dev
# apt-get -y install flex bison librdkafka1 librdkafka-dev librdkafka1-dbg
3.编译安装omkafka模块
# mkdir tmp && cd tmp
# git init
# git pull [email protected]:VertiPub/omkafka.git
# autoreconf -fvi
# ./configure --sbindir=/usr/sbin --libdir=/usr/lib --enable-omkafka && make && make install && cd ..
log_format jsonlog '{'
'"host": "$host",'
'"server_addr": "$server_addr",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"remote_addr":"$remote_addr",'
'"time_local":"$time_local",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"status":$status,'
'"body_bytes_sent":$body_bytes_sent,'
'"http_referer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"upstream_addr":"$upstream_addr",'
'"upstream_status":"$upstream_status",'
'"upstream_response_time":"$upstream_response_time",'
'"request_time":$request_time'
'}';
access_log syslog:server=rsyslog.domain.com,facility=local7,tag=nginx_access_log,severity=info jsonlog;
1.Nginx在v1.10之后的版本才支持syslog的方式处理日志,请确保你的Nginx版本高于1.10
2.为了降低logstash的处理压力,同时也为了降低整个配置的复杂度,我们nginx的日志直接采用json格式
3.抛弃文本文件记录nginx日志,改用syslog直接将日志传输到远端的rsyslog服务器,以便我们后续的处理;这样做的另一个非常重要的好处是我们再也无需考虑nginx日志的分割和定期删除问题(一般我们为了方便管理通常会采用logrotate服务来对日志进行按天拆分和定期删除,以免磁盘被占满)
4.access_log直接输出到syslog服务,各参数解释如下:
nginx_access_log
,如果有多个服务同时都写日志给rsyslog,且配置了不通的tag,在rsyslog服务端就可以根据这个tag找出哪些是nginx的日志# cat /etc/rsyslog.d/rsyslog_nginx_kafka_cluster.conf
module(load="imudp")
input(type="imudp" port="514")
# nginx access log ==> rsyslog server(local) ==> kafka
module(load="omkafka")
template(name="nginxLog" type="string" string="%msg%")
if $inputname == "imudp" then {
if ($programname == "nginx_access_log") then
action(type="omkafka"
template="nginxLog"
broker=["10.82.9.202:9092","10.82.9.203:9092","10.82.9.204:9092"]
topic="rsyslog_nginx"
partitions.auto="on"
confParam=[
"socket.keepalive.enable=true"
]
)
}
:rawmsg, contains, "nginx_access_log" ~
1.在rsyslog.d目录下添加一个专门处理nginx日志的配置文件
2.rsyslog配置文件重要配置解释如下:
imudp
且programname为nginx_access_log
(就是我们上边nginx配置里边的tag)之后的处理方式,这里的配置为匹配到的日志通过omkafka模块写入kafka集群,还有一些关于omkafka更详细的配置参考上边给出的omkafka模块官方文档nginx_access_log
的日志,没有这一行的话rsyslog服务默认会把所有日志都记录到message里边一份,我们已经把日志输出到kafka了,本地就没必要再记录了3.omkafka模块检查kafka里边topic是否存在,如果不存在则创建,无需手动创建kafka的topic
input {
kafka {
bootstrap_servers => "10.82.9.202:9092,10.82.9.203:9092,10.82.9.204:9092"
topics => ["rsyslog_nginx"]
}
}
filter {
mutate {
gsub => ["message", "\\x", "\\\x"]
}
json {
source => "message"
}
date {
match => ["time_local","dd/MMM/yyyy:HH:mm:ss Z"]
target => "@timestamp"
}
}
output {
elasticsearch {
hosts => ["10.82.9.205", "10.82.9.206", "10.82.9.207"]
index => "rsyslog-nginx-%{+YYYY.MM.dd}"
}
}
重要配置参数解释如下:
\\x
,不然json格式会报错配置完成后分别重启rsyslog服务和nginx服务,访问nginx产生日志
1.查看kafka是否有正常生成topic
# bin/kafka-topics.sh --list --zookeeper 127.0.0.1:2181
__consumer_offsets
rsyslog_nginx
2.查看topic是否能正常接收日志
# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic rsyslog_nginx
{"host": "domain.com","server_addr": "172.17.0.2","http_x_forwarded_for":"58.52.198.68","remote_addr":"10.120.89.84","time_local":"28/Aug/2018:14:26:00 +0800","request_method":"GET","request_uri":"/","status":200,"body_bytes_sent":1461,"http_referer":"-","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36","upstream_addr":"-","upstream_status":"-","upstream_response_time":"-","request_time":0.000}
3.kibana添加index,查看Elasticsearch中是否有数据,如果前两步都正常,kibana搜不到index或index没有数据,多半是index名字写错了之类的基础问题,仔细检查
rsyslog-nginx-*
的Index,并选择timestamp,创建Index Pattern