A simple MongoDB stats exporter with replicaSet support
395

Mgo-exporter is a small stats & metrics open-source exporter for MongoDB with extensible forwarding support to different data sources (currently supporting forwarding to a file or a TCP Logstash listener).
Mgo-exporter is available in many form for the Linux platform, you can install it via:
go get github.com/blippar/mgo-exporter/cmd/mgo-exporter (requires Go 1.6 or newer)To see an example of the data being exported by the mgo-exporter once installed and assuming your MongoDB is accessible via 127.0.0.1:27017, run:
mgo-exporter 127.0.0.1:27017
Running the exporter via Docker is as easy as running it in the command line, container takes the exact same options and environment variable as the binary version.
docker run --restart=always \
--link mongo:mongo \
--link logstash:logstash \
-e MGOEXPORT_REPL="myReplicaSet" \
-e MGOEXPORT_FORWARDER="logstash://logstash:2000" \
blippar/mgo-exporter mongo:27017
By installing mgo-exporter via a system package, it will automatically add a systemd unit file as well as a sysconfig style configuration file. Once installed via one of the following command:
# Centos
sudo yum install mgo-exporter_*.x86_64.rpm
# Debian
sudo dpkg -i mgo-exporter_*.amd64.dev
You will be able to configuration your software by editing /etc/sysconfig/mgo-exporter which contains the following:
MGOEXPORT_MONGODB="localhost:27017"
MGOEXPORT_DATABASES="test local"
MGOEXPORT_REPL=""
MGOEXPORT_FORWARDER="file:///var/log/mgo-exporter-metrics.log"
MGOEXPORT_LOGFILE=/var/log/mgo-exporter.log
Once edited and configured you can run the mgo-exporter as any other daemon for systemd:
# Start the exporter
sudo systemctl start mgo-exporter
# Automatically start with the system
sudo systemctl enable mgo-exporter
Usage: mgo-exporter [--repl REPL] [--forwarder FORWARDER] [--logfile LOGFILE] [--verbose] [--quiet] MONGODB [DATABASE [DATABASE ...]]
Positional arguments:
MONGODB Mongo URI for the node to connect to
DATABASE database name to monitor
Options:
--repl REPL, -r REPL replicaSet name to monitor [env: MGOEXPORT_REPL]
--forwarder FORWARDER, -f FORWARDER
forwarder URI to send messages to [env: MGOEXPORT_FORWARDER] [default: file:///dev/stdout?pretty]
--logfile LOGFILE, -l LOGFILE
file to output logs to [env: MGOEXPORT_LOGFILE] [default: /dev/stderr]
--verbose, -v enable a more verbose logging
--quiet, -q enable quieter logging
--help, -h display this help and exit
By default mgo-exporter will export most of the fields available by running db.serverStatus() on your MongoDB instance, though you can extend what it will export by asking it to also output information about your databases as well as your replicaSet.
For example the following command will export information about the database test as well as information about the myReplSet replicaSet.
mgo-exporter --replicaSet myReplSet 127.0.0.1:27017 test
To see exactly what is exposed at any point, check the Specifications section of this documentation.
When no forwarder is explicitly specified, mgo-exporter will print the gathered information to /dev/stdout, in order for those data to be useful you might want to store them or send them to another entity to be processed.
Right out of the box, you can write those data to a file (here /var/log/mongo-metrics.log) by running the exporter in the following way:
mgo-exporter --forwarder file:///var/log/mongo-metrics.log 127.0.0.1:27017
Another solution is to forward those data to a Logstash instance in order to store them in a Database, this repository contains sample configuration to use the exporter with an ELK (ElasticSearch, Logstash & Kibana) pipeline.
In this example we will use a Logstash instance setup using the configuration available here and this mapping, this configuration get the raw message exported by the mgo-exporter and split all dbStats elements in its own document (for a better indexing on ElasticSearch).
In order to forward your data to it, you will just need to run the following:
mgo-exporter --forwarder logstash://127.0.0.1:2000 127.0.0.1:27017
This is the setup we recommend and thus we have created a sample Kibana (5.4+) configuration for such setup so we can visualize those data cleanly. We created different visualizations as well as a Dashboard that can be imported using the kibana/import.sh script we've created. This script will create everything you need prefixed with the mgo- prefix for easy cleanup.
To see all available forwarder as well as how to configure them, check the Available forwarder section of this documentation.
The mgo-exporter has been created in order to monitor our production MongoDB and thus should be fitting if you are looking for something similar.
Though we recommend the following configuration for such a setup:
systemd)All elements of mgo-exporter can be built via a Makefile at the root of this repository, available rules are the followings:
make exporter: create a binary for your current system under bin/mgo-exportermake static: create a static binary targeted at Linux based system under bin/mgo-exportermake rpm: create a RPM package for x86_64 systemmake deb: create a DEB package for x86_64 systemmake docker: create the blippar/mgo-exporter Docker Container (tagged with the current version)make dist: create a RPM and a DEB package as well as the Docker Containermake generate_schemas: regenerate JSON-Schema files in documentations/schemas/*.jsonmake clean: remove all files that would be created by make exporter rpm debNOTE: system package created from this Makefile comes packages with a static binary of mgo-exporter, a systemd init script as well as sysconfig config file.
A complete docker-compose stack is available in this repository to test how the software would work, by running docker-compose up -d, this stack will create:
mgoExporterSet) with 3 different prioritymgo-exporter setup to export each nodes information to a Logstash pipelineFrom there you can test how this stack would work in a replicaSet configuration by inserting / updating / deleting documents as well as emulate replicaLag. Some manual test used to create the visualizations currently present in this repository are available in the docker/compose/README.md.
| Exported fields | Command | Format | When |
|---|---|---|---|
time | n/a | date-time | Always |
mongo | n/a | schema | Always |
serverStatus | db.serverStatus() | schema | When connected to MONGODB |
[]dbStats | db.stats() | schema | When at least one DATABASE argument is passed and |
| _ | _ | _ | - with --repl REPL: when connected to the primary |
| _ | _ | _ | - without --repl: when connected to MONGODB |
replStatus | rs.Status() | schema | When connected and run with --repl REPL |
nodeReplInfo | generated | schema | When connected and run with --repl REPL |
| Exporter | Configuration | Description |
|---|---|---|
logstash | logstash://{logstash_addr}:{logstash_port} | Send exported data to a Logstash TCP endpoint (compatible with codec=>json) |
file | file://{file_path}[?pretty] | Store exported data in a logfile defined by {file_path} |
| _ | _ | If ?pretty is specified, the json output will be prettified |
Thanks to 0rax for his work on the logo. This logo is licensed under the Creative Commons Attribution 4.0 International License.
Content type
Image
Digest
Size
1.5 MB
Last updated
over 8 years ago
docker pull blippar/mgo-exporter