-
Notifications
You must be signed in to change notification settings - Fork 2
/
telebouncer.py
243 lines (226 loc) · 7.9 KB
/
telebouncer.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
import argparse
import psycopg2
import itertools
import socket
# Possible commands to send to pgBouncer according to https://pgbouncer.github.io/usage.html#admin-console
queries = [
'active_sockets',
'clients',
'databases',
'fds',
'lists',
'mem',
'pools',
'servers',
'sockets',
'stats',
]
# A mapping for the columns of each query to assign them on the tag_set or field_set
# This should be changed according to your needs
mapping = {
'active_sockets': {
'type' : 'tag',
'user' : 'tag',
'database' : 'tag',
'state' : 'tag',
'addr' : 'field',
'port' : 'field',
'local_addr' : 'field',
'local_port' : 'field',
'connect_time' : 'field',
'request_time' : 'field',
'ptr' : 'off',
'link' : 'off',
'remote_pid' : 'off',
'tls' : 'off',
'recv_pos' : 'field',
'pkt_pos' : 'field',
'pkt_remain' : 'field',
'send_pos' : 'field',
'send_remain' : 'field',
'pkt_avail' : 'field',
'send_avail' : 'field',
},
'clients': {
'type' : 'tag',
'user' : 'tag',
'database' : 'tag',
'state' : 'tag',
'addr' : 'field',
'port' : 'field',
'local_addr' : 'off',
'local_port' : 'off',
'connect_time' : 'field',
'request_time' : 'field',
'ptr' : 'off',
'link' : 'off',
'remote_pid' : 'off',
'tls' : 'off',
},
'databases': {
'name' : 'tag',
'host' : 'field',
'port' : 'field',
'database' : 'field',
'force_user' : 'off',
'pool_size' : 'field',
'reserve_pool' : 'field',
'pool_mode' : 'tag',
'max_connections' : 'field',
'current_connections' : 'field',
},
'fds': {
'fd' : 'tag',
'task' : 'tag',
'user' : 'tag',
'database' : 'tag',
'addr' : 'field',
'port' : 'field',
'cancel' : 'off',
'link' : 'off',
'client_encoding' : 'off',
'std_strings' : 'off',
'datestyle' : 'off',
'timezone' : 'field',
'password' : 'off',
},
'lists': {
'list' : 'tag',
'items' : 'field',
},
'mem': {
'name' : 'tag',
'size' : 'field',
'used' : 'field',
'free' : 'field',
'memtotal' : 'field',
},
'pools': {
'database' : 'tag',
'user' : 'tag',
'cl_active' : 'field',
'cl_waiting' : 'field',
'sv_active' : 'field',
'sv_idle' : 'field',
'sv_used' : 'field',
'sv_tested' : 'field',
'sv_login' : 'field',
'maxwait' : 'field',
'pool_mode' : 'tag',
},
'servers': {
'type' : 'tag',
'user' : 'tag',
'database' : 'tag',
'state' : 'tag',
'addr' : 'field',
'port' : 'field',
'local_addr' : 'field',
'local_port' : 'field',
'connect_time' : 'field',
'request_time' : 'field',
'ptr' : 'off',
'link' : 'off',
'remote_pid' : 'off',
'tls' : 'off',
},
'sockets': {
'type' : 'tag',
'user' : 'tag',
'database' : 'tag',
'state' : 'tag',
'addr' : 'field',
'port' : 'field',
'local_addr' : 'field',
'local_port' : 'field',
'connect_time' : 'field',
'request_time' : 'field',
'ptr' : 'off',
'link' : 'off',
'remote_pid' : 'off',
'tls' : 'off',
'recv_pos' : 'field',
'pkt_pos' : 'field',
'pkt_remain' : 'field',
'send_pos' : 'field',
'send_remain' : 'field',
'pkt_avail' : 'field',
'send_avail' : 'field',
},
'stats': {
'database' : 'tag',
'total_requests' : 'field',
'total_received' : 'field',
'total_sent' : 'field',
'total_query_time' : 'field',
'avg_req' : 'field',
'avg_recv' : 'field',
'avg_sent' : 'field',
'avg_query' : 'field',
},
}
# host and port are optional as they have the defaults according to pgBouncer default installation values
# username is required as it must be a valid user from either stats_user or admin_users from pgBouncer's settings
# password is optional because if you have a ~/.pgpass file in place with proper values psycopg2 can get the password from there, as it is a wrapper for the libpq
parser = argparse.ArgumentParser(conflict_handler='resolve')
parser.add_argument("-h", "--host", help="pgBouncer host or IP (default: \"127.0.0.1\")", type=str, default='127.0.0.1')
parser.add_argument("-p", "--port", help="pgBouncer port (pgBouncer default: \"6432\")", type=str, default='6432')
parser.add_argument("-U", "--username", help="a valid user from pgBouncer \"stats_users\" or \"admin_users\" config", dest='user', type=str, required=True)
parser.add_argument("-W", "--password", help="password for username", type=str)
parser.add_argument("query", help="Which internal pgBouncer query to run", type=str, choices=queries)
args = parser.parse_args()
# We use the parameters passed on the command line to create the connection string
# And also add the dbname for pgBouncer's virtual database
params = vars(args)
params.update({'dbname': 'pgbouncer'})
# Extract the desired query
qtype = params.pop('query')
# Putting everything together for the connection string
conn_str = " ".join([k + '=' + v for k, v in params.items() if v is not None])
try:
conn = psycopg2.connect(conn_str)
except:
print("Error connecting to pgBouncer's database!")
quit(1)
conn.autocommit = True
cur = conn.cursor()
query = " ".join(["SHOW", qtype.upper()])
try:
cur.execute(query)
except:
print("Error running query!")
quit(2)
# To make it easier to parse later on we put the results together on a dictionary
cols = [desc[0] for desc in cur.description]
res = [dict(itertools.zip_longest(cols, row)) for row in cur.fetchall()]
cur.close()
conn.close()
# Desired output according to https://docs.influxdata.com/influxdb/v1.3/write_protocols/line_protocol_tutorial/
# measurement,tag_set field_set
server = socket.gethostname()
measurement = 'pgbouncer_' + qtype
for item in res:
# tlist is a list of "key=value" strings for the tag_set
# flist is a list of "key=value" string for the field_set
tlist = ['server='+server]
flist = []
for k, v in item.items():
# Create two key=value lists: one for tag_set and one for field_set
if mapping[qtype][k] == 'tag':
# Goes to the tlist
tlist.append("=".join([k, str(v)]))
elif mapping[qtype][k] == 'field':
# Goes to the flist
flist.append("=".join([k, str(v)]) if isinstance(v, (int, float)) else "=".join([k, '"' + str(v) + '"']))
else:
# Ignore columns with 'off' on the mapping
continue
# Create the tag_set and field_set joining the "key=value" items from their respective lists
tag_set = ",".join([kvitem for kvitem in tlist])
field_set = ",".join([kvitem for kvitem in flist])
# Merge it all together in one line
output = ",".join([measurement, tag_set])
output = " ".join([output, field_set])
# Print each result on its own line the way InfluxDB expects them
print(output)