Add ability to read query from file to postgresql_extensible input (#6361)
This commit is contained in:
parent
bae12dde1e
commit
d717e8ea03
|
@ -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
|
# Be careful that if the withdbname is set to false you don't have to define
|
||||||
# the where clause (aka with the dbname)
|
# 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 tagvalue field is used to define custom tags (separated by comas).
|
||||||
# the query is expected to return columns which match the names of the
|
# 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,
|
# 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
|
withdbname=false
|
||||||
tagvalue=""
|
tagvalue=""
|
||||||
[[inputs.postgresql_extensible.query]]
|
[[inputs.postgresql_extensible.query]]
|
||||||
sqlquery="SELECT * FROM pg_stat_bgwriter"
|
script="your_sql-filepath.sql"
|
||||||
version=901
|
version=901
|
||||||
withdbname=false
|
withdbname=false
|
||||||
tagvalue=""
|
tagvalue=""
|
||||||
|
|
|
@ -3,7 +3,9 @@ package postgresql_extensible
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
// register in driver.
|
// register in driver.
|
||||||
|
@ -25,6 +27,7 @@ type Postgresql struct {
|
||||||
|
|
||||||
type query []struct {
|
type query []struct {
|
||||||
Sqlquery string
|
Sqlquery string
|
||||||
|
Script string
|
||||||
Version int
|
Version int
|
||||||
Withdbname bool
|
Withdbname bool
|
||||||
Tagvalue string
|
Tagvalue string
|
||||||
|
@ -75,7 +78,10 @@ var sampleConfig = `
|
||||||
## field is used to define custom tags (separated by commas)
|
## field is used to define custom tags (separated by commas)
|
||||||
## The optional "measurement" value can be used to override the default
|
## The optional "measurement" value can be used to override the default
|
||||||
## output measurement name ("postgresql").
|
## 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 :
|
## Structure :
|
||||||
## [[inputs.postgresql_extensible.query]]
|
## [[inputs.postgresql_extensible.query]]
|
||||||
## sqlquery string
|
## sqlquery string
|
||||||
|
@ -96,6 +102,19 @@ var sampleConfig = `
|
||||||
tagvalue="postgresql.stats"
|
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 {
|
func (p *Postgresql) SampleConfig() string {
|
||||||
return sampleConfig
|
return sampleConfig
|
||||||
}
|
}
|
||||||
|
@ -108,6 +127,20 @@ func (p *Postgresql) IgnoredColumns() map[string]bool {
|
||||||
return ignoredColumns
|
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 {
|
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
@ -131,6 +164,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
|
||||||
for i := range p.Query {
|
for i := range p.Query {
|
||||||
sql_query = p.Query[i].Sqlquery
|
sql_query = p.Query[i].Sqlquery
|
||||||
tag_value = p.Query[i].Tagvalue
|
tag_value = p.Query[i].Tagvalue
|
||||||
|
|
||||||
if p.Query[i].Measurement != "" {
|
if p.Query[i].Measurement != "" {
|
||||||
meas_name = p.Query[i].Measurement
|
meas_name = p.Query[i].Measurement
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -25,7 +25,7 @@ func queryRunner(t *testing.T, q query) *testutil.Accumulator {
|
||||||
}
|
}
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
p.Start(&acc)
|
p.Start(&acc)
|
||||||
|
p.Init()
|
||||||
require.NoError(t, acc.GatherError(p.Gather))
|
require.NoError(t, acc.GatherError(p.Gather))
|
||||||
return &acc
|
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) {
|
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("Skipping integration test in short mode")
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
select * from pg_stat_database
|
Loading…
Reference in New Issue