rsyslog使用之坑


=Start=

缘由:

记录一下在使用rsyslog这个系统软件时碰到的各种问题,方便以后进行查询。

正文:

参考解答:

Linux的rsyslog配置
http://cn.linux.vbird.org/linux_basic/0570syslog.php#syslogd_conf

`
服务名称[.=!]信息等级       信息记录的文件名或装置或主机
#
mail.info           /var/log/maillog_info
# 上面这一行说明:mail 服务产生的大于等于 info 等级的信息,都记录到 /var/log/maillog_info 文件中的意思。
#
mail.*             -/var/log/maillog
# 上面这一行说明:mail 服务产生的信息,都记录到 /var/log/maillog_info 文件中的意思。在记录的文件 /var/log/maillog 前面还有个减号『 - 』是干嘛用的?由于邮件所产生的信息比较多,因此我们希望邮件产生的信息先储存在速度较快的内存中 (buffer) ,等到数据量够大了才一次性的将所有数据都填入磁碟内,这样将有助於登录文件的存取性能。只不过由於信息是缓存在内存内,因此若不正常关机导致登录资讯未回填到登录文件中,可能会造成部分数据的遗失。
#
local6.notice       @192.168.10.199:514
# 上面这一行说明:local6 服务产生的大于等于 notice 等级的信息,都以 UDP 协议转发到 192.168.10.199 机器上的 514 端口上。
#
local6.notice       @@192.168.10.199:514
# 上面这一行说明:local6 服务产生的大于等于 notice 等级的信息,都以 TCP 协议转发到 192.168.10.199 机器上的 514 端口上。
#
# rsyslog支持在转发至远程服务器的同时也在本地存一份,示例如下:
local6.notice       @@192.168.10.199:514
local6.notice       /var/log/local6_notice
& ~
#
# rsyslog根据服务名称和tag进行分类处理
if $syslogfacility-text == 'local1' and $syslogtag == 'Security:' then  -/var/log/security.log
& ~
`

&

# vim /etc/rsyslog.d/remote.conf
$ActionQueueType LinkedList
$ActionQueueFileName example_fwd
$ActionResumeRetryCount -1
$ActionQueueSaveOnShutdown on
local6.*        @@10.4.93.104:514

&

Ubuntu服务器慢慢停止响应
http://b.kl3in.com/2011/10/ubuntu-server-slowly-stops-responding/

syslog() -> /dev/log -> rsyslogd -> local file/remote server

syslog submits a message to the Syslog facility. It does this by writing to the Unix domain socket /dev/log.

&

/dev/log

https://stackoverflow.com/questions/40787610/how-to-read-dev-log
https://linux.die.net/man/8/syslogd
http://man7.org/linux/man-pages/man8/rsyslogd.8.html
https://www.gnu.org/software/libc/manual/html_node/Overview-of-Syslog.html
http://blog.csdn.net/robbie1314520/article/details/6077217
http://www.chinabaike.com/z/jd/2012/0409/1098299.html

import socket
import time
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.bind('/dev/log')  #需要先手动kill掉已经在运行的[r]syslogd进程,否则这里无法执行成功
while True:
    time.sleep(5)
    data, addr = sock.recvfrom(1024)
    print(data)

&

`
Kafka / Scribe / Flume
I hate to be that guy, but this software (with a patent, natch) is definitely not the first or best solution to the potential problem of the /dev/log buffer filling up.
If the standard syslogd is inadequate, how about using a different logger? Gerritt Pape's svlogd, an implementation of DJB's daemontools loggers, solves this problem very nicely, is very reliable, can log over UDP and write to disk if desired. Heck, it can even replace syslog and run more reliably.
Not to say that this software is not useful. It is a great solution if the client software insists on using syslog and cannot be modified or reconfigured and syslogd cannot be modified or replaced. However, these are quite specific and generally unusual cases. For more prosaic circumstances, there are simpler and better solutions.
`
参考链接:

http://www.linuxquestions.org/questions/red-hat-31/sshd-hangs-rsyslog-related-4175550947/

http://b.kl3in.com/2011/10/ubuntu-server-slowly-stops-responding/
https://stackoverflow.com/questions/40787610/how-to-read-dev-log

 

Unable to login to the system (eg. with ssh) after rsyslog crashes, it looks frozen
https://access.redhat.com/solutions/1250933

Rsyslog freezes the box when can’t send logs over TCP
https://lists.gt.net/rsyslog/users/7949

20.4. WORKING WITH QUEUES IN RSYSLOG #当需要使用rsyslog的TCP转发功能时,最好单独为其指定queue,避免影响整个系统
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/s1-working_with_queues_in_rsyslog.html

让 syslog() 不再阻塞
https://news.ycombinator.com/item?id=7219914
https://github.com/facebook/liblogfaf

=END=


