forked from deliveroo/pgbouncer-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgbouncer.go
353 lines (319 loc) · 7.27 KB
/
pgbouncer.go
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"context"
"database/sql"
"github.com/pkg/errors"
)
// User as returned by PGBouncer's SHOW USERS
type User struct {
Name string
PoolMode *string
}
// Config item as returned by PGBouncer's SHOW CONFIG
type Config struct {
Key string
Value *string
Changeable bool
}
// Database as returned by PGBouncer's SHOW DATABASES
type Database struct {
Name string
Host *string
Port string
Database string
ForceUser *string
PoolSize int
ReservePool int
PoolMode *string
MaxConnections int
CurrentConnections int
}
// Pool as returned by PGBouncer's SHOW POOLS
type Pool struct {
Database string
User string
ClActive int
ClWaiting int
SvActive int
SvIdle int
SvUsed int
SvTested int
SvLogin int
MaxWait int
PoolMode string
}
// Client as returned by PGBouncer's SHOW CLIENTS
type Client struct {
Type string
User string
Database string
State string
Addr string
Port int
LocalAddr string
LocalPort int
ConnectTime string
RequestTime string
Ptr string
Link string
RemotePid int
TLS string
}
// Server as returned by PGBouncer's SHOW SERVERS
type Server struct {
Type string
User string
Database string
State string
Addr string
Port int
LocalAddr string
LocalPort int
ConnectTime string
RequestTime string
Ptr string
Link string
RemotePid int
TLS string
}
// Mem info record as returned by PGBouncer's SHOW MEM
type Mem struct {
Name string
Size int
Used int
Free int
MemTotal int
}
// Stat record as returned by PGBouncer's SHOW STAT
type Stat struct {
Database string
TotalRequests int
TotalReceived int
TotalSent int
TotalQueryTime int
AvgReq int
AvgRecv int
AvgSent int
AvgQuery int
}
func unwrapNullString(in sql.NullString) *string {
if in.Valid {
return &in.String
}
return nil
}
func getUsers(ctx context.Context, db *sql.DB) ([]User, error) {
rows, err := db.QueryContext(ctx, "SHOW USERS")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var users []User
for rows.Next() {
var user User
var rawPoolMode sql.NullString
if err := rows.Scan(&user.Name, &rawPoolMode); err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
user.PoolMode = unwrapNullString(rawPoolMode)
users = append(users, user)
}
return users, nil
}
func getConfigs(ctx context.Context, db *sql.DB) ([]Config, error) {
rows, err := db.QueryContext(ctx, "SHOW CONFIG")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var configs []Config
for rows.Next() {
var config Config
var rawValue sql.NullString
var rawChangeable string
if err := rows.Scan(&config.Key, &rawValue, &rawChangeable); err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
config.Changeable = rawChangeable == "yes"
config.Value = unwrapNullString(rawValue)
configs = append(configs, config)
}
return configs, nil
}
func getDatabases(ctx context.Context, db *sql.DB) ([]Database, error) {
rows, err := db.QueryContext(ctx, "SHOW DATABASES")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var databases []Database
for rows.Next() {
var database Database
var rawHost sql.NullString
var rawForceUser sql.NullString
var rawPoolMode sql.NullString
err := rows.Scan(
&database.Name,
&rawHost,
&database.Port,
&database.Database,
&rawForceUser,
&database.PoolSize,
&database.ReservePool,
&rawPoolMode,
&database.MaxConnections,
&database.CurrentConnections,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
database.Host = unwrapNullString(rawHost)
database.ForceUser = unwrapNullString(rawForceUser)
database.PoolMode = unwrapNullString(rawPoolMode)
databases = append(databases, database)
}
return databases, nil
}
func getPools(ctx context.Context, db *sql.DB) ([]Pool, error) {
rows, err := db.QueryContext(ctx, "SHOW POOLS")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var pools []Pool
for rows.Next() {
var pool Pool
err := rows.Scan(
&pool.Database,
&pool.User,
&pool.ClActive,
&pool.ClWaiting,
&pool.SvActive,
&pool.SvIdle,
&pool.SvUsed,
&pool.SvTested,
&pool.SvLogin,
&pool.MaxWait,
&pool.PoolMode,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
pools = append(pools, pool)
}
return pools, nil
}
func getClients(ctx context.Context, db *sql.DB) ([]Client, error) {
rows, err := db.QueryContext(ctx, "SHOW CLIENTS")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var clients []Client
for rows.Next() {
var client Client
err := rows.Scan(
&client.Type,
&client.User,
&client.Database,
&client.State,
&client.Addr,
&client.Port,
&client.LocalAddr,
&client.LocalPort,
&client.ConnectTime,
&client.RequestTime,
&client.Ptr,
&client.Link,
&client.RemotePid,
&client.TLS,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
clients = append(clients, client)
}
return clients, nil
}
func getServers(ctx context.Context, db *sql.DB) ([]Server, error) {
rows, err := db.QueryContext(ctx, "SHOW SERVERS")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var servers []Server
for rows.Next() {
var server Server
err := rows.Scan(
&server.Type,
&server.User,
&server.Database,
&server.State,
&server.Addr,
&server.Port,
&server.LocalAddr,
&server.LocalPort,
&server.ConnectTime,
&server.RequestTime,
&server.Ptr,
&server.Link,
&server.RemotePid,
&server.TLS,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
servers = append(servers, server)
}
return servers, nil
}
func getMems(ctx context.Context, db *sql.DB) ([]Mem, error) {
rows, err := db.QueryContext(ctx, "SHOW MEM")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var mems []Mem
for rows.Next() {
var mem Mem
err := rows.Scan(
&mem.Name,
&mem.Size,
&mem.Used,
&mem.Free,
&mem.MemTotal,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
mems = append(mems, mem)
}
return mems, nil
}
func getStats(ctx context.Context, db *sql.DB) ([]Stat, error) {
rows, err := db.QueryContext(ctx, "SHOW STATS")
if err != nil {
return nil, errors.Wrap(err, "Failed to query PGBouncer")
}
defer rows.Close()
var stats []Stat
for rows.Next() {
var stat Stat
err := rows.Scan(
&stat.Database,
&stat.TotalRequests,
&stat.TotalReceived,
&stat.TotalSent,
&stat.TotalQueryTime,
&stat.AvgReq,
&stat.AvgRecv,
&stat.AvgSent,
&stat.AvgQuery,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch row from results")
}
stats = append(stats, stat)
}
return stats, nil
}