<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> from diamond . collector import str_to_bool <EOL> try : <EOL> import psycopg2 <EOL> import psycopg2 . extras <EOL> except ImportError : <EOL> psycopg2 = None <EOL> class PostgresqlCollector ( diamond . collector . Collector ) : <EOL> """<STR_LIT>""" <EOL> def get_default_config_help ( self ) : <EOL> """<STR_LIT>""" <EOL> config_help = super ( PostgresqlCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( PostgresqlCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> return config <EOL> def collect ( self ) : <EOL> """<STR_LIT>""" <EOL> if psycopg2 is None : <EOL> self . log . error ( '<STR_LIT>' ) <EOL> return { } <EOL> dbs = self . _get_db_names ( ) <EOL> if len ( dbs ) == <NUM_LIT:0> : <EOL> self . log . error ( "<STR_LIT>" ) <EOL> return { } <EOL> if self . config [ '<STR_LIT>' ] : <EOL> metrics = self . config [ '<STR_LIT>' ] <EOL> elif str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> metrics = registry [ '<STR_LIT>' ] <EOL> if str_to_bool ( self . config [ '<STR_LIT>' ] ) and '<STR_LIT>' not in metrics : <EOL> metrics . append ( '<STR_LIT>' ) <EOL> else : <EOL> metrics = registry [ '<STR_LIT>' ] <EOL> for metric_name in set ( metrics ) : <EOL> if metric_name not in metrics_registry : <EOL> self . log . error ( <EOL> '<STR_LIT>' % metric_name ) <EOL> continue <EOL> for dbase in dbs : <EOL> conn = self . _connect ( database = dbase ) <EOL> try : <EOL> klass = metrics_registry [ metric_name ] <EOL> stat = klass ( dbase , conn , <EOL> underscore = self . config [ '<STR_LIT>' ] ) <EOL> stat . fetch ( self . config [ '<STR_LIT>' ] ) <EOL> for metric , value in stat : <EOL> if value is not None : <EOL> self . publish ( metric , value ) <EOL> if stat . multi_db is False : <EOL> break <EOL> finally : <EOL> conn . close ( ) <EOL> def _get_db_names ( self ) : <EOL> """<STR_LIT>""" <EOL> query = """<STR_LIT>""" <EOL> conn = self . _connect ( self . config [ '<STR_LIT>' ] ) <EOL> cursor = conn . cursor ( cursor_factory = psycopg2 . extras . DictCursor ) <EOL> cursor . execute ( query ) <EOL> datnames = [ d [ '<STR_LIT>' ] for d in cursor . fetchall ( ) ] <EOL> conn . close ( ) <EOL> if not datnames : <EOL> datnames = [ '<STR_LIT>' ] <EOL> return datnames <EOL> def _connect ( self , database = None ) : <EOL> """<STR_LIT>""" <EOL> conn_args = { <EOL> '<STR_LIT:host>' : self . config [ '<STR_LIT:host>' ] , <EOL> '<STR_LIT:user>' : self . config [ '<STR_LIT:user>' ] , <EOL> '<STR_LIT:password>' : self . config [ '<STR_LIT:password>' ] , <EOL> '<STR_LIT:port>' : self . config [ '<STR_LIT:port>' ] , <EOL> '<STR_LIT>' : self . config [ '<STR_LIT>' ] , <EOL> } <EOL> if database : <EOL> conn_args [ '<STR_LIT>' ] = database <EOL> else : <EOL> conn_args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> conn = psycopg2 . connect ( ** conn_args ) <EOL> conn . set_isolation_level ( <NUM_LIT:0> ) <EOL> return conn <EOL> class QueryStats ( object ) : <EOL> query = None <EOL> path = None <EOL> def __init__ ( self , dbname , conn , parameters = None , underscore = False ) : <EOL> self . conn = conn <EOL> self . dbname = dbname <EOL> self . underscore = underscore <EOL> self . parameters = parameters <EOL> self . data = list ( ) <EOL> def _translate_datname ( self , datname ) : <EOL> """<STR_LIT>""" <EOL> if self . underscore : <EOL> datname = datname . replace ( "<STR_LIT:_>" , "<STR_LIT:.>" ) <EOL> return datname <EOL> def fetch ( self , pg_version ) : <EOL> if float ( pg_version ) >= <NUM_LIT> and hasattr ( self , '<STR_LIT>' ) : <EOL> q = self . post_92_query <EOL> else : <EOL> q = self . query <EOL> cursor = self . conn . cursor ( cursor_factory = psycopg2 . extras . DictCursor ) <EOL> try : <EOL> cursor . execute ( q , self . parameters ) <EOL> rows = cursor . fetchall ( ) <EOL> for row in rows : <EOL> if len ( row ) == <NUM_LIT:2> : <EOL> self . data . append ( { <EOL> '<STR_LIT>' : self . _translate_datname ( self . dbname ) , <EOL> '<STR_LIT>' : row [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:value>' : row [ <NUM_LIT:1> ] , <EOL> } ) <EOL> else : <EOL> for key , value in row . iteritems ( ) : <EOL> if key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , ) : <EOL> continue <EOL> self . data . append ( { <EOL> '<STR_LIT>' : self . _translate_datname ( row . get ( <EOL> '<STR_LIT>' , self . dbname ) ) , <EOL> '<STR_LIT>' : row . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : row . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : row . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : row . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : key , <EOL> '<STR_LIT:value>' : value , <EOL> } ) <EOL> finally : <EOL> cursor . close ( ) <EOL> def __iter__ ( self ) : <EOL> for data_point in self . data : <EOL> yield ( self . path % data_point , data_point [ '<STR_LIT:value>' ] ) <EOL> class DatabaseStats ( QueryStats ) : <EOL> """<STR_LIT>""" <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> post_92_query = """<STR_LIT>""" <EOL> query = post_92_query . replace ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) . replace ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class UserFunctionStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class UserTableStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class UserIndexStats ( QueryStats ) : <EOL> path = "<STR_LIT>" "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class UserTableIOStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class UserIndexIOStats ( QueryStats ) : <EOL> path = "<STR_LIT>" "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class ConnectionStateStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> post_92_query = """<STR_LIT>""" <EOL> class LockStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class RelationSizeStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class BackgroundWriterStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class WalSegmentStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class TransactionCount ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class IdleInTransactions ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> base_query = """<STR_LIT>""" <EOL> query = base_query % ( "<STR_LIT>" , ) <EOL> post_92_query = base_query % ( "<STR_LIT>" , ) <EOL> class LongestRunningQueries ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> base_query = """<STR_LIT>""" <EOL> query = base_query % ( "<STR_LIT>" , ) <EOL> post_92_query = base_query % ( "<STR_LIT>" , ) <EOL> class UserConnectionCount ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> post_92_query = query . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class DatabaseConnectionCount ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class TableScanStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class TupleAccessStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = True <EOL> query = """<STR_LIT>""" <EOL> class DatabaseReplicationStats ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> class DatabaseXidAge ( QueryStats ) : <EOL> path = "<STR_LIT>" <EOL> multi_db = False <EOL> query = """<STR_LIT>""" <EOL> metrics_registry = { <EOL> '<STR_LIT>' : DatabaseStats , <EOL> '<STR_LIT>' : DatabaseConnectionCount , <EOL> '<STR_LIT>' : UserFunctionStats , <EOL> '<STR_LIT>' : UserTableStats , <EOL> '<STR_LIT>' : UserIndexStats , <EOL> '<STR_LIT>' : UserTableIOStats , <EOL> '<STR_LIT>' : UserIndexIOStats , <EOL> '<STR_LIT>' : ConnectionStateStats , <EOL> '<STR_LIT>' : LockStats , <EOL> '<STR_LIT>' : RelationSizeStats , <EOL> '<STR_LIT>' : BackgroundWriterStats , <EOL> '<STR_LIT>' : WalSegmentStats , <EOL> '<STR_LIT>' : TransactionCount , <EOL> '<STR_LIT>' : IdleInTransactions , <EOL> '<STR_LIT>' : LongestRunningQueries , <EOL> '<STR_LIT>' : UserConnectionCount , <EOL> '<STR_LIT>' : TableScanStats , <EOL> '<STR_LIT>' : TupleAccessStats , <EOL> '<STR_LIT>' : DatabaseReplicationStats , <EOL> '<STR_LIT>' : DatabaseXidAge , <EOL> } <EOL> registry = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> } </s>
