Lesson 7: Self-Managed Monitoring / Learn
Code Summary: Self-Managed Monitoring
Review the following code, which demonstrates how to configure the Percona MongoDB Exporter tool as a Prometheus target, so that it can gather metrics from a MongoDB deployment and make them available to display with Grafana.
Install Percona MongoDB Exporter on Ubuntu Linux
Follow these steps to install the Percona MongoDB Exporter tool.
- Download version 0.39.0 of the Percona MongoDB exporter:
- Extract the downloaded tarball:
- Move the
mongodb_exporterbinary to the/usr/local/bin/directory:
wget
https://github.com/percona/mongodb_exporter/releases/download/v0.39.0/mongodb_exporter-0.39.0.linux-amd64.tar.gz
tar xvzf mongodb_exporter-0.39.0.linux-amd64.tar.gz
sudo mv mongodb_exporter-0.39.0.linux-amd64/mongodb_exporter /usr/local/bin/Create a New User
Follow these steps to create a user with sufficient privilege so that Percona MongoDB Exporter can read metrics from the MongoDB deployment.
- Connect to your local MongoDB instance:
- Switch to the
admindatabase within yourmongoshshell session: - Create a new database user (
test) with theclusterMonitorrole: - Exit the
mongoshshell session:
mongosh
use admin
db.createUser({user: "test",pwd: "testing",roles: [{ role: "clusterMonitor", db: "admin" },{ role: "read", db: "local" }]})
exitCreate a Service for Percona MongoDB Exporter
Follow these steps to create a new service for the Percona MongoDB exporter and have it run as the prometheus user.
- Create a new service file for the
mongodb_exporter: - Add the following contents to the new service file:
- Save and exit.
- Restart the system daemon to reload the unit files:
- Start the
mongodb_exportersystem service: - Enable the
mongodb_exportersystem service so that it automatically starts on boot: - Confirm that the
mongodb_exportersystem service is running and enabled by reviewing it’s status: - Confirm that MongoDB metrics are being collected and available via the
mongodb_exporter /metricsendpoint:
sudo nano /lib/systemd/system/mongodb_exporter.service
[Unit]
Description=MongoDB Exporter
User=prometheus
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/mongodb_exporter \
--collect-all \
--mongodb.uri=mongodb://test:testing@localhost:27017
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start mongodb_exporter
sudo systemctl enable mongodb_exporter
sudo systemctl status --full mongodb_exporter
curl http://localhost:9216/metricsConfigure Percona MongoDB Exporter as a Prometheus Target
- Open the Prometheus configuration file:
- Append the below scrape configuration snippet to the
scrape_configssection of the template Prometheus configuration: - Restart the
prometheussystem service to apply the configuration change: - Use the Prometheus server API to confirm that the local MongoDB exporter target is present and healthy:
sudo nano /etc/prometheus/prometheus.yml
...
scrape_configs:
...
- job_name: 'mongodb_exporter'
static_configs:
- targets: ['localhost:9216']
...
sudo systemctl restart prometheus
curl http://localhost:9090/api/v1/targets | jq --raw-output '.data.activeTargets[] | .scrapeUrl + " " + .health'
Now you’re ready to start creating Grafana visualizations using the Prometheus data source!