Repository
Munin (contrib)
Last change
2021-03-31
Graph Categories
Capabilities
Keywords
Language
Python (3.x)
License
GPL-2.0-only
Authors

mongodb_multi

Name

mongodb_multi - monitor MongoDB connections, documents, memory and operations

Applicable Systems

Systems with MongoDB installed. Tested up to MongoDB version 4.4.

Configuration

Defaults to mongodb://localhost, but can be configured:

[mongodb_multi]
env.uri mongodb://user:password@host:port/dbname?parameters

Author

Kim B. Heino b@bbbs.net

This is based heavily on non-multigraph MongoDB plugins.

License

GPLv2

#!/usr/bin/env python3
# pylint: disable=invalid-name
# pylint: enable=invalid-name

"""Munin plugin to monitor MongoDB status.

=head1 NAME

mongodb_multi - monitor MongoDB connections, documents, memory and operations

=head1 APPLICABLE SYSTEMS

Systems with MongoDB installed. Tested up to MongoDB version 4.4.

=head1 CONFIGURATION

Defaults to mongodb://localhost, but can be configured:

    [mongodb_multi]
    env.uri mongodb://user:password@host:port/dbname?parameters

=head1 AUTHOR

Kim B. Heino <b@bbbs.net>

This is based heavily on non-multigraph MongoDB plugins.

=head1 LICENSE

GPLv2
=cut
"""

import os
import sys
import pymongo


URI = os.environ.get('uri', 'mongodb://localhost')


def server_status():
    """Get Mongo status."""
    conn = pymongo.MongoClient(URI)
    return conn.admin.command('serverStatus', workingSet=True)


def print_data(status):
    """Plugin values."""
    # conn
    print('multigraph mongo_conn')
    print('connections.value {}'.format(status['connections']['current']))

    # docs
    print('multigraph mongo_docs')
    for key, value in status['metrics']['document'].items():
        print('{}.value {}'.format(key, value))

    # mem
    print('multigraph mongo_mem')
    for key, value in status['mem'].items():
        if key in ('resident', 'virtual', 'mapped'):
            print('{}.value {}'.format(key, value * 1024 * 1024))

    # ops
    print('multigraph mongo_ops')
    for key, value in status['opcounters'].items():
        print('{}.value {}'.format(key, value))


def print_config(status):
    """Plugin config."""
    # conn
    print('multigraph mongo_conn')
    print('graph_title MongoDB connections')
    print('graph_args --base 1000 -l 0')
    print('graph_vlabel Connections')
    print('graph_category db')
    print('connections.label connections')

    # docs
    print('multigraph mongo_docs')
    print('graph_title MongoDB documents')
    print('graph_args --base 1000 -l 0')
    print('graph_vlabel Documents / ${graph_period}')
    print('graph_category db')
    for key in status['metrics']['document']:
        print('{0}.label {0}'.format(key))
        print('{}.type DERIVE'.format(key))
        print('{}.min 0'.format(key))

    # mem
    print('multigraph mongo_mem')
    print('graph_title MongoDB memory usage')
    print('graph_args --base 1024 -l 0')
    print('graph_vlabel Bytes')
    print('graph_category db')
    for key in status['mem']:
        if key in ('resident', 'virtual', 'mapped'):
            print('{0}.label {0}'.format(key))

    # ops
    print('multigraph mongo_ops')
    print('graph_title MongoDB operations')
    print('graph_args --base 1000 -l 0')
    print('graph_vlabel ops / ${graph_period}')
    print('graph_category db')
    print('graph_total total')
    for key in status['opcounters']:
        print('{0}.label {0}'.format(key))
        print('{}.type DERIVE'.format(key))
        print('{}.min 0'.format(key))

    if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1':
        print_data(status)


if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == 'config':
        print_config(server_status())
    else:
        print_data(server_status())