From 6b7c0c698d843ae6f599908573b792e3cde3777a Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Thu, 4 Aug 2016 14:08:14 -0500 Subject: [PATCH 1/2] [postgres] Allow check to use UNIX socket as host The PostgreSQL check has been changed to try to connect to the PostgreSQL database via a UNIX socket if the hostname provided in the configuration begins with a forward-slash ('/'). This behaviour is similar to the 'psql' command-line utility. From psql(1): --host=hostname Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix-domain socket. With the difference being that we interpret it as an exact path, whereas psql interprets it as a directory to look for a socket with a particularly formatted name. --- checks.d/postgres.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/checks.d/postgres.py b/checks.d/postgres.py index 8193c6ade1..db1ec1510c 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -584,6 +584,11 @@ def get_connection(self, key, host, port, user, password, dbname, ssl, use_cache elif port != '': connection = pg.connect(host=host, port=port, user=user, password=password, database=dbname, ssl=ssl) + elif host.startswith('/'): + # If the hostname starts with /, it's probably a path + # to a UNIX socket. This is similar behaviour to psql + connection = pg.connect(unix_sock=host, user=user, + password=password, database=dbname) else: connection = pg.connect(host=host, user=user, password=password, database=dbname, ssl=ssl) From e662bdd31c52d1eaab9c09fba09454d3beef9101 Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Thu, 4 Aug 2016 14:34:34 -0500 Subject: [PATCH 2/2] [postgres] Update example to mention UNIX sockets postgres.yaml.example now mentions how one might connect to PostgreSQL using a UNIX socket. --- conf.d/postgres.yaml.example | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/conf.d/postgres.yaml.example b/conf.d/postgres.yaml.example index b250e9b932..74cde77c1c 100644 --- a/conf.d/postgres.yaml.example +++ b/conf.d/postgres.yaml.example @@ -11,6 +11,10 @@ instances: # - optional_tag1 # - optional_tag2 +# Connect using a UNIX socket (host must begin with a /) +# - host: /run/postgresql/.s.PGSQL.5433 +# dbname: db_name + # Track per-relation (table) metrics # The list of relations/tables must be specified here. # Each relation generates many metrics (10 + 10 per index) @@ -56,4 +60,4 @@ instances: # Collect count metrics, default value is True for backward compatibility but they migth be slow, # suggested value is False. # collect_count_metrics: False -# \ No newline at end of file +#