diff --git a/plugins/inputs/mongodb/README.md b/plugins/inputs/mongodb/README.md index 89d2cc586b1ee..b00121cc64212 100644 --- a/plugins/inputs/mongodb/README.md +++ b/plugins/inputs/mongodb/README.md @@ -38,6 +38,10 @@ All MongoDB server versions from 2.6 and higher are supported. # tls_key = "/etc/telegraf/key.pem" ## Use TLS but skip chain & host verification # insecure_skip_verify = false + + ## Skip Ping at initialization + ## If you are not sure that your mongodb is already running, skipping the ping operation will allow you to run Telegraf normally and mongodb will try to reconnect by itself + # skip_ping_at_init = false ``` ### Permissions diff --git a/plugins/inputs/mongodb/mongodb.go b/plugins/inputs/mongodb/mongodb.go index c7258378fb114..c11e82a0851a6 100644 --- a/plugins/inputs/mongodb/mongodb.go +++ b/plugins/inputs/mongodb/mongodb.go @@ -25,6 +25,7 @@ type MongoDB struct { GatherPerdbStats bool GatherColStats bool GatherTopStat bool + SkipPingAtInit bool ColStatsDbs []string tlsint.ClientConfig @@ -140,9 +141,10 @@ func (m *MongoDB) Init() error { return fmt.Errorf("unable to connect to MongoDB: %q", err) } - err = client.Ping(ctx, opts.ReadPreference) - if err != nil { - return fmt.Errorf("unable to connect to MongoDB: %s", err) + if m.SkipPingAtInit { + m.Log.Infof("skip ping at initialization to MongoDB: %q", connURL) + } else if err := client.Ping(ctx, opts.ReadPreference); err != nil { + return fmt.Errorf("unable to ping to MongoDB: %s", err) } server := &Server{ diff --git a/plugins/inputs/mongodb/mongodb_skip_test.go b/plugins/inputs/mongodb/mongodb_skip_test.go new file mode 100644 index 0000000000000..e571af17c153e --- /dev/null +++ b/plugins/inputs/mongodb/mongodb_skip_test.go @@ -0,0 +1,20 @@ +package mongodb + +import ( + "testing" + + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/assert" +) + +func TestMongodb_SkipPingAtInit(t *testing.T) { + m := &MongoDB{ + Log: testutil.Logger{}, + Servers: []string{"mongodb://aaa:bbb@127.0.0.1:37017/adminaaa"}, + } + err := m.Init() + assert.Error(t, err) + m.SkipPingAtInit = true + err = m.Init() + assert.NoError(t, err) +} diff --git a/plugins/inputs/mongodb/sample.conf b/plugins/inputs/mongodb/sample.conf new file mode 100644 index 0000000000000..06c7540d556db --- /dev/null +++ b/plugins/inputs/mongodb/sample.conf @@ -0,0 +1,37 @@ +# Read metrics from one or many MongoDB servers +[[inputs.mongodb]] + ## An array of URLs of the form: + ## "mongodb://" [user ":" pass "@"] host [ ":" port] + ## For example: + ## mongodb://user:auth_key@10.10.3.30:27017, + ## mongodb://10.10.3.33:18832, + servers = ["mongodb://127.0.0.1:27017/?connect=direct"] + + ## When true, collect cluster status. + ## Note that the query that counts jumbo chunks triggers a COLLSCAN, which + ## may have an impact on performance. + # gather_cluster_status = true + + ## When true, collect per database stats + # gather_perdb_stats = false + + ## When true, collect per collection stats + # gather_col_stats = false + + ## When true, collect usage statistics for each collection + ## (insert, update, queries, remove, getmore, commands etc...). + # gather_top_stat = false + + ## List of db where collections stats are collected + ## If empty, all db are concerned + # col_stats_dbs = ["local"] + + ## 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 + # insecure_skip_verify = false + + ## Skip Ping at initialization + # skip_ping_at_init = true