dcu / mongodb_exporter

MongoDB exporter for prometheus.io
MIT License
353 stars 216 forks source link

nil pointer panic when monitoring a mongodb with no username and password set #110

Open yeya24 opened 5 years ago

yeya24 commented 5 years ago

Start mongodb in 192.168.2.254 centos 7 with no username and password set

docker run -d -p 27017:27017 mongo:4.1

Then I start the exporter binary in another centos in the same subnet

./mongodb_exporter-linux-amd64 -mongodb.uri mongodb://192.168.2.254:27017

The error is

Listening on :9001 (scheme=HTTP, secured=no, clientValidation=no)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x508228]

goroutine 35 [running]:
panic(0x73d340, 0xc4200100d0)
    /usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/dcu/mongodb_exporter/collector.(*PreloadStats).Export(0x0, 0xc4201521e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/metrics.go:363 +0x38
github.com/dcu/mongodb_exporter/collector.(*ReplStats).Export(0xc4202100c0, 0xc4201521e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/metrics.go:352 +0x90
github.com/dcu/mongodb_exporter/collector.(*MetricsStats).Export(0xc4200f7c80, 0xc4201521e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/metrics.go:435 +0x4c7
github.com/dcu/mongodb_exporter/collector.(*ServerStatus).Export(0xc42008fae0, 0xc4201521e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/server_status.go:111 +0x1f9
github.com/dcu/mongodb_exporter/collector.(*MongodbCollector).collectServerStatus(0xc420014eb0, 0xc42016aea0, 0xc4201521e0, 0x7235e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/mongodb_collector.go:85 +0xe3
github.com/dcu/mongodb_exporter/collector.(*MongodbCollector).Collect(0xc420014eb0, 0xc4201521e0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/collector/mongodb_collector.go:64 +0x2d4
github.com/dcu/mongodb_exporter/vendor/github.com/prometheus/client_golang/prometheus.(*Registry).Gather.func2(0xc42014a090, 0xc4201521e0, 0x928900, 0xc420014eb0)
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/vendor/github.com/prometheus/client_golang/prometheus/registry.go:433 +0x63
created by github.com/dcu/mongodb_exporter/vendor/github.com/prometheus/client_golang/prometheus.(*Registry).Gather
    /Users/dc/code/go/src/github.com/dcu/mongodb_exporter/vendor/github.com/prometheus/client_golang/prometheus/registry.go:434 +0x326
ealexhaywood commented 5 years ago

Check out https://github.com/percona/mongodb_exporter which is a fork of this repository and seems more active than this one. I'm curious if it would resolve this issue

n1o commented 5 years ago

Hi @yeya24 , does this issue occur if you provide username and password? Because I am experiencing the same issue, but i have not tried it with credentials. This issue appeared after I upgrade from mongo 3.6 to 4.1.9. However I was able to apply the following quick fix:

func (replStats *ReplStats) Export(ch chan<- prometheus.Metric) {
    replStats.Apply.Export(ch)
    replStats.Buffer.Export(ch)
    replStats.Network.Export(ch)

    if replStats.PreloadStats != nil {
        replStats.PreloadStats.Export(ch)
    }
    // 3.0+ only
    if replStats.Executor != nil {
        replStats.Executor.Export(ch)
    }
}

In metrics.go This way you probably loose some metrics, but the rest should work.