2016-03-14 17:27:07 +08:00
|
|
|
package postgresql_extensible
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2019-09-13 04:38:35 +08:00
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2016-03-14 17:27:07 +08:00
|
|
|
"strings"
|
2021-01-12 02:53:44 +08:00
|
|
|
"time"
|
2016-03-14 17:27:07 +08:00
|
|
|
|
2021-02-09 00:18:40 +08:00
|
|
|
_ "github.com/jackc/pgx/stdlib" //to register stdlib from PostgreSQL Driver and Toolkit
|
2017-04-05 08:37:44 +08:00
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-01-06 08:03:09 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-03-14 17:27:07 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2017-01-25 04:36:36 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/postgresql"
|
2016-03-14 17:27:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Postgresql struct {
|
2018-01-06 08:03:09 +08:00
|
|
|
postgresql.Service
|
|
|
|
|
Databases []string
|
|
|
|
|
AdditionalTags []string
|
2021-01-12 02:53:44 +08:00
|
|
|
Timestamp string
|
2018-09-19 00:08:13 +08:00
|
|
|
Query query
|
|
|
|
|
Debug bool
|
2019-09-24 06:39:50 +08:00
|
|
|
|
|
|
|
|
Log telegraf.Logger
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type query []struct {
|
2016-04-25 20:10:13 +08:00
|
|
|
Sqlquery string
|
2019-09-13 04:38:35 +08:00
|
|
|
Script string
|
2016-04-25 20:10:13 +08:00
|
|
|
Version int
|
|
|
|
|
Withdbname bool
|
|
|
|
|
Tagvalue string
|
|
|
|
|
Measurement string
|
2021-01-12 02:53:44 +08:00
|
|
|
Timestamp string
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
var ignoredColumns = map[string]bool{"stats_reset": true}
|
2016-03-14 17:27:07 +08:00
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-04-01 07:50:24 +08:00
|
|
|
## specify address via a url matching:
|
|
|
|
|
## postgres://[pqgotest[:password]]@localhost[/dbname]\
|
|
|
|
|
## ?sslmode=[disable|verify-ca|verify-full]
|
|
|
|
|
## or a simple string:
|
2020-05-14 15:41:58 +08:00
|
|
|
## host=localhost user=pqgotest password=... sslmode=... dbname=app_production
|
2016-03-14 17:27:07 +08:00
|
|
|
#
|
2016-04-01 07:50:24 +08:00
|
|
|
## All connection parameters are optional. #
|
|
|
|
|
## 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
|
|
|
|
|
## connection with the server and doesn't restrict the databases we are trying
|
|
|
|
|
## to grab metrics for.
|
2016-03-14 17:27:07 +08:00
|
|
|
#
|
|
|
|
|
address = "host=localhost user=postgres sslmode=disable"
|
2018-01-06 08:03:09 +08:00
|
|
|
|
|
|
|
|
## connection configuration.
|
|
|
|
|
## maxlifetime - specify the maximum lifetime of a connection.
|
|
|
|
|
## default is forever (0s)
|
|
|
|
|
max_lifetime = "0s"
|
|
|
|
|
|
2016-04-01 07:50:24 +08:00
|
|
|
## A list of databases to pull metrics about. If not specified, metrics for all
|
|
|
|
|
## databases are gathered.
|
|
|
|
|
## databases = ["app_production", "testing"]
|
2016-03-14 17:27:07 +08:00
|
|
|
#
|
2016-04-25 23:09:35 +08:00
|
|
|
## A custom name for the database that will be used as the "server" tag in the
|
|
|
|
|
## measurement output. If not specified, a default one generated from
|
|
|
|
|
## the connection address is used.
|
2018-01-06 08:03:09 +08:00
|
|
|
# outputaddress = "db01"
|
2016-04-25 23:09:35 +08:00
|
|
|
#
|
2016-04-01 07:50:24 +08:00
|
|
|
## Define the toml config where the sql queries are stored
|
|
|
|
|
## New queries can be added, if the withdbname is set to true and there is no
|
|
|
|
|
## databases defined in the 'databases field', the sql query is ended by a
|
|
|
|
|
## 'is not null' in order to make the query succeed.
|
|
|
|
|
## Example :
|
|
|
|
|
## The sqlquery : "SELECT * FROM pg_stat_database where datname" become
|
|
|
|
|
## "SELECT * FROM pg_stat_database where datname IN ('postgres', 'pgbench')"
|
|
|
|
|
## because the databases variable was set to ['postgres', 'pgbench' ] and the
|
|
|
|
|
## withdbname was true. Be careful that if the withdbname is set to false you
|
|
|
|
|
## don't have to define the where clause (aka with the dbname) the tagvalue
|
2016-04-25 20:10:13 +08:00
|
|
|
## field is used to define custom tags (separated by commas)
|
|
|
|
|
## The optional "measurement" value can be used to override the default
|
|
|
|
|
## output measurement name ("postgresql").
|
2019-09-13 04:38:35 +08:00
|
|
|
##
|
|
|
|
|
## The script option can be used to specify the .sql file path.
|
|
|
|
|
## If script and sqlquery options specified at same time, sqlquery will be used
|
|
|
|
|
##
|
2021-01-12 02:53:44 +08:00
|
|
|
## the tagvalue field is used to define custom tags (separated by comas).
|
|
|
|
|
## the query is expected to return columns which match the names of the
|
|
|
|
|
## defined tags. The values in these columns must be of a string-type,
|
|
|
|
|
## a number-type or a blob-type.
|
|
|
|
|
##
|
|
|
|
|
## The timestamp field is used to override the data points timestamp value. By
|
|
|
|
|
## default, all rows inserted with current time. By setting a timestamp column,
|
|
|
|
|
## the row will be inserted with that column's value.
|
|
|
|
|
##
|
2016-04-01 07:50:24 +08:00
|
|
|
## Structure :
|
|
|
|
|
## [[inputs.postgresql_extensible.query]]
|
|
|
|
|
## sqlquery string
|
|
|
|
|
## version string
|
|
|
|
|
## withdbname boolean
|
2016-04-25 20:10:13 +08:00
|
|
|
## tagvalue string (comma separated)
|
|
|
|
|
## measurement string
|
2021-01-12 02:53:44 +08:00
|
|
|
## timestamp string
|
2016-03-14 17:27:07 +08:00
|
|
|
[[inputs.postgresql_extensible.query]]
|
|
|
|
|
sqlquery="SELECT * FROM pg_stat_database"
|
|
|
|
|
version=901
|
|
|
|
|
withdbname=false
|
|
|
|
|
tagvalue=""
|
2016-04-25 20:10:13 +08:00
|
|
|
measurement=""
|
2016-03-14 17:27:07 +08:00
|
|
|
[[inputs.postgresql_extensible.query]]
|
|
|
|
|
sqlquery="SELECT * FROM pg_stat_bgwriter"
|
|
|
|
|
version=901
|
|
|
|
|
withdbname=false
|
2016-04-25 20:10:13 +08:00
|
|
|
tagvalue="postgresql.stats"
|
2016-03-14 17:27:07 +08:00
|
|
|
`
|
|
|
|
|
|
2019-09-13 04:38:35 +08:00
|
|
|
func (p *Postgresql) Init() error {
|
|
|
|
|
var err error
|
|
|
|
|
for i := range p.Query {
|
|
|
|
|
if p.Query[i].Sqlquery == "" {
|
|
|
|
|
p.Query[i].Sqlquery, err = ReadQueryFromFile(p.Query[i].Script)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
func (p *Postgresql) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Postgresql) Description() string {
|
|
|
|
|
return "Read metrics from one or many postgresql servers"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Postgresql) IgnoredColumns() map[string]bool {
|
|
|
|
|
return ignoredColumns
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 04:38:35 +08:00
|
|
|
func ReadQueryFromFile(filePath string) (string, error) {
|
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
query, err := ioutil.ReadAll(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return string(query), err
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
|
2017-04-05 08:37:44 +08:00
|
|
|
var (
|
2021-03-02 05:04:35 +08:00
|
|
|
err error
|
|
|
|
|
sqlQuery string
|
|
|
|
|
queryAddon string
|
|
|
|
|
dbVersion int
|
|
|
|
|
query string
|
|
|
|
|
tagValue string
|
|
|
|
|
measName string
|
|
|
|
|
timestamp string
|
|
|
|
|
columns []string
|
2017-04-05 08:37:44 +08:00
|
|
|
)
|
2016-03-14 17:27:07 +08:00
|
|
|
|
2020-05-14 15:41:58 +08:00
|
|
|
// Retrieving the database version
|
2018-10-31 05:06:47 +08:00
|
|
|
query = `SELECT setting::integer / 100 AS version FROM pg_settings WHERE name = 'server_version_num'`
|
2021-03-02 05:04:35 +08:00
|
|
|
if err = p.DB.QueryRow(query).Scan(&dbVersion); err != nil {
|
|
|
|
|
dbVersion = 0
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
2018-01-06 08:03:09 +08:00
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
// We loop in order to process each query
|
|
|
|
|
// Query is not run if Database version does not match the query version.
|
|
|
|
|
for i := range p.Query {
|
2021-03-02 05:04:35 +08:00
|
|
|
sqlQuery = p.Query[i].Sqlquery
|
|
|
|
|
tagValue = p.Query[i].Tagvalue
|
2021-01-12 02:53:44 +08:00
|
|
|
timestamp = p.Query[i].Timestamp
|
2019-09-13 04:38:35 +08:00
|
|
|
|
2016-04-25 20:10:13 +08:00
|
|
|
if p.Query[i].Measurement != "" {
|
2021-03-02 05:04:35 +08:00
|
|
|
measName = p.Query[i].Measurement
|
2016-04-25 20:10:13 +08:00
|
|
|
} else {
|
2021-03-02 05:04:35 +08:00
|
|
|
measName = "postgresql"
|
2016-04-25 20:10:13 +08:00
|
|
|
}
|
2016-03-14 17:27:07 +08:00
|
|
|
|
|
|
|
|
if p.Query[i].Withdbname {
|
|
|
|
|
if len(p.Databases) != 0 {
|
2021-03-02 05:04:35 +08:00
|
|
|
queryAddon = fmt.Sprintf(` IN ('%s')`, strings.Join(p.Databases, "','"))
|
2016-03-14 17:27:07 +08:00
|
|
|
} else {
|
2021-03-02 05:04:35 +08:00
|
|
|
queryAddon = " is not null"
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2021-03-02 05:04:35 +08:00
|
|
|
queryAddon = ""
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
2021-03-02 05:04:35 +08:00
|
|
|
sqlQuery += queryAddon
|
2016-03-14 17:27:07 +08:00
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
if p.Query[i].Version <= dbVersion {
|
|
|
|
|
rows, err := p.DB.Query(sqlQuery)
|
2016-03-14 17:27:07 +08:00
|
|
|
if err != nil {
|
2019-09-24 06:39:50 +08:00
|
|
|
p.Log.Error(err.Error())
|
2017-04-25 02:13:26 +08:00
|
|
|
continue
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
// grab the column information from the result
|
2018-01-06 08:03:09 +08:00
|
|
|
if columns, err = rows.Columns(); err != nil {
|
2019-09-24 06:39:50 +08:00
|
|
|
p.Log.Error(err.Error())
|
2017-04-25 02:13:26 +08:00
|
|
|
continue
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
2018-01-06 08:03:09 +08:00
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
p.AdditionalTags = nil
|
2021-03-02 05:04:35 +08:00
|
|
|
if tagValue != "" {
|
|
|
|
|
tagList := strings.Split(tagValue, ",")
|
|
|
|
|
for t := range tagList {
|
|
|
|
|
p.AdditionalTags = append(p.AdditionalTags, tagList[t])
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 02:53:44 +08:00
|
|
|
p.Timestamp = timestamp
|
|
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
for rows.Next() {
|
2021-03-02 05:04:35 +08:00
|
|
|
err = p.accRow(measName, rows, acc, columns)
|
2016-03-14 17:27:07 +08:00
|
|
|
if err != nil {
|
2019-09-24 06:39:50 +08:00
|
|
|
p.Log.Error(err.Error())
|
2017-04-25 02:13:26 +08:00
|
|
|
break
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type scanner interface {
|
|
|
|
|
Scan(dest ...interface{}) error
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
func (p *Postgresql) accRow(measName string, row scanner, acc telegraf.Accumulator, columns []string) error {
|
2017-09-26 01:58:10 +08:00
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
columnVars []interface{}
|
|
|
|
|
dbname bytes.Buffer
|
|
|
|
|
tagAddress string
|
2021-01-12 02:53:44 +08:00
|
|
|
timestamp time.Time
|
2017-09-26 01:58:10 +08:00
|
|
|
)
|
2016-03-14 17:27:07 +08:00
|
|
|
|
|
|
|
|
// this is where we'll store the column name with its *interface{}
|
|
|
|
|
columnMap := make(map[string]*interface{})
|
|
|
|
|
|
2018-01-06 08:03:09 +08:00
|
|
|
for _, column := range columns {
|
2016-03-14 17:27:07 +08:00
|
|
|
columnMap[column] = new(interface{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// populate the array of interface{} with the pointers in the right order
|
|
|
|
|
for i := 0; i < len(columnMap); i++ {
|
2018-01-06 08:03:09 +08:00
|
|
|
columnVars = append(columnVars, columnMap[columns[i]])
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deconstruct array of variables and send to Scan
|
2017-09-26 01:58:10 +08:00
|
|
|
if err = row.Scan(columnVars...); err != nil {
|
2016-03-14 17:27:07 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2017-09-26 01:58:10 +08:00
|
|
|
|
2018-09-19 00:08:13 +08:00
|
|
|
if c, ok := columnMap["datname"]; ok && *c != nil {
|
2016-03-14 17:27:07 +08:00
|
|
|
// extract the database name from the column map
|
2018-09-19 00:08:13 +08:00
|
|
|
switch datname := (*c).(type) {
|
|
|
|
|
case string:
|
|
|
|
|
dbname.WriteString(datname)
|
|
|
|
|
default:
|
|
|
|
|
dbname.WriteString("postgres")
|
|
|
|
|
}
|
2016-03-14 17:27:07 +08:00
|
|
|
} else {
|
|
|
|
|
dbname.WriteString("postgres")
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 01:58:10 +08:00
|
|
|
if tagAddress, err = p.SanitizedAddress(); err != nil {
|
2016-03-14 17:27:07 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process the additional tags
|
2017-09-26 01:58:10 +08:00
|
|
|
tags := map[string]string{
|
|
|
|
|
"server": tagAddress,
|
|
|
|
|
"db": dbname.String(),
|
|
|
|
|
}
|
2016-03-14 17:27:07 +08:00
|
|
|
|
2021-01-12 02:53:44 +08:00
|
|
|
// set default timestamp to Now
|
|
|
|
|
timestamp = time.Now()
|
|
|
|
|
|
2016-03-14 17:27:07 +08:00
|
|
|
fields := make(map[string]interface{})
|
2016-08-09 15:25:59 +08:00
|
|
|
COLUMN:
|
2016-03-14 17:27:07 +08:00
|
|
|
for col, val := range columnMap {
|
2019-09-24 06:39:50 +08:00
|
|
|
p.Log.Debugf("Column: %s = %T: %v\n", col, *val, *val)
|
2016-03-14 17:27:07 +08:00
|
|
|
_, ignore := ignoredColumns[col]
|
2016-08-09 15:25:59 +08:00
|
|
|
if ignore || *val == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-01-25 04:36:36 +08:00
|
|
|
|
2021-01-12 02:53:44 +08:00
|
|
|
if col == p.Timestamp {
|
|
|
|
|
if v, ok := (*val).(time.Time); ok {
|
|
|
|
|
timestamp = v
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 15:25:59 +08:00
|
|
|
for _, tag := range p.AdditionalTags {
|
|
|
|
|
if col != tag {
|
|
|
|
|
continue
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
2016-08-09 15:25:59 +08:00
|
|
|
switch v := (*val).(type) {
|
2017-01-25 04:36:36 +08:00
|
|
|
case string:
|
|
|
|
|
tags[col] = v
|
2016-08-09 15:25:59 +08:00
|
|
|
case []byte:
|
|
|
|
|
tags[col] = string(v)
|
2017-01-25 04:36:36 +08:00
|
|
|
case int64, int32, int:
|
2016-08-09 15:25:59 +08:00
|
|
|
tags[col] = fmt.Sprintf("%d", v)
|
2017-01-25 04:36:36 +08:00
|
|
|
default:
|
2019-09-24 06:39:50 +08:00
|
|
|
p.Log.Debugf("Failed to add %q as additional tag", col)
|
2016-03-17 22:01:08 +08:00
|
|
|
}
|
2016-08-09 15:25:59 +08:00
|
|
|
continue COLUMN
|
|
|
|
|
}
|
2017-09-26 01:58:10 +08:00
|
|
|
|
2016-08-09 15:25:59 +08:00
|
|
|
if v, ok := (*val).([]byte); ok {
|
|
|
|
|
fields[col] = string(v)
|
|
|
|
|
} else {
|
|
|
|
|
fields[col] = *val
|
2016-03-14 17:27:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-02 05:04:35 +08:00
|
|
|
acc.AddFields(measName, fields, tags, timestamp)
|
2016-03-14 17:27:07 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("postgresql_extensible", func() telegraf.Input {
|
2018-01-06 08:03:09 +08:00
|
|
|
return &Postgresql{
|
|
|
|
|
Service: postgresql.Service{
|
|
|
|
|
MaxIdle: 1,
|
|
|
|
|
MaxOpen: 1,
|
|
|
|
|
MaxLifetime: internal.Duration{
|
|
|
|
|
Duration: 0,
|
|
|
|
|
},
|
2018-08-02 06:44:10 +08:00
|
|
|
IsPgBouncer: false,
|
2018-01-06 08:03:09 +08:00
|
|
|
},
|
|
|
|
|
}
|
2016-03-14 17:27:07 +08:00
|
|
|
})
|
|
|
|
|
}
|