diff --git a/plugins/mongodb/mongodb.go b/plugins/mongodb/mongodb.go index 1b7e5c3a1..28bbe3af0 100644 --- a/plugins/mongodb/mongodb.go +++ b/plugins/mongodb/mongodb.go @@ -1,7 +1,10 @@ package mongodb import ( + "crypto/tls" + "crypto/x509" "fmt" + "net" "net/url" "sync" "time" @@ -12,9 +15,15 @@ import ( type MongoDB struct { Servers []string + Ssl Ssl mongos map[string]*Server } +type Ssl struct { + Enabled bool + CaCerts []string `toml:"cacerts"` +} + var sampleConfig = ` # 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, @@ -92,8 +101,33 @@ func (m *MongoDB) gatherServer(server *Server, acc plugins.Accumulator) error { } dialInfo.Direct = true 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) 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()) } server.Session = sess diff --git a/plugins/mongodb/mongodb_server.go b/plugins/mongodb/mongodb_server.go index f5283105d..d9b0edaad 100644 --- a/plugins/mongodb/mongodb_server.go +++ b/plugins/mongodb/mongodb_server.go @@ -17,7 +17,7 @@ type Server struct { func (s *Server) getDefaultTags() map[string]string { tags := make(map[string]string) - tags["host"] = s.Url.Host + tags["hostname"] = s.Url.Host return tags } diff --git a/plugins/mongodb/mongodb_server_test.go b/plugins/mongodb/mongodb_server_test.go index 07058aa82..ec536bbef 100644 --- a/plugins/mongodb/mongodb_server_test.go +++ b/plugins/mongodb/mongodb_server_test.go @@ -16,7 +16,7 @@ func TestGetDefaultTags(t *testing.T) { in string out string }{ - {"host", server.Url.Host}, + {"hostname", server.Url.Host}, } defaultTags := server.getDefaultTags() for _, tt := range tagTests {