×


Monitor MongoDB with Nagios XI - How to perform this ?

We can monitor a MongoDB server with Nagios using the MongoDB Server Configuration Wizard.

The information we collect by monitoring MongoDB database will help us to determine when documents are written into the database or if the size of the database is getting too large.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Nagios queries.

In this context, we shall look into how to monitor MongoDB with Nagios XI.


How to Enable Authentication ?

The first step is to enable authentication in MongoDB Server. To enable authentication, follow the steps given below:

1. Initially, start MongoDB without access control.

mongod --port 27017 --dbpath /var/lib/mongodb

2. Then, connect to the instance.

mongo --port 27017 --host xxx.xxx.xxx.xxx

3. Once done, create the user administrator.

From the mongo shell, add a user with the userAdminAnyDatabase role in the admin database. 

Include additional roles as needed for this user. 

For example, the following creates the user myUserAdmin in the admin database with the userAdminAnyDatabase role and the readWriteAnyDatabase role:

use admin
db.createUser(
{
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

Now, Re-start the MongoDB instance with access control using the steps below:

i. Shut down the mongod instance. For example, from the mongo shell, issue the following command: db.adminCommand( { shutdown: 1 } ).

ii. Exit the mongo shell.

iii. Start the mongod with access control enabled.

iv. If we start the mongod from the command line, add the –auth command line option: mongod --auth --port 27017 --dbpath /var/lib/mongodb.

v. If we start the mongod using a configuration file, add the security.authorization configuration file setting: security:

authorization: enabled .


  • Clients that connect to this instance must now authenticate themselves as a MongoDB user. Clients can only perform actions as determined by their assigned roles.


How to Allow remote connections to MongoDB server ?

We should have a user in the admin database that has the role of clusterAdmin. This role is required by MongoDB to access statistics on the MongoDB server.

We will also be required to allow remote connections to MongoDB server.


1. bindIp

To bind to all IPv4 addresses, we can specify the bind IP address of 0.0.0.0. Alternatively, to bind to all IPv4 and IPv6 addresses, we can specify the bind ip address of ::,0.0.0.0 or alternatively, use the new net.bindIpAll setting or the new command-line option –bind_ip_all.

To override and bind to other ip addresses, we can use the net.bindIp configuration file setting or the –bind_ip command-line option to specify a list of hostnames or ip addresses.

For example, the following mongod instance binds to both the localhost and the hostname My-Example-Hostname, which is associated with the IP address 198.51.100.1:

mongod --bind_ip localhost,My-Example-Hostname

In order to connect to this instance, remote clients must specify the hostname or its associated ip address 198.51.100.1:

mongo --host My-Example-Hostname
mongo --host 198.51.100.1


2. Firewall Rules

To explicitly allow traffic to the mongod instance from the application server, run the commands given below:

iptables -A INPUT -s -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

Replace with the IP address of the application server.

The first rule allows all incoming traffic from on port 27017, which allows the application server to connect to the mongod instance. 

The second rule, allows outgoing traffic from the mongod to reach the application server.


How to Monitor a MongoDB Database with Nagios XI ?

The MongoDB Database configuration wizard will be used to set up the service checks for our MongoDB database. In the Nagios XI menu, navigate to Configure > Configuration Wizards and click the MongoDB Database wizard.


Step 1: Provide the address of MongoDB server in the Address field. Change the default port if required. Enter the Username and Password of the account that has clusterAdmin access. We need to enter the database we would like to monitor in the Database field. Click the Next button.


Step 2: Here we can define the monitoring options.

In the first section, make sure the Host Name field is correct, this is the name the host will be given in Nagios XI. The MongoDB Database Metrics section allows us to select which metrics to monitor on our MongoDB Database. Select the checks we wish to perform and what the warning and critical values will be for each one. Once we have selected the checks we want to monitor, click Next to continue.


Step 3: Complete the wizard by choosing the required options and then click on Finish in the final step of the wizard.

Once the wizard applies the configuration, click the View status details for our MongoDB server link to see the new host and services that were created.


How to Monitor a MongoDB Server ?

We can use the MongoDB Server configuration wizard to set up the service checks for our MongoDB server. In the Nagios XI menu, navigate to Configure > Configuration Wizards and click the MongoDB Server wizard.


Step 1: Provide the address of MongoDB server in the Address field. Change the default port if required. Enter the Username and Password of the account that has clusterAdmin access. Click Next to proceed.


Step 2: Here we can define the monitoring options.

In the first section, make sure the Host Name field is correct, this is the name the host will be given in Nagios XI. The MongoDB Server Metrics section allows us to select which metrics to monitor on our MongoDB Server. Select the checks we wish to perform on the server and set warning and critical values for our alert thresholds.

If we use replication, there are some checks we can perform on our replication set such as: status, lag and lag percentage. By default, these are off because not all MongoDB servers are set up with replication. Once we have selected the checks we want to monitor, click Next to continue.


Step 3: Complete the wizard by choosing the required options and then click on Finish in the final step of the wizard.

Once the wizard applies the configuration, click the View status details for our MongoDB server link to see the new host and services that were created.


[Need urgent assistance with Nagios errors? – We're available 24*7. ]


Conclusion

This article covers  how to monitor a MongoDB server with Nagios XI using the MongoDB Server Configuration Wizard in order to monitor the health of your server. The

wizard supplies checks to monitor the following: number of queries per second, memory usage, the number of databases on the server, and percentage of free connections available.


To Connect to MongoDB with the appropriate privileges:

Connect to mongod or mongos with the privileges specified in the Prerequisites section.

The following procedure uses the myUserAdmin created in Enable Access Control:

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

The myUserAdmin has privileges to create roles in the admin as well as other databases.


To Modify Access for an Existing User in MongoDB:

  • You must have the grantRole action on a database to grant a role on that database.
  • You must have the revokeRole action on a database to revoke a role on that database.
  • To view a role's information, you must be either explicitly granted the role or must have the viewRole action on the role's database.