import psycopg2
from psycopg2 import extensions
from psycopg2.extras import DictCursor
+from psycopg2 import OperationalError
from base import SimpleService
self.table_stats = configuration.pop('table_stats', True)
self.index_stats = configuration.pop('index_stats', True)
self.configuration = configuration
- self.connection = None
+ self.connection = False
self.is_superuser = False
self.data = {}
self.databases = set()
params = dict(user='postgres',
database=None,
password=None,
- host='localhost',
+ host=None,
port=5432)
params.update(self.configuration)
if not self.connection:
- self.connection = psycopg2.connect(**params)
- self.connection.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
- self.connection.set_session(readonly=True)
+ try:
+ self.connection = psycopg2.connect(**params)
+ self.connection.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
+ self.connection.set_session(readonly=True)
+ except OperationalError:
+ return False
+ return True
def check(self):
try:
- self._connect()
+ if not self._connect():
+ self.error('Can\'t connect to %s' % str(self.configuration))
+ return False
cursor = self.connection.cursor()
self._discover_databases(cursor)
self._check_if_superuser(cursor)
self.definitions[chart_name]['lines'].append([lock_id, label, 'absolute'])
def _get_data(self):
- self._connect()
-
- cursor = self.connection.cursor(cursor_factory=DictCursor)
- self.add_stats(cursor)
-
- cursor.close()
- return self.data
+ if self._connect():
+ cursor = self.connection.cursor(cursor_factory=DictCursor)
+ try:
+ self.add_stats(cursor)
+ except OperationalError:
+ self.connection = False
+ cursor.close()
+ return None
+ else:
+ cursor.close()
+ return self.data
+ else:
+ return None
def add_stats(self, cursor):
self.add_database_stats(cursor)