-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-stats.js
211 lines (187 loc) · 5.27 KB
/
get-stats.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Gathers total cluster details about used disk space
*
* @author [email protected]
* @version 1.0
* @updated 2024-10-23
*
* History:
* 1.0 - Initial Release
*/
var getNumber = function (val) {
return val ? new Number(val) : new Number(0);
};
var formatBytes = function (bytes, decimals = 2) {
if (!+bytes) return "0 Bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [
"Bytes",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
};
var printHeader = function(title) {
print("\n")
print("---------------------");
print(title);
print("---------------------");
print(
[
"Namespace",
"Total Documents",
"Average Document Size",
"Total Documents Size",
"Total Storage Size",
"Reusable from Collections",
"Indexes",
"Reusable from Indexes",
"Total Orphan Documents",
].join(",")
);
}
var printTotals = function(title, totals) {
print(
[
`${title} Total`,
"",
"",
formatBytes(totals[0]),
formatBytes(totals[1]),
formatBytes(totals[2]),
formatBytes(totals[3]),
formatBytes(totals[4]),
""
].join(",")
);
}
var getData = function (name, shard, stats) {
var data = {
name: name,
shard: shard,
count: getNumber(stats.count),
avgSize: getNumber(stats.avgObjSize),
size: getNumber(stats.size),
storageSize: getNumber(stats.storageSize),
reusableSpace: getNumber(
stats.wiredTiger["block-manager"]["file bytes available for reuse"]
),
indexSpace: getNumber(stats.totalIndexSize),
indexReusable: 0,
orphanDocuments: getNumber(stats.numOrphanDocs)
};
var keys = Object.keys(stats.indexDetails);
for (var k in keys) {
data.indexReusable += getNumber(
stats.indexDetails[keys[k]]["block-manager"]["file bytes available for reuse"]
);
}
return data;
};
var getDataPerDatabase = function (databaseName) {
var result = [];
db.getSiblingDB(databaseName)
.getCollectionInfos({ type: "collection" }, { nameOnly: true })
.forEach(function (collectionData) {
var coll = db
.getSiblingDB(databaseName)
.getCollection(collectionData.name);
var stats = coll.stats({
indexDetails: true,
});
if (stats.hasOwnProperty("sharded")) {
var keys = Object.keys(stats.shards);
if (keys.length == 0) {
result.push(getData(stats.ns, stats));
} else {
for (var i in keys) {
var shard = keys[i];
var shardStats = stats.shards[shard];
result.push(getData(stats.ns + " (" + shard + ")", shard, shardStats));
}
}
}
});
var totals = [Number(0), Number(0), Number(0), Number(0), Number(0)];
var shards = {}
for (var r in result) {
var row = result[r];
print(
[
row.name,
row.count,
row.avgSize,
formatBytes(row.size),
formatBytes(row.storageSize),
formatBytes(row.reusableSpace),
formatBytes(row.indexSpace),
formatBytes(row.indexReusable),
row.orphanDocuments
].join(",")
);
totals[0] += row.size;
totals[1] += row.storageSize;
totals[2] += row.reusableSpace;
totals[3] += row.indexSpace;
totals[4] += row.indexReusable;
if (shards.hasOwnProperty[row.shard]) {
var shardsTotal = shards[row.shard];
} else {
var shardsTotal = [Number(0), Number(0), Number(0), Number(0), Number(0)];
}
shardsTotal[0] += row.size;
shardsTotal[1] += row.storageSize;
shardsTotal[2] += row.reusableSpace;
shardsTotal[3] += row.indexSpace;
shardsTotal[4] += row.indexReusable;
shards[row.shard] = shardsTotal;
}
return {totals: totals, shards: shards};
};
var ignoreList = ["admin", "local", "config"];
var clusterTotal = [Number(0), Number(0), Number(0), Number(0), Number(0)];
var shards = {};
db.getMongo()
.getDBNames()
.forEach(function (databaseName) {
if (ignoreList.indexOf(databaseName) < 0) {
printHeader(databaseName);
var result = getDataPerDatabase(databaseName);
printTotals(`Database: ${databaseName}`, result.totals);
clusterTotal[0] += result.totals[0];
clusterTotal[1] += result.totals[1];
clusterTotal[2] += result.totals[2];
clusterTotal[3] += result.totals[3];
clusterTotal[4] += result.totals[4];
var keys = Object.keys(result.shards);
for (i in keys) {
var shard = keys[i];
if (shards[shard]) {
var shardsTotal = shards[shard];
} else {
var shardsTotal = [Number(0), Number(0), Number(0), Number(0), Number(0)];
}
shardsTotal[0] += result.shards[shard][0];
shardsTotal[1] += result.shards[shard][1];
shardsTotal[2] += result.shards[shard][2];
shardsTotal[3] += result.shards[shard][3];
shardsTotal[4] += result.shards[shard][4];
shards[shard] = shardsTotal;
}
}
});
printHeader("Cluster Total");
var keys = Object.keys(shards);
for (i in keys) {
var shard = keys[i];
printTotals(shard, shards[shard]);
}
printTotals("Cluster", clusterTotal);