/
pgbouncer.py
33 lines (26 loc) · 1.14 KB
/
pgbouncer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
from munin.postgres import MuninPostgresPlugin
class MuninPgBouncerPlugin(MuninPostgresPlugin):
dbname_in_args = False
default_table = "pgbouncer"
category = "PgBouncer"
def __init__(self, *args, **kwargs):
super(MuninPgBouncerPlugin, self).__init__(*args, **kwargs)
self.dbwatched = sys.argv[0].rsplit('_', 1)[-1]
def connection(self):
if not hasattr(self, '_connection'):
import psycopg2
self._connection = psycopg2.connect(self.dsn)
self._connection.set_isolation_level(0)
return self._connection
def execute(self):
cursor = self.cursor()
cursor.execute(self.command)
columns = [column[0] for column in cursor.description]
totals = dict.fromkeys((field[0] for field in self.fields), 0)
for row in cursor:
row_dict = dict(zip(columns, row))
if row_dict['database'] in (self.dbwatched, self.dbwatched + '\x00'):
for field in self.fields:
totals[field[0]] += row_dict[field[0]]
return dict((field[0], totals[field[0]]) for field in self.fields)