-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollstats.py
141 lines (113 loc) · 5.76 KB
/
collstats.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from pymongo import MongoClient
from pymongo import ReadPreference
import pymongo
import argparse
class Collstats(object):
HOST = 'localhost'
PORT = 27017
client = []
members = []
coll_stats_results = {}
index_stat_results = {}
i = 0
def __init__(self, args_results=None):
if args_results.host is None:
self.host = self.HOST
else:
self.host = args_results.host
if args_results.port is None:
self.port = self.PORT
else:
self.port = int(args_results.port)
self.db_name = args_results.database_name
self.coll_name = args_results.collection_name
if args_results.discover is not None:
self.client.append(MongoClient(self.host, self.port))
discover = self.client[0]["admin"].command("replSetGetStatus", "admin")
for value in discover['members']:
if "SECONDARY" in value['stateStr']:
self.members.append(value['name'])
for m in self.members:
h, p = m.split(":")
h = "192.168.56.101"
self.client.append(MongoClient(host=h, port=int(p), read_preference=ReadPreference.SECONDARY))
else:
self.client.append(MongoClient(self.host, self.port))
'''
Check to ensure db and collection exists
'''
db_names = self.client[0].database_names()
if self.db_name not in db_names:
print("Error no db")
exit(1)
collection_names = self.client[0][self.db_name].collection_names()
if self.coll_name not in collection_names:
print("No colleciton name")
exit(1)
'''
Funtions
'''
def run_collStats(self):
for self.i in range(len(self.client)):
coll_stats = self.client[self.i][self.db_name].command("collstats", self.coll_name)
self.coll_stats_results[self.client[self.i].address] = coll_stats
def print_collStat(self):
for self.i, v in self.coll_stats_results.items():
print(self.i, v)
def print_wiredTiger_info(self):
print('{:30}'.format(" " + self.coll_stats_results[self.client[self.i].address]['ns']),
'{:10}'.format(self.coll_stats_results[self.client[self.i].address]["wiredTiger"]["cache"][
"bytes currently in the cache"]),
'{:10}'.format(self.coll_stats_results[self.client[self.i].address]["wiredTiger"]["cache"][
"tracked dirty bytes in the cache"]),
'{:10}'.format(
self.coll_stats_results[self.client[self.i].address]["wiredTiger"]["cache"]["pages read into cache"]),
'{:10}'.format(self.coll_stats_results[self.client[self.i].address]["wiredTiger"]["cache"][
"pages written from cache"]),
'{:10}'.format(self.coll_stats_results[self.client[self.i].address]["wiredTiger"]["cache"][
"pages requested from the cache"]))
def print_index_info(self):
self.index_stat_results[self.client[self.i].address] = self.coll_stats_results[self.client[self.i].address][
"indexDetails"]
for key in self.index_stat_results[self.client[self.i].address]:
indx_n = key
pipeline = [{"$indexStats": {}}]
query = pymongo.collection.Collection(self.client[self.i][self.db_name], name=self.coll_name)
cursor = query.aggregate(pipeline)
res = list(cursor)
for v in res:
name = v['name']
self.index_stat_results[name] = v
print('{:30}'.format(" " + key),
'{:10}'.format(self.index_stat_results[self.client[self.i].address][indx_n]["cache"][
"bytes currently in the cache"]),
'{:10}'.format(self.index_stat_results[self.client[self.i].address][indx_n]["cache"][
"tracked dirty bytes in the cache"]),
'{:10}'.format(self.index_stat_results[self.client[self.i].address][indx_n]["cache"][
"pages read into cache"]),
'{:10}'.format(self.index_stat_results[self.client[self.i].address][indx_n]["cache"][
"pages written from cache"]),
'{:10}'.format(self.index_stat_results[self.client[self.i].address][indx_n]["cache"][
"pages requested from the cache"]),
'{:10}'.format(self.index_stat_results[indx_n]['accesses']['ops']))
def print_all_results(self):
for self.i in range(len(self.client)):
print(self.client[self.i].address)
print('{:30} {:>10} {:>10} {:>10} {:>10} {:>10} {:>10}'.format("name", "used", "dirty", "pri", "pwf", "prq",
"ops"))
self.print_wiredTiger_info()
self.print_index_info()
def main():
parser = argparse.ArgumentParser(description='dba-mongodb-reporting')
parser.add_argument('-host', help='hostname', action='store', dest='host', default=None)
parser.add_argument('-port', help='port number', action='store', dest='port', default=None, )
parser.add_argument('-db', help='database_name', action='store', dest='database_name', required=True)
parser.add_argument('-coll', action='store', dest='collection_name', help='collection name', required=True)
parser.add_argument('-discover', action='store_true', dest='discover', help='Discover repset members',
default=None)
arg_results = parser.parse_args()
results = Collstats(arg_results)
results.run_collStats()
results.print_all_results()
if __name__ == '__main__':
main()