Infrastructure at your Service

The question comes frequently: How can I monitor my MongoDB cluster (ReplicaSet or Sharded) in a production environment? To answer this question we need first to get an overview of all potential monitoring solutions, and then compare them one by one. That’s exactly what we gonna do in this blog post series of MongoDB Monitoring solutions. Actually, a few monitoring solutions exist for MongoDB:

  • Ops Manager/Cloud Manager
  • Nagios
  • Prometheus/Grafana
  • MongoDB Utilities (mongostat, mongotop)

For the moment, we are going to leave OPS Manager aside because it can only be used with the MongoDB Enterprise Edition. The power of Nagios is its ability to integrate custom plugins developed by the community. One of the most popular MongoDB plugins for Nagios is the check_mongodb.py available here: https://github.com/mzupan/nagios-plugin-mongodb/

In this blog post, we will describe steps by steps on how to install, configure, and monitor MongoDB instances using the check_mongodb.py plugin.

Prerequisites:

  • MongoDB is installed, up and running
  • Nagios server is installed

For Nagios installation please refer to the official documentation: https://library.nagios.com/library/products/nagios-core/documentation/nagios-core-installing-on-centos- 7/

Create MongoDB user

You must be logged as an admin user in the admin database:

[[email protected] ~]$ /opt/nosql/admin/mongodb/products/mongodb-linux-x86_64-amazon-4.4.1/bin/mongo --host 172.21.9.106 --port 27001 -u admin -p
MongoDB shell version v4.4.1
Enter password:
connecting to: mongodb://172.21.9.106:27001/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1fa616d7-5d2b-4952-9bab-e75bb6c3f169") }
MongoDB server version: 4.4.1
---

The first step is to create the monitoring user as follow:

mongo-replica:PRIMARY> db.getSiblingDB("admin").createUser( {
user: "monitoring",
pwd: "test",
roles: [ { role: "clusterMonitor", db: "admin" } ]
} )

Download and Install check_mongodb Nagios Plugin

Download the full project from here: https://github.com/mzupan/nagios-plugin-mongodb

[[email protected] ~]$ wget --no-check-certificate https://github.com/mzupan/nagios-plugin-mongodb/archive/master.zip
[[email protected] ~]$ unzip /usr/save/nagios-plugin-mongodb-master.zip
[[email protected] ~]$ mv /usr/save/nagios-plugin-mongodb-master /usr/save/nagios-plugin- mongodb
[[email protected] ~]$ cd /usr/local/nagios/libexec/
[[email protected] ~]$ chown -R nagios:nagios nagios-plugin-mongodb/

Test the plugin, to verify it has been installed properly:

[[email protected] ~]$ /usr/local/nagios/libexec/nagios-plugin-mongodb/check_mongodb.py
No module named pymongo
At this stage, the plugin is installed but needs a dependency: pymongo.

Download and Install PyMongo Driver

Method 1: Download and install PyMongo manually.
Download the MongoDB Python driver from here: https://github.com/mongodb/mongo-python-driver.

[[email protected] ~]$ cd ~
[[email protected] ~]$ wget --no-check-certificate https://github.com/mongodb/mongo-python- driver/archive/master.zip
[[email protected] ~]$ unzip mongo-python-driver-master.zip $ cd mongo-python-driver-master
Install pymongo by running the setup.py as shown below :
# python setup.py install ..
..
..
Installed /usr/lib/python2.4/site-packages/pymongo-2.6-py2.4-linux- i686.egg
Processing dependencies for pymongo==2.6
Finished processing dependencies for pymongo==2.6

Method 2: Install PyMongo driver using pip.
First install Python pip:

[[email protected] ~]$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" $ python get-pip.py
[[email protected] ~]$ pip -V
pip 20.0.2 from /usr/lib/python2.7/site-packages/pip (python 2.7)

Inside the MongoDB plugin directory for Nagios execute the following command:

[[email protected] ~]$ cd /usr/local/nagios/libexec/nagios-plugin-mongodb/ 
[[email protected] ~]$ pip install -r requirements

Test MongoDB Nagios Plugin from Command Line

[[email protected] ~]$ /usr/local/nagios/libexec/nagios-plugin-mongodb/check_mongodb.py -H 192.168.0.50
OK - Connection took 1 second
The MongoDB Nagios plugin works properly, we can now start configuring services and commands.

Command definition

Add the following command to the command.cfg file.

[[email protected] ~]$ vi /usr/local/nagios/etc/objects/commands.cfg


################################################################################
#
# MONGODB CHECK COMMANDS
#
################################################################################

define command {
    command_name    check_mongodb
    command_line    $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $HOSTADDRESS$ -A $ARG1$ -P $ARG2$ -W $ARG3$ -C $ARG4$
}

define command {
    command_name    check_mongodb_database
    command_line    $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $HOSTADDRESS$ -A $ARG1$ -P $ARG2$ -W $ARG3$ -C $ARG4$ -d $ARG5$
}

define command {
    command_name    check_mongodb_collection
    command_line    $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $HOSTADDRESS$ -A $ARG1$ -P $ARG2$ -W $ARG3$ -C $ARG4$ -d $ARG5$ -c $ARG6$
}

define command {
    command_name    check_mongodb_replicaset
    command_line    $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $HOSTADDRESS$ -A $ARG1$ -P $ARG2$ -W $ARG3$ -C $ARG4$ -r $ARG5$
}

define command {
    command_name    check_mongodb_query
    command_line    $USER1$/nagios-plugin-mongodb/check_mongodb.py -H $HOSTADDRESS$ -A $ARG1$ -P $ARG2$ -W $ARG3$ -C $ARG4$ -q $ARG5$
}

Nagios Service Definition for MongoDB Commands

Create a service definition and place it under the /usr/local/nagios/etc directory.

[[email protected] etc]$ vi /usr/local/nagios/etc/mongodb-prod-server.cfg

define host{
        use           linux-server
        host_name     mdb01
        alias         mdb01
        address       192.168.56.105
}
define host{
        use           linux-server
        host_name     mdb02
        alias         mdb02
        address       192.168.56.106
}
define host{
        use           linux-server
        host_name     mdb03
        alias         mdb03
        address       192.168.56.107
}
define hostgroup{
        hostgroup_name          Mongo Servers
        alias                   Mongo Servers
        members                 mdb01,mdb02,mdb03
}
######################################################################## 
######## SERVICES DEFINITIONS ########
########################################################################
define service {
    use                 generic-service
    hostgroup_name          Mongo Servers
    service_description     Mongo Connect Check
    check_command           check_mongodb!connect!27017!2!4
}
define service {
    use                 generic-service
    hostgroup_name          Mongo Servers
    service_description     Mongo Free Connections
    check_command           check_mongodb!connections!27017!70!80
}
....
## All services definition can be found here: https://github.com/mzupan/nagios-plugin-mongodb

Now add the MongoDB configuration file in the central Nagios configuration file:

[[email protected] etc]$ vi /usr/local/nagios/etc/nagios.cfg
# Definitions for Monitoring MongoDB Hosts and Services
cfg_dir=/usr/local/nagios/etc/objects/mongodb-prod-server

Save and exit

Test Service Check before restarting Nagios Service

[[email protected] etc]$ /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg Running pre-flight check on configuration data...
...
Total Warnings: 0 Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check

Then, restart the Nagios service:

[[email protected] etc]$ systemctl restart nagios && systemctl status nagios

Connect to the Nagios web interface a see all services running:

[[email protected] etc]$ open http://IP/nagios

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mehdi Bada
Mehdi Bada

Senior Consultant