forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgbouncer.go
201 lines (164 loc) · 4.28 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
package pgbouncer
import (
"bytes"
"strconv"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/postgresql"
_ "github.com/jackc/pgx/stdlib" // register driver
)
type PgBouncer struct {
postgresql.Service
}
var ignoredColumns = map[string]bool{"user": true, "database": true, "pool_mode": true,
"avg_req": true, "avg_recv": true, "avg_sent": true, "avg_query": true,
}
var sampleConfig = `
## specify address via a url matching:
## postgres://[pqgotest[:password]]@localhost[/dbname]\
## ?sslmode=[disable|verify-ca|verify-full]
## or a simple string:
## host=localhost user=pqotest password=... sslmode=... dbname=app_production
##
## All connection parameters are optional.
##
address = "host=localhost user=pgbouncer sslmode=disable"
`
func (p *PgBouncer) SampleConfig() string {
return sampleConfig
}
func (p *PgBouncer) Description() string {
return "Read metrics from one or many pgbouncer servers"
}
func (p *PgBouncer) Gather(acc telegraf.Accumulator) error {
var (
err error
query string
columns []string
)
query = `SHOW STATS`
rows, err := p.DB.Query(query)
if err != nil {
return err
}
defer rows.Close()
// grab the column information from the result
if columns, err = rows.Columns(); err != nil {
return err
}
for rows.Next() {
tags, columnMap, err := p.accRow(rows, acc, columns)
if err != nil {
return err
}
fields := make(map[string]interface{})
for col, val := range columnMap {
_, ignore := ignoredColumns[col]
if ignore {
continue
}
switch v := (*val).(type) {
case int64:
// Integer fields are returned in pgbouncer 1.5 through 1.9
fields[col] = v
case string:
// Integer fields are returned in pgbouncer 1.12
integer, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
fields[col] = integer
}
}
acc.AddFields("pgbouncer", fields, tags)
}
err = rows.Err()
if err != nil {
return err
}
query = `SHOW POOLS`
poolRows, err := p.DB.Query(query)
if err != nil {
return err
}
defer poolRows.Close()
// grab the column information from the result
if columns, err = poolRows.Columns(); err != nil {
return err
}
for poolRows.Next() {
tags, columnMap, err := p.accRow(poolRows, acc, columns)
if err != nil {
return err
}
if user, ok := columnMap["user"]; ok {
if s, ok := (*user).(string); ok && s != "" {
tags["user"] = s
}
}
if poolMode, ok := columnMap["pool_mode"]; ok {
if s, ok := (*poolMode).(string); ok && s != "" {
tags["pool_mode"] = s
}
}
fields := make(map[string]interface{})
for col, val := range columnMap {
_, ignore := ignoredColumns[col]
if !ignore {
fields[col] = *val
}
}
acc.AddFields("pgbouncer_pools", fields, tags)
}
return poolRows.Err()
}
type scanner interface {
Scan(dest ...interface{}) error
}
func (p *PgBouncer) accRow(row scanner, acc telegraf.Accumulator, columns []string) (map[string]string,
map[string]*interface{}, error) {
var columnVars []interface{}
var dbname bytes.Buffer
// this is where we'll store the column name with its *interface{}
columnMap := make(map[string]*interface{})
for _, column := range columns {
columnMap[column] = new(interface{})
}
// populate the array of interface{} with the pointers in the right order
for i := 0; i < len(columnMap); i++ {
columnVars = append(columnVars, columnMap[columns[i]])
}
// deconstruct array of variables and send to Scan
err := row.Scan(columnVars...)
if err != nil {
return nil, nil, err
}
if columnMap["database"] != nil {
// extract the database name from the column map
dbname.WriteString((*columnMap["database"]).(string))
} else {
dbname.WriteString("postgres")
}
var tagAddress string
tagAddress, err = p.SanitizedAddress()
if err != nil {
return nil, nil, err
}
// Return basic tags and the mapped columns
return map[string]string{"server": tagAddress, "db": dbname.String()}, columnMap, nil
}
func init() {
inputs.Add("pgbouncer", func() telegraf.Input {
return &PgBouncer{
Service: postgresql.Service{
MaxIdle: 1,
MaxOpen: 1,
MaxLifetime: internal.Duration{
Duration: 0,
},
IsPgBouncer: true,
},
}
})
}