Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mongodb / mongo_collection_ @ 7fd554f0

Historique | Voir | Annoter | Télécharger (6,75 ko)

1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
# vim: set sts=4 sw=4 encoding=utf-8
4

    
5
# Based on plugins from https://github.com/pcdummy/mongomon
6
# Copyright (c) 2015, Tomas Zvala
7
# Copyright (c) 2010, Rene Jochum
8
# All rights reserved.
9
#
10
# Redistribution and use in source and binary forms, with or without
11
# modification, are permitted provided that the following conditions are met:
12
#    * Redistributions of source code must retain the above copyright
13
#      notice, this list of conditions and the following disclaimer.
14
#    * Redistributions in binary form must reproduce the above copyright
15
#      notice, this list of conditions and the following disclaimer in the
16
#      documentation and/or other materials provided with the distribution.
17
#    * Neither the name of the Rene Jochum nor the
18
#      names of its contributors may be used to endorse or promote products
19
#      derived from this software without specific prior written permission.
20
#
21
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
25
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31

    
32
# #%# family=auto
33
# #%# capabilities=suggest autoconf
34

    
35

    
36
import pymongo
37
from operator import itemgetter
38

    
39
settings_host = '127.0.0.1'
40
settings_port = 27017
41
# mongodb_uri will override host and port
42
settings_mongodb_uri = ''
43
settings_db = 'admin'
44
settings_user = ''
45
settings_password = ''
46
settings_ignoredb = {}
47

    
48
typeIndex = {}
49
typeIndex['collcount'] = {}
50
typeIndex['collcount']['index'] = 'count'
51
typeIndex['collcount']['title'] = 'per collection document count'
52
typeIndex['collcount']['yaxis'] = 'documents'
53
typeIndex['collcount']['base'] = '1000'
54
typeIndex['collcount']['scale'] = '--logarithmic -l1'
55
typeIndex['collcount']['category'] = 'db'
56

    
57
typeIndex['collsize'] = {}
58
typeIndex['collsize']['index'] = 'size'
59
typeIndex['collsize']['title'] = 'per collection data size'
60
typeIndex['collsize']['yaxis'] = 'Byte'
61
typeIndex['collsize']['base'] = '1024'
62
typeIndex['collsize']['scale'] = '--logarithmic -l1 --units=si'
63
typeIndex['collsize']['category'] = 'db'
64

    
65
typeIndex['avgsize'] = {}
66
typeIndex['avgsize']['index'] = 'avgObjSize'
67
typeIndex['avgsize']['title'] = 'average object size'
68
typeIndex['avgsize']['yaxis'] = 'Byte'
69
typeIndex['avgsize']['base'] = '1024'
70
typeIndex['avgsize']['scale'] = '--logarithmic --units=si'
71
typeIndex['avgsize']['category'] = 'db'
72

    
73
typeIndex['storage'] = {}
74
typeIndex['storage']['index'] = 'storageSize'
75
typeIndex['storage']['title'] = 'per collection storage size'
76
typeIndex['storage']['yaxis'] = 'Byte'
77
typeIndex['storage']['base'] = '1024'
78
typeIndex['storage']['scale'] = '--logarithmic -l1 --units=si'
79
typeIndex['storage']['category'] = 'db'
80

    
81
typeIndex['indexsize'] = {}
82
typeIndex['indexsize']['index'] = 'totalIndexSize'
83
typeIndex['indexsize']['title'] = 'per collection index size'
84
typeIndex['indexsize']['yaxis'] = 'Byte'
85
typeIndex['indexsize']['base'] = '1024'
86
typeIndex['indexsize']['scale'] = '--logarithmic -l 1 --units=si'
87
typeIndex['indexsize']['category'] = 'db'
88

    
89

    
90
def getCollstats(graphtype):
91
    if settings_mongodb_uri:
92
        con = pymongo.MongoClient(settings_mongodb_uri)
93
    else:
94
        con = pymongo.MongoClient(settings_host, int(settings_port))
95

    
96
    if settings_user:
97
        db = con[settings_db]
98
        db.authenticate(settings_user, settings_password)
99

    
100
    stats_tmp = {}
101
    for dbname in con.list_database_names():
102
        if dbname in settings_ignoredb:
103
            continue
104
        db = con[dbname]
105
        for coll in db.list_collection_names():
106
            if coll.startswith('system.'):
107
                continue
108
            stats = db.command("collstats", coll)
109
            collname = dbname + "_" + coll.replace('.', '_')
110
            if collname not in stats_tmp:
111
                stats_tmp[collname] = {}
112
                stats_tmp[collname]['value'] = 0
113
                stats_tmp[collname]['dbname'] = dbname
114
            if typeIndex[graphtype]['index'] in stats:
115
                stats_tmp[collname]['value'] += int(stats[typeIndex[graphtype]['index']])
116

    
117
    con.close()
118

    
119
    for collname, item in sorted(stats_tmp.items()):
120
        yield ("%s" % collname, item['value'], item['dbname'])
121

    
122

    
123
def doData(base, graphtype):
124
    lastdb = ""
125
    for coll, stats, db in sorted(getCollstats(graphtype), key=itemgetter(2)):
126
        if lastdb != db:
127
            print("multigraph " + base + "_" + graphtype + "_" + db)
128
            lastdb = db
129
        print("%s_%s.value %s" % (graphtype, coll, stats))
130

    
131

    
132
def doConfig(base, graphtype):
133

    
134
    lastdb = ""
135
    for k, v, d in sorted(getCollstats(graphtype), key=itemgetter(2)):
136
        if lastdb != d:
137
            print("multigraph " + base + "_" + graphtype + "_" + d)
138
            lastdb = d
139
            print("graph_title MongoDB " + typeIndex[graphtype]['title'] + " for database " + d)
140
            print("graph_args --base " + typeIndex[graphtype]['base'] + " " + typeIndex[graphtype]['scale'])
141
            print("graph_vlabel " + typeIndex[graphtype]['yaxis'])
142
            print("graph_category db")
143
        print("%s_%s.label %s" % (graphtype, k, k))
144
        print("%s_%s.min 0" % (graphtype, k))
145
        print("%s_%s.draw LINE1" % (graphtype, k))
146

    
147

    
148
def doSuggest():
149
    print("keys")
150
    for k in typeIndex.keys():
151
        print(k)
152

    
153

    
154
if __name__ == "__main__":
155
    from sys import argv, exit
156
    from os import environ, path
157
    import re
158

    
159
    # Could be done by a for loop
160
    # but i think if's are faster
161
    if 'host' in environ:
162
        settings_host = environ['host']
163
    if 'port' in environ:
164
        settings_port = environ['port']
165
    if 'db' in environ:
166
        settings_db = environ['db']
167
    if 'username' in environ:
168
        settings_user = environ['username']
169
    if 'password' in environ:
170
        settings_password = environ['password']
171
    if 'ignoredb' in environ:
172
        settings_ignoredb = environ['ignoredb'].split(',')
173
    if 'MONGO_DB_URI' in environ:
174
        settings_mongodb_uri = environ['MONGO_DB_URI']
175
    m = re.search('^(.*)_([a-zA-Z0-9]*)$', path.basename(argv[0]))
176
    if len(argv) < 2:
177
        doData(m.group(1), m.group(2))
178
    elif argv[1] == "config":
179
        doConfig(m.group(1), m.group(2))
180
    elif argv[1] == "autoconf":
181
        print("yes")
182
    elif argv[1] == "suggest":
183
        doSuggest()
184
    else:
185
        print("invalid argument")
186
        exit(1)