如何避免在控制台上打印syslog广播消息

10

当连接到PostgreSQL数据库失败时,我编写了一小段代码,使用C API向syslog发送消息。

int main ( int argc, char **argv )
{
        PGconn *psql;
        PGresult *res;
        int flag = 0;

        openlog ( "postgres", LOG_NDELAY, LOG_SYSLOG );

        psql = PQconnectdb("hostaddr = '127.0.0.0' port = '5432' dbname = 'RtpDb' user = 'rtp_user_99' password = 'rtp_user' connect_timeout = '10'");
        if ( PQstatus(psql) != CONNECTION_OK )
        {
           //Send an event to syslog for DB Connection Failure
           syslog (LOG_EMERG, "%s", PQerrorMessage(psql) )
        }
       closelog ();
       PQclear(res);
       PQfinish(psql);
}

当连接到postgres数据库失败时,即使在openlog中未启用LOG_CONS选项,消息仍会打印到控制台上。

Broadcast message from systemd-journald@blr09 (Tue 2017-01-03 05:24:46 EST):
postgres[40933]: could not connect to server: Network is unreachable
        Is the server running on host "127.0.0.0" and accepting
        TCP/IP connections on port 5432?
Message from syslogd@blr09 at Jan  3 05:24:46 ...
 postgres:could not connect to server: Network is unreachable#012#011Is the server running on host "127.0.0.0" and accepting#012#011TCP/IP connections on port 5432?

请问如何避免信息在控制台上打印?


3
相关链接:http://serverfault.com/a/392333/143982 - alk
那么,敢展示一下你的syslog守护进程配置吗? - alk
1个回答

11

在 @alk 提供的提示下,我进行了更多的研究,并找到了如何避免消息被打印到控制台上。

Broadcast message from systemd-journald@blr09 (Tue 2017-01-03 05:24:46 EST):
postgres[40933]: could not connect to server: Network is unreachable
        Is the server running on host "127.0.0.0" and accepting
        TCP/IP connections on port 5432?
Message from syslogd@blr09 at Jan  3 05:24:46 ...
 postgres:could not connect to server: Network is unreachable#012#011Is the server running on host "127.0.0.0" and accepting#012#011TCP/IP connections on port 5432?

以上消息有两个部分:

  1. 来自systemd-journald的广播消息 => 当发送紧急消息时,这些消息将通过journalctl在控制台上打印。 要禁用这些消息,我们需要在/etc/systemd/journald.conf中禁用ForwardToWall,即ForwardToWall=no

  2. 来自syslogd的消息 => 由于/etc/rsyslog.conf中的以下配置行,这些消息由rsyslog打印:

  3. *.emerg                                                 :omusrmsg:*
    
    该选择器指令表明:“紧急消息通常会发送给当前所有在线用户,以通知他们系统出现了异常情况。要指定这个 wall(1) 功能,请使用“:omusrmsg:*” 。注释掉这行。”执行以上操作后,消息没有在控制台上打印出来。由于存在安全威胁,这些操作是不允许的,因此我正在提高事件的警报优先级。
    syslog ( LOG_ALERT, "%s", PQerrorMessage(psql) );
    

    感谢@alk。


不客气。你很可能会接受自己对自己问题的(好)答案。 - alk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接