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

mongo_mem

Name

mongo_mem - MongoDB memory 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:

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

or

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

mongo_mem - MongoDB memory 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:

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

or

    [mongodb_mem]
    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 ok(s):
    return s == "resident" or s == "virtual" or s == "mapped"

def doData():
    for k,v in getServerStatus()["mem"].items():
        if ok(k):
            print( str(k) + ".value " + str(v * 1024 * 1024) )


def doConfig():
    print("""
graph_title MongoDB memory usage
graph_args --base 1024 -l 0 --vertical-label Bytes
graph_category %s
""" % os.getenv('graph_category', 'db'))

    for k in getServerStatus()["mem"]:
        if ok( k ):
            print(k + ".label " + k)
            print(k + ".draw LINE1")


if __name__ == "__main__":

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