Guest User

Untitled

a guest
Jul 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. // 2 scripts below combine multiple commands to get all needed Index/Collection data from Primary and Secondary
  2. // Index accessed.ops counts, index size (MB), number of days of index data, TTL index seconds, and capped collections
  3.  
  4. // get all index data for all collections
  5. // in 1 line
  6. 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);});
  7. // pretty
  8. indexUsage=[];
  9. now=new Date().getTime();
  10. db.getCollectionNames().forEach(function(collectionName){
  11. db.getMongo().setReadPref('primary');
  12. db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
  13. 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()});
  14. });
  15. db.getCollection(collectionName).getIndexes().forEach(function(a){
  16. indexUsage.forEach(function(b){
  17. if(a.name==b.name&&collectionName==b.collection){b.expireAfterSeconds=a.expireAfterSeconds||0;}
  18. });
  19. });
  20. db.getMongo().setReadPref('secondary');
  21. db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
  22. indexUsage.forEach(function(b){
  23. if(a.name==b.name&&collectionName==b.collection){b.count+=a.accesses.ops;}
  24. });
  25. });
  26. indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;
  27. for(index in indexSizes){
  28. indexUsage.forEach(function(b){
  29. if(index==b.name&&collectionName==b.collection){b.size=indexSizes[index];}
  30. });
  31. }
  32. });
  33. indexUsage.sort(function(a,b){
  34. return b.count-a.count||a.collection.localeCompare(b.collection)||a.name.localeCompare(b.name);
  35. });
  36. print('\nOPS SIZE DAYS TTL COLLECTION NAME\n');
  37. indexUsage.forEach(function(a){
  38. 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);
  39. });
  40.  
  41.  
  42.  
  43.  
  44. // group by collection, specify 'all' or specific collection
  45. // in 1 line
  46. 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);});});
  47. // pretty
  48. collection='all';
  49. now=new Date().getTime();
  50. db.getCollectionNames().sort().forEach(function(collectionName){
  51. if(collection!='all'&&collection!=collectionName){return;}
  52. indexUsage=[];
  53. db.getMongo().setReadPref('primary');
  54. db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
  55. indexUsage.push({name:a.name,count:a.accesses.ops,since:Math.ceil((now-a.accesses.since.getTime())/1000/60/60/24)});
  56. });
  57. db.getCollection(collectionName).getIndexes().forEach(function(a){
  58. indexUsage.forEach(function(b){
  59. if(a.name==b.name){b.expireAfterSeconds=a.expireAfterSeconds||0;}
  60. });
  61. });
  62. db.getMongo().setReadPref('secondary');
  63. db.getCollection(collectionName).aggregate([{$indexStats:{}}]).forEach(function(a){
  64. indexUsage.forEach(function(b){
  65. if(a.name==b.name){b.count+=a.accesses.ops;}
  66. });
  67. });
  68. indexSizes=db.getCollection(collectionName).stats(1000000).indexSizes;
  69. for(index in indexSizes){
  70. indexUsage.forEach(function(b){
  71. if(index==b.name){b.size=indexSizes[index];}
  72. });
  73. }
  74. indexUsage.sort(function(a,b){
  75. return b.count-a.count;
  76. });
  77. print('\n- '+collectionName+(db.getCollection(collectionName).isCapped()?' [CAPPED]':'')+'\n\nOPS SIZE DAYS TTL NAME');
  78. indexUsage.forEach(function(a){
  79. 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);
  80. });
  81. });
Add Comment
Please, Sign In to add comment