add SSL support, change tag to hostname
This commit is contained in:
parent
86145d5eb5
commit
c0512e720c
|
@ -1,7 +1,10 @@
|
||||||
package mongodb
|
package mongodb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -12,9 +15,15 @@ import (
|
||||||
|
|
||||||
type MongoDB struct {
|
type MongoDB struct {
|
||||||
Servers []string
|
Servers []string
|
||||||
|
Ssl Ssl
|
||||||
mongos map[string]*Server
|
mongos map[string]*Server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Ssl struct {
|
||||||
|
Enabled bool
|
||||||
|
CaCerts []string `toml:"cacerts"`
|
||||||
|
}
|
||||||
|
|
||||||
var sampleConfig = `
|
var sampleConfig = `
|
||||||
# An array of URI to gather stats about. Specify an ip or hostname
|
# An array of URI to gather stats about. Specify an ip or hostname
|
||||||
# with optional port add password. ie mongodb://user:auth_key@10.10.3.30:27017,
|
# with optional port add password. ie mongodb://user:auth_key@10.10.3.30:27017,
|
||||||
|
@ -92,8 +101,33 @@ func (m *MongoDB) gatherServer(server *Server, acc plugins.Accumulator) error {
|
||||||
}
|
}
|
||||||
dialInfo.Direct = true
|
dialInfo.Direct = true
|
||||||
dialInfo.Timeout = time.Duration(10) * time.Second
|
dialInfo.Timeout = time.Duration(10) * time.Second
|
||||||
|
|
||||||
|
if m.Ssl.Enabled {
|
||||||
|
tlsConfig := &tls.Config{}
|
||||||
|
if len(m.Ssl.CaCerts) > 0 {
|
||||||
|
roots := x509.NewCertPool()
|
||||||
|
for _, caCert := range m.Ssl.CaCerts {
|
||||||
|
ok := roots.AppendCertsFromPEM([]byte(caCert))
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("failed to parse root certificate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tlsConfig.RootCAs = roots
|
||||||
|
} else {
|
||||||
|
tlsConfig.InsecureSkipVerify = true
|
||||||
|
}
|
||||||
|
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
|
||||||
|
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error in Dial, %s\n", err.Error())
|
||||||
|
}
|
||||||
|
return conn, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sess, err := mgo.DialWithInfo(dialInfo)
|
sess, err := mgo.DialWithInfo(dialInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("error dialing over ssl, %s\n", err.Error())
|
||||||
return fmt.Errorf("Unable to connect to MongoDB, %s\n", err.Error())
|
return fmt.Errorf("Unable to connect to MongoDB, %s\n", err.Error())
|
||||||
}
|
}
|
||||||
server.Session = sess
|
server.Session = sess
|
||||||
|
|
|
@ -17,7 +17,7 @@ type Server struct {
|
||||||
|
|
||||||
func (s *Server) getDefaultTags() map[string]string {
|
func (s *Server) getDefaultTags() map[string]string {
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
tags["host"] = s.Url.Host
|
tags["hostname"] = s.Url.Host
|
||||||
return tags
|
return tags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ func TestGetDefaultTags(t *testing.T) {
|
||||||
in string
|
in string
|
||||||
out string
|
out string
|
||||||
}{
|
}{
|
||||||
{"host", server.Url.Host},
|
{"hostname", server.Url.Host},
|
||||||
}
|
}
|
||||||
defaultTags := server.getDefaultTags()
|
defaultTags := server.getDefaultTags()
|
||||||
for _, tt := range tagTests {
|
for _, tt := range tagTests {
|
||||||
|
|
Loading…
Reference in New Issue