Skip to content

Commit

Permalink
Add ability to read query from file to postgresql_extensible input (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyqsempai authored and danielnelson committed Sep 12, 2019
1 parent 3ff7e55 commit 3f4375b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
5 changes: 4 additions & 1 deletion plugins/inputs/postgresql_extensible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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=""
Expand Down
36 changes: 35 additions & 1 deletion plugins/inputs/postgresql_extensible/postgresql_extensible.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package postgresql_extensible
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

// register in driver.
Expand All @@ -25,6 +27,7 @@ type Postgresql struct {

type query []struct {
Sqlquery string
Script string
Version int
Withdbname bool
Tagvalue string
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/postgresql_extensible/testdata/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from pg_stat_database

0 comments on commit 3f4375b

Please sign in to comment.