简体   繁体   English

syslog:特定于进程的优先级

[英]syslog: process specific priority

I have two user processes A and B. Both use syslog using facility LOG_USER . 我有两个用户进程A和B。都使用设施LOG_USER使用syslog

I want to have different threshold levels for them: 我想为他们设置不同的阈值级别:

  • For A, only messages of priority ERR-and-above must be logged 对于A,仅必须记录优先级为ERR或更高的消息。
  • For B, only messages of priority CRIT-and-above must be logged 对于B,仅必须记录优先级CRIT和更高的消息

I found that if I setup /etc/syslog.conf as 我发现如果我将/etc/syslog.conf设置为

user.err    /var/log/messages

then messages of ERR-and-above are logged, but, from both A and B. 然后记录错误消息及以上,但来自A和B。

How can I have different minimum threshold levels for different processes? 如何为不同的流程设置不同的最低阈值水平?

Note: I am exploring if there is a config file based solution. 注意:我正在研究是否有基于配置文件的解决方案。 Otherwise, there is another approach that works. 否则,还有另一种方法可行。 In each process, we can use setlogmask() to install process specific priority mask. 在每个进程中,我们可以使用setlogmask()安装进程特定的优先级掩码。

EDIT (Nov 18): I want to use syslog and some portable solution. 编辑(11月18日):我想使用syslog和一些便携式解决方案。

A config file based solution is available. 提供了基于配置文件的解决方案。 I think CentOS by default ships with rsyslog and even if it does not, you can always install rsyslog with yum. 我认为CentOS默认带有rsyslog,即使没有,您也可以始终在yum上安装rsyslog。 This solution works only with rsyslog and nothing else. 该解决方案仅适用于rsyslog,而没有其他功能。

The is a catch, though. 不过,这是一个陷阱。 You can not separate log messages with rsyslog (or pretty much any syslog daemon implementation) between processes with same name ie. 您不能在名称相同的进程之间使用rsyslog(或几乎所有的syslog守护程序实现)分隔日志消息。 the same executable path. 相同的可执行路径。 However, rsyslog does allow you to filter messages based on program name. 但是,rsyslog确实允许您根据程序名称过滤消息。 Here lies a possible solution: most programs call openlog(3) using argv[0], ie. 这是一个可能的解决方案:大多数程序使用argv [0]调用openlog(3),即。 the executable name, as the first argument. 可执行文件名称,作为第一个参数。 Now since you don't reveal the actual program you're running, there is no way to determine this for you, but you can always read the sources of your own program, I guess. 现在,由于您没有透露正在运行的实际程序,因此无法为您确定此程序,但我想您始终可以阅读自己程序的源代码。

In most cases the executable path is the program name, though some daemons do fiddle with argv[0] (notable examples are postfix and sendmail). 在大多数情况下,可执行路径是程序名称,尽管某些守护程序确实用argv [0]摆弄(值得注意的例子是postfix和sendmail)。 Rsyslog on the other hand provides a filtering mechanism which allows one to filter messages based on the name of the sending program (you can now probably see how this is all connected to how openlog(3) is called). 另一方面,Rsyslog提供了一种过滤机制,该机制允许用户根据发送程序的名称来过滤消息(您现在可能会看到它们如何与openlog(3)的调用方式联系在一起)。 So, instead of trying to filter directly processes, we can do filtering on program names. 因此,我们可以尝试对程序名称进行过滤,而不是尝试直接过滤进程。 And that we can affect by creating symbolic links. 而且我们可以通过创建符号链接来影响。

So, this solution only works given following conditions: a) the process you're running does not fiddle with argv[0] after beginning execution; 因此,此解决方案仅在以下条件下有效:a)您正在运行的进程在开始执行后不会摆弄argv [0]; b) you can create symlinks to the binary, thus creating two different names for the same program; b)您可以创建到二进制文件的符号链接,从而为同一程序创建两个不同的名称; c) your program is calling openlog(3) using argv[0] as the first parameter to the call. c)您的程序正在使用argv [0]作为调用的第一个参数来调用openlog(3)。

Given those two conditions, you can simply filter messages on /etc/rsyslog.conf like this (example directly from rsyslog documentation ): 在这两个条件下,您可以像这样直接在/etc/rsyslog.conf上过滤消息(示例直接来自rsyslog文档 ):

if $programname == 'prog1' then {
   action(type="omfile" file="/var/log/prog1.log")
}
if $programname == 'prog2' then {
   action(type="omfile" file="/var/log/prog2.log")
}

Eg if your program is called /usr/bin/foobar and you've created symbolic links /usr/bin/prog1 and /usr/bin/prog2 both pointing at /usr/bin/foobar , the above configuration file example will then direct messages from processes started as "prog1" and "prog2" to different log files respectively. 例如,如果您的程序名为/usr/bin/foobar并且创建了符号链接/usr/bin/prog1/usr/bin/prog2都指向/usr/bin/foobar ,则上面的配置文件示例将直接来自进程的消息分别以“ prog1”和“ prog2”开头到不同的日志文件。 This example will not fiddle with anything else, so all those messages are still going to general log files, unless you filter them out explicitly. 此示例将不做任何其他工作,因此所有这些消息仍将进入常规日志文件,除非您明确地将其过滤掉。

This tutorial http://www.freebsd.org/cgi/man.cgi?query=syslog.conf&sektion=5 helped me. 本教程http://www.freebsd.org/cgi/man.cgi?query=syslog.conf&sektion=5帮助了我。 The following seem to work: 以下似乎有效:

# process A: log only error and above
!A
*.err                /var/log/messages

# process B: log only critical and above
!B
*.critical           /var/log/messages

# all processes other than A and B: log only info and above
!-A,B
*.info               /var/log/messages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM