Update postgresql_extensible plugin to hide the password

This commit is contained in:
Thomas Menard 2016-03-11 17:07:30 +01:00
parent 9b2986c882
commit 0d5b749890
1 changed files with 33 additions and 8 deletions

View File

@ -4,12 +4,13 @@ import (
"bytes" "bytes"
"database/sql" "database/sql"
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
_ "github.com/lib/pq" "github.com/lib/pq"
) )
type Postgresql struct { type Postgresql struct {
@ -18,6 +19,7 @@ type Postgresql struct {
OrderedColumns []string OrderedColumns []string
AllColumns []string AllColumns []string
AdditionalTags []string AdditionalTags []string
sanitizedAddress string
Query []struct { Query []struct {
Sqlquery string Sqlquery string
Version int Version int
@ -179,6 +181,23 @@ type scanner interface {
Scan(dest ...interface{}) error Scan(dest ...interface{}) error
} }
var passwordKVMatcher, _ = regexp.Compile("password=\\S+ ?")
func (p *Postgresql) SanitizedAddress() (_ string, err error) {
var canonicalizedAddress string
if strings.HasPrefix(p.Address, "postgres://") || strings.HasPrefix(p.Address, "postgresql://") {
canonicalizedAddress, err = pq.ParseURL(p.Address)
if err != nil {
return p.sanitizedAddress, err
}
} else {
canonicalizedAddress = p.Address
}
p.sanitizedAddress = passwordKVMatcher.ReplaceAllString(canonicalizedAddress, "")
return p.sanitizedAddress, err
}
func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error { func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
var columnVars []interface{} var columnVars []interface{}
var dbname bytes.Buffer var dbname bytes.Buffer
@ -211,10 +230,16 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
dbname.WriteString("postgres") dbname.WriteString("postgres")
} }
var tagAddress string
tagAddress, err = p.SanitizedAddress()
if err != nil {
return err
}
// Process the additional tags // Process the additional tags
tags := map[string]string{} tags := map[string]string{}
tags["server"] = p.Address tags["server"] = tagAddress
tags["db"] = dbname.String() tags["db"] = dbname.String()
fields := make(map[string]interface{}) fields := make(map[string]interface{})