2015-07-07 01:20:11 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2015-07-09 20:06:18 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2015-07-07 01:20:11 +00:00
|
|
|
"fmt"
|
2015-07-09 20:06:18 +00:00
|
|
|
"net"
|
2015-07-07 01:20:11 +00:00
|
|
|
"net/url"
|
2017-06-08 23:52:01 +00:00
|
|
|
"strings"
|
2015-07-07 01:20:11 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsint "github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-07-07 01:20:11 +00:00
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MongoDB struct {
|
2016-07-19 11:47:12 +00:00
|
|
|
Servers []string
|
|
|
|
Ssl Ssl
|
|
|
|
mongos map[string]*Server
|
|
|
|
GatherPerdbStats bool
|
2019-07-31 04:36:19 +00:00
|
|
|
GatherColStats bool
|
|
|
|
ColStatsDbs []string
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsint.ClientConfig
|
2019-09-23 22:39:50 +00:00
|
|
|
|
|
|
|
Log telegraf.Logger
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:06:18 +00:00
|
|
|
type Ssl struct {
|
|
|
|
Enabled bool
|
|
|
|
CaCerts []string `toml:"cacerts"`
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
var sampleConfig = `
|
2017-06-08 23:52:01 +00:00
|
|
|
## An array of URLs of the form:
|
|
|
|
## "mongodb://" [user ":" pass "@"] host [ ":" port]
|
|
|
|
## For example:
|
2016-02-18 21:26:51 +00:00
|
|
|
## mongodb://user:auth_key@10.10.3.30:27017,
|
|
|
|
## mongodb://10.10.3.33:18832,
|
2017-06-08 23:52:01 +00:00
|
|
|
servers = ["mongodb://127.0.0.1:27017"]
|
2018-04-03 23:58:56 +00:00
|
|
|
|
|
|
|
## When true, collect per database stats
|
|
|
|
# gather_perdb_stats = false
|
2019-09-09 22:55:46 +00:00
|
|
|
|
2019-07-31 04:36:19 +00:00
|
|
|
## When true, collect per collection stats
|
|
|
|
# gather_col_stats = false
|
2019-09-09 22:55:46 +00:00
|
|
|
|
2019-07-31 04:36:19 +00:00
|
|
|
## List of db where collections stats are collected
|
|
|
|
## If empty, all db are concerned
|
|
|
|
# col_stats_dbs = ["local"]
|
2017-03-10 19:27:55 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
2017-03-10 19:27:55 +00:00
|
|
|
# insecure_skip_verify = false
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-07-07 01:20:11 +00:00
|
|
|
|
|
|
|
func (m *MongoDB) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*MongoDB) Description() string {
|
|
|
|
return "Read metrics from one or many MongoDB servers"
|
|
|
|
}
|
|
|
|
|
2017-06-08 23:52:01 +00:00
|
|
|
var localhost = &url.URL{Host: "mongodb://127.0.0.1:27017"}
|
2015-07-07 01:20:11 +00:00
|
|
|
|
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
2016-01-27 21:21:36 +00:00
|
|
|
func (m *MongoDB) Gather(acc telegraf.Accumulator) error {
|
2015-07-07 01:20:11 +00:00
|
|
|
if len(m.Servers) == 0 {
|
|
|
|
m.gatherServer(m.getMongoServer(localhost), acc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2017-06-08 23:52:01 +00:00
|
|
|
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
|
2019-09-23 22:39:50 +00:00
|
|
|
m.Log.Warnf("Using %q as connection URL; please update your configuration to use an URL", serv)
|
2017-06-08 23:52:01 +00:00
|
|
|
m.Servers[i] = serv
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
u, err := url.Parse(serv)
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
m.Log.Errorf("Unable to parse address %q: %s", serv, err.Error())
|
2017-04-24 18:13:26 +00:00
|
|
|
continue
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
2017-06-08 23:52:01 +00:00
|
|
|
if u.Host == "" {
|
2019-09-23 22:39:50 +00:00
|
|
|
m.Log.Errorf("Unable to parse address %q", serv)
|
2017-06-08 23:52:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
wg.Add(1)
|
2016-05-17 10:02:51 +00:00
|
|
|
go func(srv *Server) {
|
2015-07-07 01:20:11 +00:00
|
|
|
defer wg.Done()
|
2019-09-23 22:39:50 +00:00
|
|
|
m.Log.Error(m.gatherServer(srv, acc))
|
2016-05-17 10:02:51 +00:00
|
|
|
}(m.getMongoServer(u))
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MongoDB) getMongoServer(url *url.URL) *Server {
|
|
|
|
if _, ok := m.mongos[url.Host]; !ok {
|
|
|
|
m.mongos[url.Host] = &Server{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: m.Log,
|
2015-07-07 01:20:11 +00:00
|
|
|
Url: url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.mongos[url.Host]
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (m *MongoDB) gatherServer(server *Server, acc telegraf.Accumulator) error {
|
2015-07-07 01:20:11 +00:00
|
|
|
if server.Session == nil {
|
|
|
|
var dialAddrs []string
|
|
|
|
if server.Url.User != nil {
|
|
|
|
dialAddrs = []string{server.Url.String()}
|
|
|
|
} else {
|
|
|
|
dialAddrs = []string{server.Url.Host}
|
|
|
|
}
|
|
|
|
dialInfo, err := mgo.ParseURL(dialAddrs[0])
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
return fmt.Errorf("unable to parse URL %q: %s", dialAddrs[0], err.Error())
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
dialInfo.Direct = true
|
2016-04-04 09:59:28 +00:00
|
|
|
dialInfo.Timeout = 5 * time.Second
|
2015-07-09 20:06:18 +00:00
|
|
|
|
2017-03-10 19:27:55 +00:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
|
2015-07-09 20:06:18 +00:00
|
|
|
if m.Ssl.Enabled {
|
2018-05-04 23:33:23 +00:00
|
|
|
// Deprecated TLS config
|
2017-03-10 19:27:55 +00:00
|
|
|
tlsConfig = &tls.Config{}
|
2015-07-09 20:06:18 +00:00
|
|
|
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
|
|
|
|
}
|
2017-03-10 19:27:55 +00:00
|
|
|
} else {
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsConfig, err = m.ClientConfig.TLSConfig()
|
2018-04-23 20:29:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-10 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If configured to use TLS, add a dial function
|
|
|
|
if tlsConfig != nil {
|
2015-07-09 20:06:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:20:11 +00:00
|
|
|
sess, err := mgo.DialWithInfo(dialInfo)
|
|
|
|
if err != nil {
|
2019-09-23 22:39:50 +00:00
|
|
|
return fmt.Errorf("unable to connect to MongoDB: %s", err.Error())
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
server.Session = sess
|
|
|
|
}
|
2019-07-31 04:36:19 +00:00
|
|
|
return server.gatherData(acc, m.GatherPerdbStats, m.GatherColStats, m.ColStatsDbs)
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("mongodb", func() telegraf.Input {
|
2015-07-07 01:20:11 +00:00
|
|
|
return &MongoDB{
|
2019-09-09 22:55:46 +00:00
|
|
|
ColStatsDbs: []string{"local"},
|
|
|
|
mongos: make(map[string]*Server),
|
2015-07-07 01:20:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|