23

rsyslog,并不落伍的汇总日志利器

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzAwOTU4NzM5Ng%3D%3D&%3Bmid=2455771562&%3Bidx=1&%3Bsn=ffc14faf347f9f6cf33843501a152fe5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

服务器应用日志越来越大,而且服务器越来越多,排查问题很不方便,所以:

  • 将服务器应用日志汇总到一台机器上,排查问题方便
  • 轮转应用日志(轮转系统软件日志更简单,比如Nginx日志),压缩后容量会小很多

其实使用rsyslog的imfile模块就能解决问题,而且是系统内核就支持的,并不low,自己学到的也只是九牛一毛。

1:由于imfile模块会用到通配符,而rsyslog 8.25以上才支持,所以需要升级

cd /etc/yum.repos.d/
wget http://rpms.adiscon.com/v8-stable/rsyslog.repo
yum install rsyslog
rsyslogd -v
service rsyslog restart

2:服务器端配置

由于所有客户端需要通过UDP传递数据到服务器端,所以服务器端配置:

vim /etc/rsyslog.conf

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

3:客户端配置imfle模块

配置 /etc/rsyslog.d/phplog.conf

input(type="imfile"
File="/var/logs/api/time*"
Tag="phplog-api-debug"
Severity="debug"
Facility="local6")

input(type="imfile"
File="/var/logs/mobile/mobile.2*.log"
Tag="phplog-mobile-request"
Severity="notice"
Facility="local6")

local6.* @remoteip:514

为避免local6.*进入/var/log/message文件,配置:

vim /etc/rsyslog.conf

cron.none;local6.none   /var/log/messages

其中File表示源文件,Severity表示信息等级,Facility表示自定义信息,tag类似日志名称(会在服务器端日志信息中显示),然后我们要将local6的所有日志发送到远端rsyslog机器上。

4:服务器端配置

将客户端传递过来的日志保存到一个目录下,配置:

vim /etc/rsyslog.conf

local6.=info     /var/logs/phplog/apirequest.log
local6.=debug    /var/logs/phplog/apidebug.log

注意local6.=info 和 local6.info的区别,.info会包含其他级别的信息。

5:在服务器端轮转日志

/var/logs/phplog/*.log  {
daily
rotate 15
missingok
dateext
compress
notifempty
sharedscripts
create 0640 nobody root
postrotate
   systemctl kill -s HUP rsyslog.service
endscript
}

注意要重启rsyslog服务,同时改变当前文件的权限,否则是root

测试是否生效,运行:

logrotate -vf /etc/logrotate.conf

6:几个问题

(1)如果rsyslog既当服务器端,又当客户端,在测试的时候发现服务器端日志增长特别快,cpu负载很高,实际上客户端上,需要去除local6.* @remoteip:514配置,否则会死循环。

(2)配置查看是否生效,也可以查看/var/log/messages,比如:

too long (26744) with configured size 8096, begin of message is

(3)有的时候配置不生效,且服务器端日志增加非常快,有点死循环的意思。

参考:

  • https://www.rsyslog.com/doc/v8-stable/configuration/modules/imfile.html
  • https://www.rsyslog.com/rsyslog-configuration-builder

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK