Update the postgresql plugin to add the pg_stat_bgwriter view

This commit is contained in:
Thomas Menard 2016-02-11 01:34:17 +01:00
parent 76dd728c97
commit eac52f1929
2 changed files with 44 additions and 40 deletions

View File

@ -1,6 +1,6 @@
# PostgreSQL plugin # PostgreSQL plugin
This postgresql plugin provides metrics for your postgres database. It currently works with postgres versions 8.1+. It uses data from the built in _pg_stat_database_ view. The metrics recorded depend on your version of postgres. See table: This postgresql plugin provides metrics for your postgres database. It currently works with postgres versions 8.1+. It uses data from the built in _pg_stat_database_ and pg_stat_bgwriter views. The metrics recorded depend on your version of postgres. See table:
``` ```
pg version 9.2+ 9.1 8.3-9.0 8.1-8.2 7.4-8.0(unsupported) pg version 9.2+ 9.1 8.3-9.0 8.1-8.2 7.4-8.0(unsupported)
--- --- --- ------- ------- ------- --- --- --- ------- ------- -------
@ -27,4 +27,6 @@ stats_reset* x x
_* value ignored and therefore not recorded._ _* value ignored and therefore not recorded._
More information about the meaning of these metrics can be found in the [PostgreSQL Documentation](http://www.postgresql.org/docs/9.2/static/monitoring-stats.html#PG-STAT-DATABASE-VIEW) More information about the meaning of these metrics can be found in the [PostgreSQL Documentation](http://www.postgresql.org/docs/9.2/static/monitoring-stats.html#PG-STAT-DATABASE-VIEW)

View File

@ -21,22 +21,22 @@ type Postgresql struct {
var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true} var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
var sampleConfig = ` var sampleConfig = `
# specify address via a url matching: ### specify address via a url matching:
# postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full] ### postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full]
# or a simple string: ### or a simple string:
# host=localhost user=pqotest password=... sslmode=... dbname=app_production ### host=localhost user=pqotest password=... sslmode=... dbname=app_production
# ###
# All connection parameters are optional. ### All connection parameters are optional.
# ###
# Without the dbname parameter, the driver will default to a database ### Without the dbname parameter, the driver will default to a database
# with the same name as the user. This dbname is just for instantiating a ### with the same name as the user. This dbname is just for instantiating a
# connection with the server and doesn't restrict the databases we are trying ### connection with the server and doesn't restrict the databases we are trying
# to grab metrics for. ### to grab metrics for.
# ###
address = "host=localhost user=postgres sslmode=disable" address = "host=localhost user=postgres sslmode=disable"
# A list of databases to pull metrics about. If not specified, metrics for all ### A list of databases to pull metrics about. If not specified, metrics for all
# databases are gathered. ### databases are gathered.
# databases = ["app_production", "testing"] # databases = ["app_production", "testing"]
` `
@ -94,30 +94,29 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
return err return err
} }
} }
//return rows.Err()
query = `SELECT * FROM pg_stat_bgwriter` query = `SELECT * FROM pg_stat_bgwriter`
rows, err := db.Query(query) bg_writer_row, err := db.Query(query)
if err != nil { if err != nil {
return err return err
} }
defer rows.Close() defer bg_writer_row.Close()
// grab the column information from the result // grab the column information from the result
p.OrderedColumns, err = rows.Columns() p.OrderedColumns, err = bg_writer_row.Columns()
if err != nil { if err != nil {
return err return err
} }
for rows.Next() { for bg_writer_row.Next() {
err = p.accRow(rows, acc) err = p.accRow(bg_writer_row, acc)
if err != nil { if err != nil {
return err return err
} }
} }
return bg_writer_row.Err()
return rows.Err()
} }
type scanner interface { type scanner interface {
@ -146,12 +145,15 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
if err != nil { if err != nil {
return err return err
} }
if columnMap["datname"] != nil {
// extract the database name from the column map // extract the database name from the column map
dbnameChars := (*columnMap["datname"]).([]uint8) dbnameChars := (*columnMap["datname"]).([]uint8)
for i := 0; i < len(dbnameChars); i++ { for i := 0; i < len(dbnameChars); i++ {
dbname.WriteString(string(dbnameChars[i])) dbname.WriteString(string(dbnameChars[i]))
} }
} else {
dbname.WriteString("postgres")
}
tags := map[string]string{"server": p.Address, "db": dbname.String()} tags := map[string]string{"server": p.Address, "db": dbname.String()}