-
Notifications
You must be signed in to change notification settings - Fork 4
/
IndexReport.js
133 lines (123 loc) · 3.58 KB
/
IndexReport.js
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
/*
* IndexReport.js
*
* Scans multiple nodes, gathers index information,
* and compares across other nodes.
*
* Outputs:
* Report of indexes.
*
* Inputs:
* Set the connection info below
* NOTE: Wherever you run this from, it needs to be able to connect
* to the mongod nodes
*
* the 'connections' array holds all the host information
*
* Usage:
* $mongo -nodb IndexReport.js
*
*/
var checkAllReplSetMembers = false;
var connections = [
{
host : "localhost:27000",
username : "",
password : "",
authenticationDatabase : "",
dbsToCheck : [ "test" ]
}
,{
host : "localhost:27001",
username : "",
password : "",
authenticationDatabase : "",
dbsToCheck : [ "test" ]
}
,{
host : "localhost:27002",
username : "user",
password : "password",
authenticationDatabase : "admin",
dbsToCheck : [ "test" ]
}
];
var outputReport = function(output) {
print(output.report + "\t" + output.ts + "\n");
output.hosts.forEach( function(host) {
print("Host: " + host.host );
host.databases.forEach( function( database ) {
database.indexes.forEach( function(coll) {
var numIndexes = coll.indexes.length;
print( "Collection: " + database.database + "." + coll.collection
+ " " + numIndexes + " indexes");
coll.indexes.forEach( function(idx) {
print( "name: " +
("'"+idx.name+"'").pad( 20, true )
+ " key: " + tojson(idx.key) );
});
print();
});
print();
});
});
}
var report = [];
var printReport = function(line) { report.push(line); }
var getIndexes = function(connection,database) {
var out = {}
out.database = database;
out.indexes = [];
connection.getDB(database).getCollectionNames().forEach( function(c) {
var indexes = connection.getDB(database)[c].getIndexes();
out.indexes.push( { collection : c, indexes : indexes } );
});
return out;
}
var analyzeHost = function(connection,connInfo) {
var hostInfo = {}
hostInfo.host = connection.host;
hostInfo.databases = [];
try {
connInfo.dbsToCheck.forEach( function(database) {
hostInfo.databases.push( getIndexes(connection,database) );
});
} catch(error) {
hostInfo.error = error;
}
return hostInfo;
}
var connectAndCheckHost = function(connInfo) {
var hostInfo = {}
hostInfo.host = connInfo.host;
hostInfo.databases = [];
try {
var conn = new Mongo(connInfo.host);
var authOn = connInfo.username!="" && connInfo.password!="";
if ( authOn ) {
conn.getDB( connInfo.authenticationDatabase )
.auth( connInfo.username, connInfo.password );
}
conn.slaveOk=true;
var hostInfo = analyzeHost(conn,connInfo);
if ( authOn ) {
conn.getDB( connInfo.authenticationDatabase ).logout();
}
} catch(error ) {
hostInfo.error = error;
}
return hostInfo;
}
// spin through connections and gather data
var output = { "report" : "IndexAnalysis", "ts" : new Date() };
output.hosts = [];
for(var i=0;i<connections.length;i++) {
var connInfo = connections[i];
try {
var hostInfo = connectAndCheckHost(connInfo);
output.hosts.push( hostInfo );
} catch(error) {
output.hosts.push( { host : connInfo.host, error : error + 3} );
}
}
outputReport( output );