15
15
Collector Utilities for PostgreSQL.
16
16
"""
17
17
18
- import sys
19
18
import os
20
- import time
21
19
import socket
22
- import errno
23
20
24
21
try :
25
- import psycopg2
22
+ import psycopg2
26
23
except ImportError :
27
- psycopg2 = None # handled in main()
24
+ psycopg2 = None # handled in main()
28
25
29
- CONNECT_TIMEOUT = 2 # seconds
26
+ CONNECT_TIMEOUT = 2 # seconds
30
27
31
28
from collectors .lib import utils
32
29
from collectors .etc import postgresqlconf
33
30
34
31
# Directories under which to search socket files
35
32
SEARCH_DIRS = frozenset ([
36
- "/var/run/postgresql" , # Debian default
37
- "/var/pgsql_socket" , # MacOS default
38
- "/usr/local/var/postgres" , # custom compilation
39
- "/tmp" , # custom compilation
33
+ "/var/run/postgresql" , # Debian default
34
+ "/var/pgsql_socket" , # MacOS default
35
+ "/usr/local/var/postgres" , # custom compilation
36
+ "/tmp" , # custom compilation
40
37
])
41
38
39
+
42
40
def find_sockdir ():
43
- """Returns a path to PostgreSQL socket file to monitor."""
44
- for dir in SEARCH_DIRS :
45
- for dirpath , dirnames , dirfiles in os .walk (dir , followlinks = True ):
46
- for name in dirfiles :
47
- # ensure selection of PostgreSQL socket only
48
- if (utils .is_sockfile (os .path .join (dirpath , name )) and "PGSQL" in name ):
49
- return (dirpath )
41
+ """Returns a path to PostgreSQL socket file to monitor."""
42
+ for dir in SEARCH_DIRS :
43
+ for dirpath , dirnames , dirfiles in os .walk (dir , followlinks = True ):
44
+ for name in dirfiles :
45
+ # ensure selection of PostgreSQL socket only
46
+ if (utils .is_sockfile (os .path .join (dirpath , name )) and "PGSQL" in name ):
47
+ return dirpath
48
+
50
49
51
50
def postgres_connect (sockdir ):
52
- """Connects to the PostgreSQL server using the specified socket file."""
53
- user , password = postgresqlconf .get_user_password ()
51
+ """Connects to the PostgreSQL server using the specified socket file."""
52
+ user , password = postgresqlconf .get_user_password ()
53
+
54
+ try :
55
+ return psycopg2 .connect ("host='%s' user='%s' password='%s' "
56
+ "connect_timeout='%s' dbname=postgres"
57
+ % (sockdir , user , password ,
58
+ CONNECT_TIMEOUT ))
59
+ except (EnvironmentError , EOFError , RuntimeError , socket .error ) as e :
60
+ utils .err ("Couldn't connect to DB :%s" % (e ))
54
61
55
- try :
56
- return psycopg2 .connect ("host='%s' user='%s' password='%s' "
57
- "connect_timeout='%s' dbname=postgres"
58
- % (sockdir , user , password ,
59
- CONNECT_TIMEOUT ))
60
- except (EnvironmentError , EOFError , RuntimeError , socket .error ) as e :
61
- utils .err ("Couldn't connect to DB :%s" % (e ))
62
62
63
63
def connect ():
64
- """Returns an initialized connection to Postgresql."""
65
- if psycopg2 is None :
66
- raise RuntimeError ("error: Python module 'psycopg2' is missing" )
64
+ """Returns an initialized connection to Postgresql."""
65
+ if psycopg2 is None :
66
+ raise RuntimeError ("error: Python module 'psycopg2' is missing" )
67
67
68
- sockdir = find_sockdir ()
69
- if not sockdir : # Nothing to monitor
70
- raise RuntimeError ("error: Can't find postgresql socket file" )
68
+ sockdir = find_sockdir ()
69
+ if not sockdir : # Nothing to monitor
70
+ raise RuntimeError ("error: Can't find postgresql socket file" )
71
71
72
- db = postgres_connect (sockdir )
73
- db .autocommit = True
72
+ db = postgres_connect (sockdir )
73
+ db .autocommit = True
74
74
75
- return db
75
+ return db
0 commit comments