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.

  1. Download version 0.39.0 of the Percona MongoDB exporter:
  2. wget
    https://github.com/percona/mongodb_exporter/releases/download/v0.39.0/mongodb_exporter-0.39.0.linux-amd64.tar.gz
    
    

  3. Extract the downloaded tarball:
  4. tar xvzf mongodb_exporter-0.39.0.linux-amd64.tar.gz

  5. Move the mongodb_exporter binary to the /usr/local/bin/ directory:
  6. 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.

  1. Connect to your local MongoDB instance:
  2. mongosh

  3. Switch to the admin database within your mongosh shell session:
  4. use admin

  5. Create a new database user (test) with the clusterMonitor role:
  6. db.createUser({user: "test",pwd: "testing",roles: [{ role: "clusterMonitor", db: "admin" },{ role: "read", db: "local" }]})
    
    

  7. Exit the mongosh shell session:
  8. exit

Create 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.

  1. Create a new service file for the mongodb_exporter:
  2. sudo nano /lib/systemd/system/mongodb_exporter.service

  3. Add the following contents to the new service file:
  4. [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
    

  5. Save and exit.
  6. Restart the system daemon to reload the unit files:
  7. sudo systemctl daemon-reload

  8. Start the mongodb_exporter system service:
  9. sudo systemctl start mongodb_exporter

  10. Enable the mongodb_exporter system service so that it automatically starts on boot:
  11. sudo systemctl enable mongodb_exporter

  12. Confirm that the mongodb_exporter system service is running and enabled by reviewing it’s status:
  13. sudo systemctl status --full mongodb_exporter

  14. Confirm that MongoDB metrics are being collected and available via the mongodb_exporter /metrics endpoint:
  15. curl http://localhost:9216/metrics

Configure Percona MongoDB Exporter as a Prometheus Target

  1. Open the Prometheus configuration file:
  2. sudo nano /etc/prometheus/prometheus.yml

  3. Append the below scrape configuration snippet to the scrape_configs section of the template Prometheus configuration:
  4. ...
    scrape_configs:
    ...
      - job_name: 'mongodb_exporter'
        static_configs:
          - targets: ['localhost:9216']
    ...
    

  5. Restart the prometheus system service to apply the configuration change:
  6. sudo systemctl restart prometheus

  7. Use the Prometheus server API to confirm that the local MongoDB exporter target is present and healthy:
  8. 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!