diff options
author | Christopher Baines <chris@lucida.cbaines.net> | 2015-12-24 10:59:02 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2015-12-30 13:09:00 +0000 |
commit | b12129f0da219291e52b1725dae24528c882a55e (patch) | |
tree | cd37cd6b7f4a53eb7078dfdc1ff54639c9b45491 /prometheus_pgbouncer_exporter/utils.py | |
download | prometheus-pgbouncer-exporter-b12129f0da219291e52b1725dae24528c882a55e.tar prometheus-pgbouncer-exporter-b12129f0da219291e52b1725dae24528c882a55e.tar.gz |
Initial commit
Diffstat (limited to 'prometheus_pgbouncer_exporter/utils.py')
-rw-r--r-- | prometheus_pgbouncer_exporter/utils.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/prometheus_pgbouncer_exporter/utils.py b/prometheus_pgbouncer_exporter/utils.py new file mode 100644 index 0000000..6789290 --- /dev/null +++ b/prometheus_pgbouncer_exporter/utils.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import psycopg2 + + +def get_connection(user, port): + connection = psycopg2.connect( + database='pgbouncer', + user=user, + port=port, + ) + + # pgbouncer does not support transactions (as it does not make sense to), + # so don't start a transaction when connecting + connection.set_session(autocommit=True) + + return connection + + +def get_data_by_named_column(connection, key): + with connection.cursor() as cursor: + cursor.execute('SHOW %s;' % key) + + rows = cursor.fetchall() + column_names = list(column.name for column in cursor.description) + + return [ + dict(zip(column_names, row)) + for row in rows + ] + + +def get_data_by_named_row(connection, key): + with connection.cursor() as cursor: + cursor.execute('SHOW %s;' % key) + + return dict(cursor.fetchall()) |