root / plugins / mongodb / mongodb_multi @ b1251d0c
Historique | Voir | Annoter | Télécharger (3,12 ko)
| 1 | b1251d0c | Kim B. Heino | #!/usr/bin/env python3 |
|---|---|---|---|
| 2 | f2f292f1 | Kim B. Heino | # pylint: disable=invalid-name |
| 3 | # pylint: enable=invalid-name |
||
| 4 | |||
| 5 | """Munin plugin to monitor MongoDB status. |
||
| 6 | |||
| 7 | b1251d0c | Kim B. Heino | =head1 NAME |
| 8 | f2f292f1 | Kim B. Heino | |
| 9 | b1251d0c | Kim B. Heino | mongodb_multi - monitor MongoDB connections, documents, memory and operations |
| 10 | |||
| 11 | =head1 APPLICABLE SYSTEMS |
||
| 12 | |||
| 13 | Systems with MongoDB installed. Tested up to MongoDB version 4.4. |
||
| 14 | |||
| 15 | =head1 CONFIGURATION |
||
| 16 | |||
| 17 | Defaults to mongodb://localhost, but can be configured: |
||
| 18 | |||
| 19 | [mongodb_multi] |
||
| 20 | env.uri mongodb://user:password@host:port/dbname?parameters |
||
| 21 | |||
| 22 | =head1 AUTHOR |
||
| 23 | |||
| 24 | Kim B. Heino <b@bbbs.net> |
||
| 25 | |||
| 26 | This is based heavily on non-multigraph MongoDB plugins. |
||
| 27 | |||
| 28 | =head1 LICENSE |
||
| 29 | |||
| 30 | GPLv2 |
||
| 31 | =cut |
||
| 32 | f2f292f1 | Kim B. Heino | """ |
| 33 | |||
| 34 | import os |
||
| 35 | import sys |
||
| 36 | import pymongo |
||
| 37 | |||
| 38 | |||
| 39 | URI = os.environ.get('uri', 'mongodb://localhost')
|
||
| 40 | |||
| 41 | |||
| 42 | def server_status(): |
||
| 43 | """Get Mongo status.""" |
||
| 44 | conn = pymongo.MongoClient(URI) |
||
| 45 | return conn.admin.command('serverStatus', workingSet=True)
|
||
| 46 | |||
| 47 | |||
| 48 | def print_data(status): |
||
| 49 | """Plugin values.""" |
||
| 50 | # conn |
||
| 51 | print('multigraph mongo_conn')
|
||
| 52 | print('connections.value {}'.format(status['connections']['current']))
|
||
| 53 | |||
| 54 | # docs |
||
| 55 | print('multigraph mongo_docs')
|
||
| 56 | for key, value in status['metrics']['document'].items(): |
||
| 57 | print('{}.value {}'.format(key, value))
|
||
| 58 | |||
| 59 | # mem |
||
| 60 | print('multigraph mongo_mem')
|
||
| 61 | for key, value in status['mem'].items(): |
||
| 62 | if key in ('resident', 'virtual', 'mapped'):
|
||
| 63 | print('{}.value {}'.format(key, value * 1024 * 1024))
|
||
| 64 | |||
| 65 | # ops |
||
| 66 | print('multigraph mongo_ops')
|
||
| 67 | for key, value in status['opcounters'].items(): |
||
| 68 | print('{}.value {}'.format(key, value))
|
||
| 69 | |||
| 70 | |||
| 71 | def print_config(status): |
||
| 72 | """Plugin config.""" |
||
| 73 | # conn |
||
| 74 | print('multigraph mongo_conn')
|
||
| 75 | print('graph_title MongoDB connections')
|
||
| 76 | print('graph_args --base 1000 -l 0')
|
||
| 77 | print('graph_vlabel Connections')
|
||
| 78 | print('graph_category db')
|
||
| 79 | print('connections.label connections')
|
||
| 80 | |||
| 81 | # docs |
||
| 82 | print('multigraph mongo_docs')
|
||
| 83 | print('graph_title MongoDB documents')
|
||
| 84 | print('graph_args --base 1000 -l 0')
|
||
| 85 | print('graph_vlabel Documents / ${graph_period}')
|
||
| 86 | print('graph_category db')
|
||
| 87 | for key in status['metrics']['document']: |
||
| 88 | print('{0}.label {0}'.format(key))
|
||
| 89 | print('{}.type DERIVE'.format(key))
|
||
| 90 | print('{}.min 0'.format(key))
|
||
| 91 | |||
| 92 | # mem |
||
| 93 | print('multigraph mongo_mem')
|
||
| 94 | print('graph_title MongoDB memory usage')
|
||
| 95 | print('graph_args --base 1024 -l 0')
|
||
| 96 | print('graph_vlabel Bytes')
|
||
| 97 | print('graph_category db')
|
||
| 98 | for key in status['mem']: |
||
| 99 | if key in ('resident', 'virtual', 'mapped'):
|
||
| 100 | print('{0}.label {0}'.format(key))
|
||
| 101 | |||
| 102 | # ops |
||
| 103 | print('multigraph mongo_ops')
|
||
| 104 | print('graph_title MongoDB operations')
|
||
| 105 | print('graph_args --base 1000 -l 0')
|
||
| 106 | print('graph_vlabel ops / ${graph_period}')
|
||
| 107 | print('graph_category db')
|
||
| 108 | print('graph_total total')
|
||
| 109 | for key in status['opcounters']: |
||
| 110 | print('{0}.label {0}'.format(key))
|
||
| 111 | print('{}.type DERIVE'.format(key))
|
||
| 112 | print('{}.min 0'.format(key))
|
||
| 113 | |||
| 114 | if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1':
|
||
| 115 | print_data(status) |
||
| 116 | |||
| 117 | |||
| 118 | if __name__ == '__main__': |
||
| 119 | if len(sys.argv) > 1 and sys.argv[1] == 'config': |
||
| 120 | print_config(server_status()) |
||
| 121 | else: |
||
| 122 | print_data(server_status()) |
