telegraf/plugins/inputs/postgresql/postgresql.go

179 lines
3.8 KiB
Go
Raw Normal View History

//go:generate ../../../tools/readme_config_includer/generator
2015-05-19 01:46:44 +08:00
package postgresql
import (
"bytes"
_ "embed"
"fmt"
"strings"
2015-05-19 01:46:44 +08:00
2022-08-02 19:52:17 +08:00
// Blank import required to register driver
2021-06-03 11:28:16 +08:00
_ "github.com/jackc/pgx/v4/stdlib"
2017-04-05 08:37:44 +08:00
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
2016-01-21 02:57:35 +08:00
"github.com/influxdata/telegraf/plugins/inputs"
2015-05-19 01:46:44 +08:00
)
//go:embed sample.conf
var sampleConfig string
type Postgresql struct {
Service
Databases []string `toml:"databases"`
IgnoredDatabases []string `toml:"ignored_databases"`
PreparedStatements bool `toml:"prepared_statements"`
2015-05-19 01:46:44 +08:00
}
var ignoredColumns = map[string]bool{"stats_reset": true}
func (*Postgresql) SampleConfig() string {
return sampleConfig
}
func (p *Postgresql) IgnoredColumns() map[string]bool {
return ignoredColumns
}
func (p *Postgresql) Init() error {
p.Service.IsPgBouncer = !p.PreparedStatements
return nil
}
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
2017-04-05 08:37:44 +08:00
var (
err error
query string
columns []string
2017-04-05 08:37:44 +08:00
)
if len(p.Databases) == 0 && len(p.IgnoredDatabases) == 0 {
query = `SELECT * FROM pg_stat_database`
} else if len(p.IgnoredDatabases) != 0 {
query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname NOT IN ('%s')`,
strings.Join(p.IgnoredDatabases, "','"))
} else {
query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname IN ('%s')`,
strings.Join(p.Databases, "','"))
}
2015-05-19 01:46:44 +08:00
rows, err := p.DB.Query(query)
if err != nil {
return err
}
2015-05-19 01:46:44 +08:00
defer rows.Close()
2015-05-19 01:46:44 +08:00
2015-09-14 00:51:50 +08:00
// grab the column information from the result
if columns, err = rows.Columns(); err != nil {
return err
}
2015-05-19 01:46:44 +08:00
for rows.Next() {
err = p.accRow(rows, acc, columns)
if err != nil {
return err
2015-05-19 01:46:44 +08:00
}
}
2017-04-05 08:37:44 +08:00
query = `SELECT * FROM pg_stat_bgwriter`
bgWriterRow, err := p.DB.Query(query)
if err != nil {
return err
}
defer bgWriterRow.Close()
// grab the column information from the result
if columns, err = bgWriterRow.Columns(); err != nil {
return err
}
2015-05-19 01:46:44 +08:00
for bgWriterRow.Next() {
err = p.accRow(bgWriterRow, acc, columns)
if err != nil {
return err
}
}
return bgWriterRow.Err()
2015-05-19 01:46:44 +08:00
}
type scanner interface {
Scan(dest ...interface{}) error
}
func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator, columns []string) error {
var dbname bytes.Buffer
2015-09-14 00:51:50 +08:00
// 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{})
}
columnVars := make([]interface{}, 0, len(columnMap))
2015-09-14 00:51:50 +08:00
// 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]])
}
2015-09-14 00:51:50 +08:00
// deconstruct array of variables and send to Scan
err := row.Scan(columnVars...)
2015-05-19 01:46:44 +08:00
if err != nil {
return err
}
if columnMap["datname"] != nil {
// extract the database name from the column map
if dbNameStr, ok := (*columnMap["datname"]).(string); ok {
if _, err := dbname.WriteString(dbNameStr); err != nil {
return err
}
} else {
// PG 12 adds tracking of global objects to pg_stat_database
if _, err := dbname.WriteString("postgres_global"); err != nil {
return err
}
}
} else {
if _, err := dbname.WriteString("postgres"); err != nil {
return err
}
}
var tagAddress string
tagAddress, err = p.SanitizedAddress()
if err != nil {
return err
}
tags := map[string]string{"server": tagAddress, "db": dbname.String()}
2015-12-19 08:09:01 +08:00
fields := make(map[string]interface{})
for col, val := range columnMap {
_, ignore := ignoredColumns[col]
if !ignore {
2015-12-19 08:09:01 +08:00
fields[col] = *val
}
}
2015-12-19 08:09:01 +08:00
acc.AddFields("postgresql", fields, tags)
2015-05-19 01:46:44 +08:00
return nil
}
func init() {
inputs.Add("postgresql", func() telegraf.Input {
return &Postgresql{
Service: Service{
MaxIdle: 1,
MaxOpen: 1,
MaxLifetime: config.Duration(0),
},
PreparedStatements: true,
}
2015-05-19 01:46:44 +08:00
})
}