Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql database blacklist configuration option #1699

Merged
merged 21 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a27d5ee
separate hello and authenticate functions, force connection close at …
janxious Jun 21, 2016
05a0186
update changelog, though this will need to be updated again to merge …
janxious Jun 21, 2016
298d928
bump instrumental agent version
janxious Jun 21, 2016
b64d5f7
fix test to deal with better better connect/reconnect logic and chang…
janxious Jun 23, 2016
149dea9
Update CHANGELOG.md
janxious Jun 23, 2016
bb35a5a
go fmt
janxious Jun 25, 2016
b4c8ef6
Split out Instrumental tests for invalid metric and value.
jqr Jun 27, 2016
66dc5a4
Ensure nothing remains on the wire after final test.
jqr Jun 27, 2016
eb9744d
Force valid metric names by replacing invalid parts with underscores.
jqr Jun 27, 2016
227ffbb
Multiple invalid characters being joined into a single udnerscore.
jqr Jun 27, 2016
42f1a14
Adjust comment to what happens.
jqr Jun 27, 2016
3d0f5d2
undo split hello and auth commands, to reduce roundtrips
janxious Jun 27, 2016
dec9535
Merge pull request #1 from Instrumental/connect-disconnect-logic
jqr Jun 27, 2016
8d75740
Merge pull request #2 from Instrumental/underscore-metric-names
jqr Jun 27, 2016
0aa8e36
adjust formatting to match main telegraf repo
mediocretes Sep 1, 2016
398479c
Add ignored_databases option to postgresql configuration files, to en…
mediocretes Sep 1, 2016
51db125
run go fmt on new postgresql database whitelist/blacklist code
mediocretes Sep 1, 2016
f820f99
Merge branch 'master' of https://github.com/influxdata/telegraf into …
mediocretes Sep 6, 2016
088e093
Merge branch 'master' of github.com:Instrumental/telegraf into postgr…
mediocretes Sep 6, 2016
60d3d40
add postgresql database blacklist option to changelog
mediocretes Sep 6, 2016
5750fa5
remove a bad merge from the changelog
mediocretes Sep 6, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [#1599](https://github.com/influxdata/telegraf/pull/1599): Add server hostname for each docker measurements.
- [#1697](https://github.com/influxdata/telegraf/pull/1697): Add NATS output plugin.
- [#1407](https://github.com/influxdata/telegraf/pull/1407): HTTP service listener input plugin.
- [#1699](https://github.com/influxdata/telegraf/pull/1699): Add database blacklist option for Postgresql

### Bugfixes

Expand Down
12 changes: 10 additions & 2 deletions plugins/inputs/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Postgresql struct {
Address string
Databases []string
IgnoredDatabases []string
OrderedColumns []string
AllColumns []string
sanitizedAddress string
Expand All @@ -40,8 +41,12 @@ var sampleConfig = `
##
address = "host=localhost user=postgres sslmode=disable"

## A list of databases to explicitly ignore. If not specified, metrics for all
## databases are gathered. Do NOT use with the 'databases' option.
# ignored_databases = ["postgres", "template0", "template1"]

## A list of databases to pull metrics about. If not specified, metrics for all
## databases are gathered.
## databases are gathered. Do NOT use with the 'ignore_databases' option.
# databases = ["app_production", "testing"]
`

Expand Down Expand Up @@ -73,8 +78,11 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {

defer db.Close()

if len(p.Databases) == 0 {
if len(p.Databases) == 0 && len(p.IgnoredDatabases) == 0 {
query = `SELECT * FROM pg_stat_database`
} else if len(p.IgnoredDatabases) != 0 {
query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname NOT IN ('%s')`,
strings.Join(p.IgnoredDatabases, "','"))
} else {
query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname IN ('%s')`,
strings.Join(p.Databases, "','"))
Expand Down
72 changes: 72 additions & 0 deletions plugins/inputs/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,75 @@ func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
assert.False(t, acc.HasMeasurement(col))
}
}

func TestPostgresqlDatabaseWhitelistTest(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

p := &Postgresql{
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
Databases: []string{"template0"},
}

var acc testutil.Accumulator

err := p.Gather(&acc)
require.NoError(t, err)

var foundTemplate0 = false
var foundTemplate1 = false

for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template0" {
foundTemplate0 = true
}
}
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template1" {
foundTemplate1 = true
}
}
}

assert.True(t, foundTemplate0)
assert.False(t, foundTemplate1)
}

func TestPostgresqlDatabaseBlacklistTest(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

p := &Postgresql{
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
IgnoredDatabases: []string{"template0"},
}

var acc testutil.Accumulator

err := p.Gather(&acc)
require.NoError(t, err)

var foundTemplate0 = false
var foundTemplate1 = false

for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template0" {
foundTemplate0 = true
}
}
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "template1" {
foundTemplate1 = true
}
}
}

assert.False(t, foundTemplate0)
assert.True(t, foundTemplate1)
}