Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2 scripts below combine multiple commands to get all needed Index/Collection data from Primary and Secondary
- // Index accessed.ops counts, index size (MB), number of days of index data, TTL index seconds, and capped collections
- // get all index data for all collections
- // in 1 line
- indexUsage=[];now=new Date().getTime();db.getCollectionNames().forEach(function(collectionName){db.getMongo().setReadPref('primary');db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){indexUsage.push({collection:collectionName,name:a.name,count:a.accesses.ops,since:Math.ceil((now-a.accesses.since.getTime())/1000/60/60/24),isCapped:db.getCollection(collectionName).isCapped()});});db.getCollection(collectionName).getIndexes().forEach(function(a){indexUsage.forEach(function(b){if(a.name==b.name&&collectionName==b.collection){b.expireAfterSeconds=a.expireAfterSeconds||0;}});});db.getMongo().setReadPref('secondary');db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){indexUsage.forEach(function(b){if(a.name==b.name&&collectionName==b.collection){b.count+=a.accesses.ops;}});});indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;for(index in indexSizes){indexUsage.forEach(function(b){if(index==b.name&&collectionName==b.collection){b.size=indexSizes[index];}});}});indexUsage.sort(function(a,b){return b.count-a.count||a.collection.localeCompare(b.collection)||a.name.localeCompare(b.name);});print('\nOPS SIZE DAYS TTL COLLECTION NAME\n');indexUsage.forEach(function(a){print(a.count+' '.slice(a.count.toString().length)+a.size+' '.slice(a.size.toString().length)+a.since+' '.slice(a.since.toString().length)+a.expireAfterSeconds+' '.slice(a.expireAfterSeconds.toString().length)+a.collection+(a.isCapped?' [C]':'')+' '.slice(a.collection.toString().length+(a.isCapped?4:0))+a.name);});
- // pretty
- indexUsage=[];
- now=new Date().getTime();
- db.getCollectionNames().forEach(function(collectionName){
- db.getMongo().setReadPref('primary');
- db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
- indexUsage.push({collection:collectionName,name:a.name,count:a.accesses.ops,since:Math.ceil((now-a.accesses.since.getTime())/1000/60/60/24),isCapped:db.getCollection(collectionName).isCapped()});
- });
- db.getCollection(collectionName).getIndexes().forEach(function(a){
- indexUsage.forEach(function(b){
- if(a.name==b.name&&collectionName==b.collection){b.expireAfterSeconds=a.expireAfterSeconds||0;}
- });
- });
- db.getMongo().setReadPref('secondary');
- db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
- indexUsage.forEach(function(b){
- if(a.name==b.name&&collectionName==b.collection){b.count+=a.accesses.ops;}
- });
- });
- indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;
- for(index in indexSizes){
- indexUsage.forEach(function(b){
- if(index==b.name&&collectionName==b.collection){b.size=indexSizes[index];}
- });
- }
- });
- indexUsage.sort(function(a,b){
- return b.count-a.count||a.collection.localeCompare(b.collection)||a.name.localeCompare(b.name);
- });
- print('\nOPS SIZE DAYS TTL COLLECTION NAME\n');
- indexUsage.forEach(function(a){
- print(a.count+' '.slice(a.count.toString().length)+a.size+' '.slice(a.size.toString().length)+a.since+' '.slice(a.since.toString().length)+a.expireAfterSeconds+' '.slice(a.expireAfterSeconds.toString().length)+a.collection+(a.isCapped?' [C]':'')+' '.slice(a.collection.toString().length+(a.isCapped?4:0))+a.name);
- });
- // group by collection, specify 'all' or specific collection
- // in 1 line
- collection='all';now=new Date().getTime();db.getCollectionNames().sort().forEach(function(collectionName){if(collection!='all'&&collection!=collectionName){return;}indexUsage=[];db.getMongo().setReadPref('primary');db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){indexUsage.push({name:a.name,count:a.accesses.ops,since:Math.ceil((now-a.accesses.since.getTime())/1000/60/60/24)});});db.getCollection(collectionName).getIndexes().forEach(function(a){indexUsage.forEach(function(b){if(a.name==b.name){b.expireAfterSeconds=a.expireAfterSeconds||0;}});});db.getMongo().setReadPref('secondary');db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){indexUsage.forEach(function(b){if(a.name==b.name){b.count+=a.accesses.ops;}});});indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;for(index in indexSizes){indexUsage.forEach(function(b){if(index==b.name){b.size=indexSizes[index];}});}indexUsage.sort(function(a,b){return b.count-a.count;});print('\n- '+collectionName+(db.getCollection(collectionName).isCapped()?' [CAPPED]':'')+'\n\nOPS SIZE DAYS TTL NAME');indexUsage.forEach(function(a){print(a.count+' '.slice(a.count.toString().length)+a.size+' '.slice(a.size.toString().length)+a.since+' '.slice(a.since.toString().length)+a.expireAfterSeconds+' '.slice(a.expireAfterSeconds.toString().length)+a.name);});});
- // pretty
- collection='all';
- now=new Date().getTime();
- db.getCollectionNames().sort().forEach(function(collectionName){
- if(collection!='all'&&collection!=collectionName){return;}
- indexUsage=[];
- db.getMongo().setReadPref('primary');
- db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
- indexUsage.push({name:a.name,count:a.accesses.ops,since:Math.ceil((now-a.accesses.since.getTime())/1000/60/60/24)});
- });
- db.getCollection(collectionName).getIndexes().forEach(function(a){
- indexUsage.forEach(function(b){
- if(a.name==b.name){b.expireAfterSeconds=a.expireAfterSeconds||0;}
- });
- });
- db.getMongo().setReadPref('secondary');
- db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
- indexUsage.forEach(function(b){
- if(a.name==b.name){b.count+=a.accesses.ops;}
- });
- });
- indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;
- for(index in indexSizes){
- indexUsage.forEach(function(b){
- if(index==b.name){b.size=indexSizes[index];}
- });
- }
- indexUsage.sort(function(a,b){
- return b.count-a.count;
- });
- print('\n- '+collectionName+(db.getCollection(collectionName).isCapped()?' [CAPPED]':'')+'\n\nOPS SIZE DAYS TTL NAME');
- indexUsage.forEach(function(a){
- print(a.count+' '.slice(a.count.toString().length)+a.size+' '.slice(a.size.toString().length)+a.since+' '.slice(a.since.toString().length)+a.expireAfterSeconds+' '.slice(a.expireAfterSeconds.toString().length)+a.name);
- });
- });
Add Comment
Please, Sign In to add comment