usr bin env python from collections import defaultdict import pymongo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
from collections import defaultdict
import pymongo
from weblib.metric import in_unit
def main():
connection = pymongo.MongoClient()
stat = defaultdict(lambda: {
'storage': 0,
'index': 0,
})
for db_name in connection.database_names():
db = connection[db_name]
info = db.command('dbstats')
stat[db_name]['storage'] = info['storageSize']
stat[db_name]['index'] = info['indexSize']
for db_name, item in sorted(stat.items(), key=lambda x: x[1]['storage'], reverse=False):
print('%s: %s Gb [%s Gb]' % (
db_name,
in_unit(item['storage'], 'gb'),
in_unit(item['index'], 'gb'),
))
print('Total:')
print(' storage: %s Gb' % in_unit(sum(x['storage'] for x in stat.values()), 'gb'))
print(' index: %s Gb' % in_unit(sum(x['index'] for x in stat.values()), 'gb'))
if __name__ == '__main__':
main()