《 “rsyslog使用之坑” 》 有 6 条评论

  1. Nginx 源代码笔记 – syslog
    http://ialloc.org/posts/2014/07/06/nginx-notes-log-syslog/
    `
    Nginx 社区版从 1.7.1 开始,支持将 “access log” 和 “error log” 通过网络发送给 syslog/rsyslog 等日志存储服务的功能。

    支持方:
    · 即然 syslog(3) 函数是阻塞操作,会影响建立在非阻塞事件驱动机制基础上的 Nginx 的性能,那么可以重新实现这个函数为非阻塞形式;
    · 使用非阻塞 UDP 发送日志数据,接收端在同一主机上时,可靠性问题不大;
    · 使用专用线程调用阻塞的 syslog(3),Nginx 主线程可以使用 fifo 或者文件和该线程通信;
    · 有些类 syslogd 实现的性能己经足够高了,比如 rsyslogd 可以达到 1M logs/s,并且它已经是很多 Linux 发行版的默认日志收集程序了;
    · Nginx 向类 syslogd 传输日志完全可以作为非默认的选项提供,接下来系统管理员可以评估风险并且做合适的决策;
    · 使用 UDP 实时传输日志时,即使类 syslogd 程序挂掉造成了日志丢失,Nginx 进程也不会被阻塞。
    · syslog(3) 的阻塞特征不是理由!如果 /dev/log 满了带来的问题远比能不能让 Nginx 进程正常运行更严重,这时候你可能连系统都无法登陆了。
    · 现在 Nginx 将日志写到文件的方式也是有问题的,这种非网络阻塞操作也不适合 Nginx。 日志最好还是交给独立的线程处理,同时,异步缓冲模式的日志记录方法是大流量网络程序的标准做法 (缓冲区满了的时候允许丢掉后续的日志数据,线程间的同步需要设计良好的锁或者使用无锁队列等等)。
    · 还可以使用 pipe,一般系统的 pipe 允许 4K (PIPE_BUF) 的缓冲数据,对一般 日志来说足够了;

    反对方 (Maxim Dounin):
    类 syslog 软件的主要问题就是它提供的接口是阻塞的,这时如果类 syslogd 进程不 工作了或者负载较高日志量较大的情况下无法及时处理时就会影响 Nginx 的吞吐。并且 它们的处理速度和普通文件比起来相对较慢;

    最终,Nginx 还是选择了使用非阻塞 UDP 的方式实现了 syslogd 的支持。但是注意,这个特性还未被合并进稳定版。同时,由于 UDP 本身的特性和对 Nginx 本身 逻辑简洁性的考虑 (从下面的代码可以看到,Nginx 并不会对日志的发送成功或者失败做检查),要做好日志可能丢失的心理准备。
    `

  2. Writing specific messages to a file and discarding them
    https://www.rsyslog.com/writing-specific-messages-to-a-file-and-discarding-them/

    Storing Messages from a Remote System into a specific File
    https://www.rsyslog.com/storing-messages-from-a-remote-system-into-a-specific-file/

    Configuring Rsyslog To Stop The Logging Of Certain Messages
    https://serverfault.com/questions/366023/configuring-rsyslog-to-stop-the-logging-of-certain-messages
    `
    在匹配之后,rsyslog需要一个语句来停止日志记录。所以,如果不希望重复记录的话,请在对应的if语句之后立即添加这一行:
    & stop
    // 或
    & ~
    `
    Rsyslog duplicate logs (rsyslog.conf and rsyslog.d/something.conf)
    https://serverfault.com/questions/586803/rsyslog-duplicate-logs-rsyslog-conf-and-rsyslog-d-something-conf

  3. Logtail从入门到精通(一):日志采集杂谈
    https://yq.aliyun.com/articles/583858
    `
    日志采集Agent有很多,例如Logstash、Fluentd、Beats系列(FileBeats、MetricBeats、Packetbeat、Winlogbeat、Auditbeat、Heartbeat)、Nxlog、Telegraf、Heka、Nifi、Logspout、Datadog agent、Sematext agent、Splunk addon系列、Sumologic collector。。。

    业界有那么多的Agent,每个Agent各种各样的功能和特性看起来让人眼花缭乱。但围绕日志采集这个最原始的需求展开,无非也就是功能、性能、稳定性、运维代价这4个方面:

    1、功能:作为选择Agent最基本需求,主要分为输入源、数据处理(除日志解析外,还包括过滤、压缩等)、数据输出。
    2、性能:不同类型Agent之间会有数十倍的性能差距。当日志产生速率较低且资源充足时,此项可以忽略;但大部分情况下采集Agent是作为整个集群的基础软件,在集群规模庞大的情况下,即使每台机器节省1%的CPU、10MB的内存也是很大一笔资金节省。
    3、稳定性:稳定的重要性无需多言,除保证自身运行的稳定外,Agent还需保证不能超出限定的资源使用Quota,以免对应用产生影响。
    4、运维代价:日志采集的运维主要包括:Agent部署、动态伸缩、配置管理、Agent升级、Agent异常监控。当只有数台主机时,Agent可以使用人肉的方式进行管理,但当集群规模扩大到数百及以上时,运维必须依赖有效的机制。
    `

  4. 用rsyslog转发SSH登录成功日志
    https://www.rsyslog.com/doc/v8-stable/configuration/filters.html
    https://serverfault.com/questions/512240/rsyslog-filtering-based-on-message-content
    https://www.rsyslog.com/discarding-unwanted-messages/
    https://www.rsyslog.com/doc/rsyslog%255Fconf%255Ffilter.html
    https://serverfault.com/questions/197321/rsyslog-filters-on-message-contents-and-facility
    `
    if $msg contains ‘: Accepted’ then /opt/logs/login.log #不行!!!
    # if $programname == ‘sshd’ then /opt/logs/login.log #可行!就是量比较多。
    # if ($msg contains ‘Accepted’) then /opt/logs/login.log #可行!
    # if $msg contains ‘Accepted’ then /opt/logs/login.log #可行!但还是建议加上括号!
    # if $programname == ‘sshd’ and ($msg contains ‘Accepted’) then /opt/logs/login.log #可行!
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注