monitor mongodb from nagios

I was asked to find a way to alert if the PRIMARY server in our mongodb replicaset changed… I found check_mongodb.py from the mongodb documentation and google search.

Grab the check_mongodb.py file from https://github.com/mzupan/nagios-plugin-mongodb. Also here are the install instructions and the nagios configurations.

I installed the check_mongodb.py file and tried it:

 ./check_mongodb.py
No module named pymongo

so I went and got pymongo from http://api.mongodb.org/python/current/installation.html and then figured out how to get it installed on ubuntu

That took:

 sudo apt-get install build-essential python-dev

I downloaded and zipped up the pymongo source code, transferred it to the nagios server.

 scp remote.server.net:pymongo.zip  .
 unzip pymongo.zip
 cd pymongo/
 python setup.py install

Re-running check_mongo.py with pymongo installed gets a more coherent result:

root@server:/etc/nagios-plugins# ./check_mongodb.py                        CRITICAL - General MongoDB Error: could not connect to 127.0.0.1:27017: [Errno 111] Connection refused
root@server:/etc/nagios-plugins#

Testing on the command line to see if I can get the data on the PRIMARY I want to check:

 ./check_mongodb.py -H 192.168.1.10 -A replica_primary -P 27017 -W 0 -C 1 -r mongodb

The first time there is an error as NONE is the stored result and your new primary ov erwrites that…
Then you get:

OK - Primary server has not changed

Then set up the command.cfg sections for the new plugin:

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_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$
}

and add the check I want to use for the servers I want to check:

define service {
      use                     generic-service
      hostgroup_name          Mongo Servers
      service_description     MongoDB Replicaset Master Monitor: your-replicaset
      check_command           check_mongodb_replicaset!replica_primary!27017!0!1!your-replicaset
}

And we get an alert if the PRIMARY server of the replicaset changed unexpectedly…

 

—doug