Repository
Munin (contrib)
Last change
2021-03-16
Graph Categories
Keywords
Language
Python (3.x)

mongodb_docs

Name

mongodb_docs - MongoDB docs Plugin

Applicable Systems

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

Configuration

Default for host is 127.0.0.1 and port 27017 and will work without being defined:

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

or

[mongodb_docs]
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

#!/usr/bin/env python3
"""
=head1 NAME

mongodb_docs - MongoDB docs Plugin

=head1 APPLICABLE SYSTEMS

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

=head1 CONFIGURATION

Default for host is 127.0.0.1 and port 27017 and will work without being defined:

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

or

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

=head1 AUTHOR

Original script there : https://github.com/comerford/mongo-munin

Updated by Alban Espie-Guillon <alban.espie@alterway.fr>

=cut
"""

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["metrics"]["document"].items():
        print(str(k) + ".value " + str(v))


def doConfig():
    print("""
graph_title MongoDB documents
graph_args --base 1000 -l 0
graph_vlabel documents
graph_category %s
""" % os.getenv('graph_category', 'db'))

    for k in getServerStatus()["metrics"]["document"]:
        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()