NAME MongoDB ops Plugin

APPLICABLE SYSTEMS

    MongoDB 3.X and 4.X with pymongo installed.

CONFIGURATION

    munin-node.conf
    defaults for host is 127.0.0.1 and port 27017
    and will work without being defined :

    [mongodb_ops]
    env.host 127.0.0.1
    env.port 27017
    env.username user
    env.password P@55w0rd
    env.db dbname

    or

    [mongodb_ops]
    env.MONGO_DB_URI mongodb://user:password@host:port/dbname

AUTHOR

    Original script there : https://github.com/comerford/mongo-munin
    Updated by Alban Espie-Guillon <alban.espie@alterway.fr>
"""
import sys
import os
import pymongo

def getServerStatus(): if 'MONGO_DB_URI' in os.environ: c = pymongo.MongoClient(os.environ['MONGO_DB_URI'])

    elif 'username' and 'password' in os.environ:
      host = os.environ.get('host', '127.0.0.1')
      port = os.environ.get('port', 27017)
      username = os.environ.get('username', '')
      password = os.environ.get('password', '')
      c = pymongo.MongoClient(host, int(port))
      if username:
        cAuth = c['admin']
        cAuth.authenticate(username, password)

    else:
      c = pymongo.MongoClient()

    return c.admin.command('serverStatus', workingSet=True)

def doData(): ss = getServerStatus() for k,v in ss["opcounters"].items(): print( str(k) + ".value " + str(v) )

def doConfig(): print(""" graph_title MongoDB ops graph_args --base 1000 -l 0 graph_vlabel ops / ${graph_period} graph_category db graph_total total """)

    for k in getServerStatus()["opcounters"]:
        print(k + ".label " + k)
        print(k + ".min 0")
        print(k + ".type COUNTER")
        print(k + ".max 500000")
        print(k + ".draw LINE1")

if __name__ == "__main__":

    if len(sys.argv) > 1 and sys.argv[1] == "config":
        doConfig()
    else:
        doData()