如何避免在控制台上打印来自 syslog 的广播消息
How to avoid Broadcast messages from syslog printed on the console
我写了一个小代码,当无法连接到 postgres 数据库时,使用 C Api 将消息发送到系统日志。
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 即 ForwardToWall=no in /etc/systemd/journald.conf
来自 syslogd 的消息 => 由于 /etc/rsyslog.conf
中的以下配置行,这些消息由 rsyslog 打印
*.emerg :omusrmsg:*
此选择器操作声明 "Emergency messages often go to all users currently online to notify them that something strange is happening with the system. To specify this wall(1)-feature use an ":omusrmsg:*"."注释掉这一行。
执行上述操作后,控制台上未打印消息。由于安全威胁不允许这些操作,我将以 Alert 优先级引发事件。
syslog ( LOG_ALERT, "%s", PQerrorMessage(psql) );
感谢@alk。
我写了一个小代码,当无法连接到 postgres 数据库时,使用 C Api 将消息发送到系统日志。
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 即 ForwardToWall=no in /etc/systemd/journald.conf
来自 syslogd 的消息 => 由于 /etc/rsyslog.conf
中的以下配置行,这些消息由 rsyslog 打印*.emerg :omusrmsg:*
此选择器操作声明 "Emergency messages often go to all users currently online to notify them that something strange is happening with the system. To specify this wall(1)-feature use an ":omusrmsg:*"."注释掉这一行。
执行上述操作后,控制台上未打印消息。由于安全威胁不允许这些操作,我将以 Alert 优先级引发事件。
syslog ( LOG_ALERT, "%s", PQerrorMessage(psql) );
感谢@alk。