Skip to content

Commit

Permalink
[postgres] Allow 'host' config value be UNIX socket path (#2734)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DylanFrese authored and olivielpeau committed Oct 27, 2016
1 parent cf6b834 commit 94b5856
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions checks.d/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ def get_connection(self, key, host, port, user, password, dbname, ssl, connect_f
elif port != '':
connection = connect_fct(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 = connect_fct(host=host, user=user, password=password,
database=dbname, ssl=ssl)
Expand Down
4 changes: 4 additions & 0 deletions conf.d/postgres.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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)
Expand Down

0 comments on commit 94b5856

Please sign in to comment.