§2024-05-22

¶To monitor a MongoDB replica set with Zabbix, you'll need to set up a few components and configure them correctly. Here's a step-by-step guide: Prerequisites

Step-by-Step Guide

  1. Install MongoDB Monitoring Scripts

There are several scripts and templates available for monitoring MongoDB with Zabbix. One popular choice is the mongodb-zabbix script available on GitHub.

Download the monitoring scripts from the [mongodb-zabbix GitHub repository](https://github.com/omni-lchen/zabbix-mongodb).

bash

cd /opt git clone https://github.com/vvondra/mongodb-zabbix.git

  1. Configure the MongoDB User for Monitoring

Create a MongoDB user with the necessary permissions for monitoring.

bash

use admin db.createUser({ user: "zabbix", pwd: "zabbixpassword", roles: [ { role: "readAnyDatabase", db: "admin" }, { role: "clusterMonitor", db: "admin" } ] })

  1. Configure Zabbix Agent on MongoDB Servers

Ensure that the Zabbix agent is installed on all MongoDB replica set members.

Edit the Zabbix agent configuration file (zabbix_agentd.conf) to include the MongoDB monitoring script.

ini

UserParameter=mongo.ping[],/opt/mongodb-zabbix/scripts/mongo_ping.sh $1 UserParameter=mongo.replicationLag[],/opt/mongodb-zabbix/scripts/mongo_replication_lag.sh $1 UserParameter=mongo.replicationState[*],/opt/mongodb-zabbix/scripts/mongo_replication_state.sh $1

Add other parameters as needed

Restart the Zabbix agent service.

bash

sudo systemctl restart zabbix-agent

  1. Import Zabbix Template

    Download the Zabbix template from the mongodb-zabbix repository. Log in to the Zabbix web interface. Go to Configuration > Templates. Click on Import in the top right corner. Choose the template file you downloaded and import it.

  2. Link the Template to MongoDB Hosts

    Go to Configuration > Hosts. Select the MongoDB host you want to monitor. Click on Templates. Link the MongoDB template you imported. Save the configuration.

  3. Verify Data Collection

    After configuring the template, wait for some time to see if data is being collected. Go to Monitoring > Latest data. Check the metrics to see if MongoDB-related data is appearing.

Additional Monitoring and Alerts

To monitor specific metrics and set up alerts for your MongoDB replica set:

Monitoring Replica Set Status: The scripts include checks for replica set status, replication lag, and member state. You can configure triggers in Zabbix to alert you based on these metrics.

Custom Checks: Modify or add custom scripts to monitor additional MongoDB parameters as needed.

Example: Setting up a Trigger

To set up a trigger for MongoDB replication lag:

Go to Configuration > Hosts.

Select the MongoDB host.

Go to the Triggers tab.

Click on Create trigger.

Define the trigger expression, for example:

plaintext

{Template_MongoDB:mongo.replicationLag[localhost].last()} > 1000

Set the severity and other options.

Click on Add to create the trigger.

This guide should help you set up monitoring for a MongoDB replica set in Zabbix. You can customize the monitoring scripts and Zabbix templates further to suit your specific requirements. ChatGPT can make mistakes. Check important info.