ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-11-11 23:51:13  阅读:1126  来源: 互联网

标签:syslog c-3 linux


我写了一个小代码,当无法连接到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?

您能否帮助我避免在控制台上打印消息.

解决方法:

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

上面的消息分为两部分:

>从systemd-journald广播消息=>发送紧急消息时,这些消息将通过journalctl打印在控制台上.要禁用这些消息,我们需要禁用ForwardToWall,即/etc/systemd/journald.conf中的ForwardToWall = no
>来自syslogd的消息=>由于/etc/rsyslog.conf中的以下配置行,这些消息由rsyslog打印

.emerg:omusrmsg:

该选择器操作指出:“紧急消息经常发送给当前在线的所有用户,以通知他们系统发生了奇怪的事情.要指定wall(1)功能,请使用“:omusrmsg:*”.”注释掉这一行.

执行上述操作后,消息未在控制台上打印.由于安全威胁,不允许执行这些操作,因此我以“警报”优先级提出事件.

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

感谢@alk.

标签:syslog,c-3,linux
来源: https://codeday.me/bug/20191111/2023043.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有