Esempio n. 1
0
def detect_with_proc_net(pid):
    inodes = fetch_socket_inodes_for_process(pid)
    parser = ProcNetParser()
    result = parser.match_socket_inodes(inodes)
    if not result or len(result) == 0:
        logger.error('could not detect connection string from /proc/net for postgres process {0}'.format(pid))
        return None
    return result
Esempio n. 2
0
def detect_with_proc_net(pid):
    result = None
    inodes = fetch_socket_inodes_for_process(pid)
    parser = ProcNetParser()
    result = parser.match_socket_inodes(inodes)
    if not result or len(result) == 0:
        logger.error('could not detect connection string from /proc/net for postgres process {0}'.format(pid))
        return None
    return result
Esempio n. 3
0
 def test__get_connection_by_type_should_return_conn_params_when_unix_type_ok(
         self, mocked_net_connections):
     parser = ProcNetParser(1048)
     unix_conn = sconn(fd=6,
                       family=1,
                       type=1,
                       laddr='/var/run/postgres/.s.PGSQL.5432',
                       raddr=None,
                       status='NONE',
                       pid=1048)
     conn_params = parser._get_connection_by_type('unix', unix_conn)
     self.assertEqual(('/var/run/postgres', '5432'), conn_params)
Esempio n. 4
0
    def test_get_connections_from_sockets_should_return_connections_by_type_when_ok(
            self, mocked_net_connections):
        unix_conns = [
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr='/var/run/postgres/.s.PGSQL.5432',
                  raddr=None,
                  status='NONE',
                  pid=1048),
        ]

        tcp_conns = [
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr=('127.0.0.1', 5432),
                  raddr=None,
                  status='NONE',
                  pid=1048),
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr=('127.0.0.1', 5432),
                  raddr=None,
                  status='NONE',
                  pid=1049)
        ]
        tcp6_conns = [
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr=('127.0.0.1', 5432),
                  raddr=None,
                  status='NONE',
                  pid=1048),
        ]

        mocked_net_connections.side_effect = [
            unix_conns, tcp_conns, tcp6_conns
        ]
        parser = ProcNetParser(1048)
        expected_connections = {
            'unix': [('/var/run/postgres', '5432')],
            'tcp6': [('127.0.0.1', 5432)],
            'tcp': [('127.0.0.1', 5432)]
        }
        self.assertEqual(expected_connections,
                         parser.get_connections_from_sockets())
Esempio n. 5
0
    def test__get_connection_by_type_should_return_conn_params_when_tcp_type_ok(
            self, mocked_net_connections):
        parser = ProcNetParser(1048)
        unix_conn = sconn(fd=3,
                          family=2,
                          type=1,
                          laddr=('127.0.0.1', 5432),
                          raddr=(),
                          status='LISTEN',
                          pid=1048)
        conn_params = parser._get_connection_by_type('tcp', unix_conn)
        self.assertEqual(('127.0.0.1', 5432), conn_params)

        conn_params = parser._get_connection_by_type('tcp6', unix_conn)
        self.assertEqual(('127.0.0.1', 5432), conn_params)
Esempio n. 6
0
 def test__get_connection_by_type_should_return_none_when_unix_type_wrong_format(
         self, mocked_net_connections, mocked_logger):
     parser = ProcNetParser(1048)
     unix_conn = sconn(fd=6,
                       family=1,
                       type=1,
                       laddr='/var/run/.s.PGSQQL.5432',
                       raddr=None,
                       status='NONE',
                       pid=1048)
     conn_params = parser._get_connection_by_type('unix', unix_conn)
     self.assertIsNone(conn_params)
     expected_msg = 'unix socket name is not recognized as belonging to PostgreSQL: {0}'.format(
         unix_conn)
     mocked_logger.warning.assert_called_with(expected_msg)
Esempio n. 7
0
    def test_get_socket_connections_exclude_by_pid(self,
                                                   mocked_net_connections):
        unix_conns = [
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr='/var/run/postgres/.s.PGSQL.5432',
                  raddr=None,
                  status='NONE',
                  pid=1048),
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr='/var/run/postgres/.s.PGSQL.5432',
                  raddr=None,
                  status='NONE',
                  pid=1049)
        ]
        tcp_conns = [
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr=('127.0.0.1', 5432),
                  raddr=None,
                  status='NONE',
                  pid=1048),
            sconn(fd=6,
                  family=1,
                  type=1,
                  laddr=('127.0.0.1', 5432),
                  raddr=None,
                  status='NONE',
                  pid=1049)
        ]

        mocked_net_connections.side_effect = [unix_conns, tcp_conns, []]
        parser = ProcNetParser(1048)

        self.assertEqual(1, len(parser.sockets['unix']))
        self.assertIn(unix_conns[0], parser.sockets['unix'])

        self.assertEqual(1, len(parser.sockets['tcp']))
        self.assertIn(tcp_conns[0], parser.sockets['tcp'])
Esempio n. 8
0
 def test_get_socket_connections_call_net_connections_with_allowed_conn_types(
         self, mocked_net_connections):
     ProcNetParser(1048)
     calls = [mock.call('unix'), mock.call('tcp'), mock.call('tcp6')]
     mocked_net_connections.assert_has_calls(calls, any_order=True)