Fix support for mongodb/leofs urls without scheme (#2900)

This was broken by changes in go 1.8 to url.Parse.  This change allows
the string but prompts the user to move to the correct url string.
This commit is contained in:
Daniel Nelson 2017-06-08 16:52:01 -07:00 committed by GitHub
parent d2eddec1e8
commit f944bd1feb
3 changed files with 50 additions and 37 deletions

View File

@ -3,6 +3,7 @@ package leofs
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"log"
"net/url" "net/url"
"os/exec" "os/exec"
"strconv" "strconv"
@ -18,7 +19,7 @@ import (
const oid = ".1.3.6.1.4.1.35450" const oid = ".1.3.6.1.4.1.35450"
// For Manager Master // For Manager Master
const defaultEndpoint = "127.0.0.1:4020" const defaultEndpoint = "udp://127.0.0.1:4020"
type ServerType int type ServerType int
@ -135,9 +136,9 @@ var serverTypeMapping = map[string]ServerType{
} }
var sampleConfig = ` var sampleConfig = `
## An array of URI to gather stats about LeoFS. ## An array of URLs of the form:
## Specify an ip or hostname with port. ie 127.0.0.1:4020 ## "udp://" host [ ":" port]
servers = ["127.0.0.1:4021"] servers = ["udp://127.0.0.1:4020"]
` `
func (l *LeoFS) SampleConfig() string { func (l *LeoFS) SampleConfig() string {
@ -154,17 +155,28 @@ func (l *LeoFS) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
var wg sync.WaitGroup var wg sync.WaitGroup
for _, endpoint := range l.Servers { for i, endpoint := range l.Servers {
_, err := url.Parse(endpoint) if !strings.HasPrefix(endpoint, "udp://") {
// Preserve backwards compatibility for hostnames without a
// scheme, broken in go 1.8. Remove in Telegraf 2.0
endpoint = "udp://" + endpoint
log.Printf("W! [inputs.mongodb] Using %q as connection URL; please update your configuration to use an URL", endpoint)
l.Servers[i] = endpoint
}
u, err := url.Parse(endpoint)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("Unable to parse the address:%s, err:%s", endpoint, err)) acc.AddError(fmt.Errorf("Unable to parse address %q: %s", endpoint, err))
continue continue
} }
port, err := retrieveTokenAfterColon(endpoint) if u.Host == "" {
if err != nil { acc.AddError(fmt.Errorf("Unable to parse address %q", endpoint))
acc.AddError(err)
continue continue
} }
port := u.Port()
if port == "" {
port = "4020"
}
st, ok := serverTypeMapping[port] st, ok := serverTypeMapping[port]
if !ok { if !ok {
st = ServerTypeStorage st = ServerTypeStorage

View File

@ -4,12 +4,12 @@
```toml ```toml
[[inputs.mongodb]] [[inputs.mongodb]]
## An array of URI to gather stats about. Specify an ip or hostname ## An array of URLs of the form:
## with optional port add password. ie, ## "mongodb://" [user ":" pass "@"] host [ ":" port]
## For example:
## mongodb://user:auth_key@10.10.3.30:27017, ## mongodb://user:auth_key@10.10.3.30:27017,
## mongodb://10.10.3.33:18832, ## mongodb://10.10.3.33:18832,
## 10.0.0.1:10000, etc. servers = ["mongodb://127.0.0.1:27017"]
servers = ["127.0.0.1:27017"]
gather_perdb_stats = false gather_perdb_stats = false
## Optional SSL Config ## Optional SSL Config
@ -19,14 +19,7 @@
## Use SSL but skip chain & host verification ## Use SSL but skip chain & host verification
# insecure_skip_verify = false # insecure_skip_verify = false
``` ```
This connection uri may be different based on your environment and mongodb
For authenticated mongodb instances use `mongodb://` connection URI
```toml
[[inputs.mongodb]]
servers = ["mongodb://username:password@10.XX.XX.XX:27101/mydatabase?authSource=admin"]
```
This connection uri may be different based on your environement and mongodb
setup. If the user doesn't have the required privilege to execute serverStatus setup. If the user doesn't have the required privilege to execute serverStatus
command the you will get this error on telegraf command the you will get this error on telegraf

View File

@ -4,8 +4,10 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"log"
"net" "net"
"net/url" "net/url"
"strings"
"sync" "sync"
"time" "time"
@ -37,12 +39,12 @@ type Ssl struct {
} }
var sampleConfig = ` var sampleConfig = `
## An array of URI to gather stats about. Specify an ip or hostname ## An array of URLs of the form:
## with optional port add password. ie, ## "mongodb://" [user ":" pass "@"] host [ ":" port]
## For example:
## mongodb://user:auth_key@10.10.3.30:27017, ## mongodb://user:auth_key@10.10.3.30:27017,
## mongodb://10.10.3.33:18832, ## mongodb://10.10.3.33:18832,
## 10.0.0.1:10000, etc. servers = ["mongodb://127.0.0.1:27017"]
servers = ["127.0.0.1:27017"]
gather_perdb_stats = false gather_perdb_stats = false
## Optional SSL Config ## Optional SSL Config
@ -61,7 +63,7 @@ func (*MongoDB) Description() string {
return "Read metrics from one or many MongoDB servers" return "Read metrics from one or many MongoDB servers"
} }
var localhost = &url.URL{Host: "127.0.0.1:27017"} var localhost = &url.URL{Host: "mongodb://127.0.0.1:27017"}
// Reads stats from all configured servers accumulates stats. // Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any). // Returns one of the errors encountered while gather stats (if any).
@ -72,19 +74,25 @@ func (m *MongoDB) Gather(acc telegraf.Accumulator) error {
} }
var wg sync.WaitGroup var wg sync.WaitGroup
for _, serv := range m.Servers { for i, serv := range m.Servers {
if !strings.HasPrefix(serv, "mongodb://") {
// Preserve backwards compatibility for hostnames without a
// scheme, broken in go 1.8. Remove in Telegraf 2.0
serv = "mongodb://" + serv
log.Printf("W! [inputs.mongodb] Using %q as connection URL; please update your configuration to use an URL", serv)
m.Servers[i] = serv
}
u, err := url.Parse(serv) u, err := url.Parse(serv)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("Unable to parse to address '%s': %s", serv, err)) acc.AddError(fmt.Errorf("Unable to parse address %q: %s", serv, err))
continue continue
} else if u.Scheme == "" {
u.Scheme = "mongodb"
// fallback to simple string based address (i.e. "10.0.0.1:10000")
u.Host = serv
if u.Path == u.Host {
u.Path = ""
} }
if u.Host == "" {
acc.AddError(fmt.Errorf("Unable to parse address %q", serv))
continue
} }
wg.Add(1) wg.Add(1)
go func(srv *Server) { go func(srv *Server) {
defer wg.Done() defer wg.Done()