Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Broadcast messages from syslog printed on the console

Tags:

c

linux

syslog

I have a written a small code to send messages to syslog using C Api's when there is failure to connect to postgres database.

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);
}

When there is a connection failure to postgres database the messages are printed on to the console even though the option LOG_CONS is not enabled in openlog.

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?

Could you please help me how to avoid the messages being printed on the console.

like image 899
Nikhil Avatar asked Jan 03 '17 13:01

Nikhil


1 Answers

After the hint provided by @alk, i did some more research and found how to avoid the messages being printed on the console.

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?

The above message have two parts:

  1. Broadcast message from systemd-journald => These messages will be printed on the console by journalctl when emergency messages are sent. To disable these messages we need to disable ForwardToWall ie., ForwardToWall=no in /etc/systemd/journald.conf

  2. Message from syslogd => These messages are printed by rsyslog because of the below configuration line in /etc/rsyslog.conf

    *.emerg                                                 :omusrmsg:*
    

This selector-action states "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:*"." Comment this line out.

After performing the above operations the messages were not printed on the console. As these operations were not allowed due to security threats, I am raising the events with Alert priority.

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

Thanks to @alk.

like image 136
Nikhil Avatar answered Nov 06 '22 12:11

Nikhil