Add ability to read query from file to postgresql_extensible input (#6361)

This commit is contained in:
Boris Popovschi 2019-09-12 23:38:35 +03:00 committed by Daniel Nelson
parent bae12dde1e
commit d717e8ea03
4 changed files with 66 additions and 3 deletions

View File

@ -44,6 +44,9 @@ The example below has two queries are specified, with the following parameters:
# Be careful that if the withdbname is set to false you don't have to define
# the where clause (aka with the dbname)
#
# 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
#
# 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,
@ -61,7 +64,7 @@ The example below has two queries are specified, with the following parameters:
withdbname=false
tagvalue=""
[[inputs.postgresql_extensible.query]]
sqlquery="SELECT * FROM pg_stat_bgwriter"
script="your_sql-filepath.sql"
version=901
withdbname=false
tagvalue=""

View File

@ -3,7 +3,9 @@ package postgresql_extensible
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
// register in driver.
@ -25,6 +27,7 @@ type Postgresql struct {
type query []struct {
Sqlquery string
Script string
Version int
Withdbname bool
Tagvalue string
@ -75,7 +78,10 @@ var sampleConfig = `
## 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").
#
##
## 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
##
## Structure :
## [[inputs.postgresql_extensible.query]]
## sqlquery string
@ -96,6 +102,19 @@ var sampleConfig = `
tagvalue="postgresql.stats"
`
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
}
func (p *Postgresql) SampleConfig() string {
return sampleConfig
}
@ -108,6 +127,20 @@ func (p *Postgresql) IgnoredColumns() map[string]bool {
return ignoredColumns
}
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
}
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
var (
err error
@ -131,6 +164,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
for i := range p.Query {
sql_query = p.Query[i].Sqlquery
tag_value = p.Query[i].Tagvalue
if p.Query[i].Measurement != "" {
meas_name = p.Query[i].Measurement
} else {

View File

@ -25,7 +25,7 @@ func queryRunner(t *testing.T, q query) *testutil.Accumulator {
}
var acc testutil.Accumulator
p.Start(&acc)
p.Init()
require.NoError(t, acc.GatherError(p.Gather))
return &acc
}
@ -201,6 +201,31 @@ func TestPostgresqlFieldOutput(t *testing.T) {
}
}
func TestPostgresqlSqlScript(t *testing.T) {
q := query{{
Script: "testdata/test.sql",
Version: 901,
Withdbname: false,
Tagvalue: "",
}}
p := &Postgresql{
Service: postgresql.Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
),
IsPgBouncer: false,
},
Databases: []string{"postgres"},
Query: q,
}
var acc testutil.Accumulator
p.Start(&acc)
p.Init()
require.NoError(t, acc.GatherError(p.Gather))
}
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")

View File

@ -0,0 +1 @@
select * from pg_stat_database