da lanciare con: mongo mongo check_collection_size.js
permette di ottenere la size delle collection di tutti i db in modo da poterla monitorare con qualche app tipo jmeter o nagios.
il risultato:
[ {
"name" : "meteoArpa",
"collections" : [
{
"name" : "data",
"storageSize" : "680.00 KB",
"size" : "510.89 KB",
"count" : 519
},
{
"name" : "pluvio",
"storageSize" : "21.46 MB",
"size" : "19.97 MB",
"count" : 20779
},
{
"name" : "system.indexes",
"storageSize" : "8.00 KB",
"size" : "336 B",
"count" : 3
},
{
"name" : "temperature",
"storageSize" : "21.46 MB",
"size" : "20.00 MB",
"count" : 20807
}
]
},
{
"name" : "pcat",
"collections" : [
{
"name" : "products",
"storageSize" : "8.00 KB",
"size" : "3.58 KB",
"count" : 11
},
{
"name" : "system.indexes",
"storageSize" : "8.00 KB",
"size" : "112 B",
"count" : 1
}
]
},
{
"name" : "admin",
"collections" : [ ]
}
]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bytesToSize = function(bytes, precision) { | |
var kilobyte = 1024; | |
var megabyte = kilobyte * 1024; | |
var gigabyte = megabyte * 1024; | |
var terabyte = gigabyte * 1024; | |
if ((bytes >= 0) && (bytes < kilobyte)) { | |
return bytes + ' B'; | |
} else if ((bytes >= kilobyte) && (bytes < megabyte)) { | |
return (bytes / kilobyte).toFixed(precision) + ' KB'; | |
} else if ((bytes >= megabyte) && (bytes < gigabyte)) { | |
return (bytes / megabyte).toFixed(precision) + ' MB'; | |
} else if ((bytes >= gigabyte) && (bytes < terabyte)) { | |
return (bytes / gigabyte).toFixed(precision) + ' GB'; | |
} else if (bytes >= terabyte) { | |
return (bytes / terabyte).toFixed(precision) + ' TB'; | |
} else { | |
return bytes + ' B'; | |
} | |
} | |
var report = []; | |
for(dbName in db.getMongo().getDBNames()){ | |
var name = db.getMongo().getDBNames()[dbName]; | |
var newDB = db.getSisterDB(name); | |
var db_json = {'name' : name, collections : [] }; | |
for(dbCollName in newDB.getCollectionNames()){ | |
var collName = newDB.getCollectionNames()[dbCollName]; | |
var newColl = newDB.getCollection(collName); | |
db_json.collections.push({"name":collName, "storageSize": bytesToSize(newColl.stats().storageSize,2), "size":bytesToSize(newColl.stats().size,2), "count":newColl.stats().count} ) | |
} | |
report.push(db_json); | |
} | |
printjson(report); |