Skip to content

Instantly share code, notes, and snippets.

View alexbevi's full-sized avatar
🙀

Alex Bevilacqua alexbevi

🙀
View GitHub Profile

This script uses bsondump (twice), jq (twice) and ruby

bsondump --quiet metrics.2019-12-22T17-34-22Z-00000 | jq -s '.[] | select( .type | ."$numberInt" == "1")' | jq -s 'first | .data ."$binary" .base64' -Mc | ruby -rzlib -rbase64 -e 'd = STDIN.read; print Zlib::Inflate.new.inflate(Base64.decode64(d)[4..-1])' | bsondump --quiet | jq | less
@alexbevi
alexbevi / initialSyncProgress.js
Created February 14, 2020 15:59
Script to help identify the progress of an intial sync of a MongoDB SECONDARY node
/*
* initialSyncProgress
* @author Alex Bevilacqua <[email protected]>
*
* Can be run against a MongoDB 3.4+ mongod that is in STARTUP2 (intitial sync) state to gain some
* insight into how the sync is progressing. This script WILL NOT tell you how long until the sync
* is complete, but based on how the script reports progress can be used to estimate this.
*
* usage:
* mongo --quiet --eval "load('initialSyncProgress.js'); initialSyncProgress();"
{
"set": "replset",
"date": ISODate("2019-12-04T05:12:52.835Z"),
"myState": 5,
"term": NumberLong(3),
"syncingTo": "m2.example.net:27017",
"syncSourceHost": "m2.example.net:27017",
"syncSourceId": 1,
"heartbeatIntervalMillis": NumberLong(2000),
"majorityVoteCount": 2,
/*
* Print storage details for all collections and indexes.
* Supports sharded clusters
*
* @author [email protected]
* @version 1.3
* @updated 2022-11-21
*
* History:
* 1.3 - Filter out admin, local and config databases
// Print index details
print(["Namespace", "Index Name", "Index Size", "Index Keys", "Usage Count", "Last Used"].join('\t'));
db.getMongo().getDBNames().forEach(function (dbname) {
db.getSiblingDB(dbname).getCollectionNames().forEach(function (cname) {
var coll = db.getSiblingDB(dbname).getCollection(cname);
var stats = coll.stats();
coll.aggregate([ { $indexStats: {} } ]).forEach(function(ix) {
var ixname = ix.name;
var ns = dbname + "." + cname;
var ixsize = stats.indexSizes[ixname];
@alexbevi
alexbevi / getBackingFilenames.js
Created March 23, 2020 15:22
Return a summarized list of all collection and index uris for a MongoDB cluster
/**
* getBackingFilenames(filter)
*
* Function to return a summarized list of all collections and indexes within all databases.
* Passing *filter* will optionally filter the results by either the collection uri or
* the uri of any of the indexes within a collection.
*
* @samples
* // unfiltered
* printjson(getBackingFilenames());
@alexbevi
alexbevi / measureIndexUsage.js
Last active March 23, 2020 20:13
Measure the index utilization for all collections in a database before/after an operation
/**
* measureIndexUsage(block)
*
* Measure the index utilization before/after a block is run. The block can contain
* one to many operations, however the index utilization will only measure the collections
* associated with the current `db` instance.
*
* NOTE: this script does not "isolate" the operation(s) run so if running in a busy
* environment the results may represent other operations index utilization.
*
@alexbevi
alexbevi / measureInitialSyncProgress.js
Last active December 1, 2023 12:18
MongoDB 5.0+ Initial Sync Progress Monitoring
/*
* measureInitialSyncProgress
* @author Alex Bevilacqua <[email protected]>
* @updated 2021-02-23
*
* Can be run against a MongoDB 4.2.12+ mongod that is in STARTUP2 (intitial sync) state to gain some
* insight into how the sync is progressing based on the improvements introduced with SERVER-47863.
* For versions of MongoDB < 4.2.12, see https://www.alexbevi.com/blog/2020/02/13/mongodb-initial-sync-progress-monitoring
*
* usage:
@alexbevi
alexbevi / README.md
Last active January 14, 2021 16:31
Dump BSON File Contents as JSON

dump_bson.rb

Usage

# Use -f/--filename to pass the file to emit JSON for
ruby dump_bson.rb -f auditLog_prod01.bson
....
{"atype":"authenticate","ts":"2020-11-21 06:25:22 UTC","local":{"ip":"127.0.0.1","port":27013},"remote":{"ip":"127.0.0.1","port":55842},"users":[{"user":"__system","db":"local"}],"roles":[],"param":{"user":"__system","db":"local","mechanism":"SCRAM-SHA-1"},"result":0}
{"atype":"shutdown","ts":"2020-11-21 06:25:23 UTC","local":{"ip":"127.0.0.1","port":27013},"remote":{"ip":"127.0.0.1","port":55842},"users":[{"user":"__system","db":"local"}],"roles":[],"param":{},"result":0}
@alexbevi
alexbevi / printSyncSourceTree.js
Last active January 27, 2022 07:44
MongoDB Replica Set Sync Source Tree
function printReplicationTree(rs) {
print("\nReplication Sync Source Tree\n============================");
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var idMapping = rs.members.reduce((acc, el, i) => {
acc[el.name] = i;
return acc;
}, {});