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

时间:2017-01-03 13:28:51

标签: c linux syslog

我写了一个小代码,当无法连接到postgres数据库时,使用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?

您能帮我解决一下如何避免在控制台上打印消息。

1 个答案:

答案 0 :(得分:3)

在@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

  2. 中禁用ForwardToWall,即 ForwardToWall = no
  3. 来自syslogd =>的消息这些消息由rsyslog打印,因为/etc/rsyslog.conf中的以下配置行

  4. .emerg:omusrmsg:

    此选择器操作状态"紧急消息通常发送给当前在线的所有用户,以通知他们系统发生了奇怪的事情。要指定此墙(1) - 特征使用":omusrmsg:*"。"评论此行。

    执行上述操作后,控制台上没有打印消息。由于安全威胁不允许这些操作,我以警告优先级提升事件。

    syslog ( LOG_ALERT, "%s", PQerrorMessage(psql) );
    

    感谢@alk。

相关问题