Created
May 21, 2013 19:32
-
-
Save mikedamage/5622531 to your computer and use it in GitHub Desktop.
Mongo collection stats script
This file contains hidden or 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
#!/bin/bash | |
# | |
# = Mongo Collection Stats Script | |
database=${1:-test} | |
mongo --quiet --eval ' | |
var collections = db.getCollectionNames(); | |
print("Collection Count Size StorageSize AvgObjSize nIndexes"); | |
collections.forEach(function(coll) { | |
var stats = db[coll].stats(); | |
var cols = [ coll, stats["count"], stats["size"], stats["storageSize"], stats["avgObjSize"], stats["nindexes"] ]; | |
print(cols.join(" ")); | |
}); | |
' $database | awk ' | |
BEGIN { | |
total_records = 0 | |
total_size = 0 | |
total_storage_size = 0 | |
} | |
NR == 1 { print } | |
NR > 1 { | |
printf("%s %s %s %s %s %s\n", $1, $2, num_to_human($3), num_to_human($4), num_to_human($5), $6) | |
total_records += $2 | |
total_size += $3 | |
total_storage_size += $4 | |
} | |
END { | |
printf("TOTAL %s %s %s\n", total_records, num_to_human(total_size), num_to_human(total_storage_size)) | |
} | |
function num_to_human(num) { | |
if (num == "undefined") { | |
adjusted = 0 | |
units = "B" | |
} else if (num > 1024000) { | |
adjusted = num / 1024000 | |
units = "MB" | |
} else if (num > 1024) { | |
adjusted = num / 1024 | |
units = "KB" | |
} else { | |
adjusted = num | |
units = "B" | |
} | |
return sprintf("%.2f%s", adjusted, units) | |
} | |
' | column -t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment