diff --git a/shell/db.js b/shell/db.js index ec2b28fda2b..4228001126d 100644 --- a/shell/db.js +++ b/shell/db.js @@ -119,6 +119,7 @@ DB.prototype.help = function() { print("\tdb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }"); print("\tdb.addUser(username, password)"); print("\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )"); + print("\tdb.getReplicationInfo()"); print("\tdb.getProfilingLevel()"); print("\tdb.setProfilingLevel(level) 0=off 1=slow 2=all"); print("\tdb.eval(func, args) run code server-side"); @@ -325,3 +326,64 @@ DB.prototype.toString = function(){ return this._name; } + +/** + Get a replication log information summary. +

+ This command is for the database/cloud administer and not applicable to most databases. + It is only used with the local database. One might invoke from the JS shell: +

+       use local
+       db.getReplicationInfo();
+  
+ It is assumed that this database is a replication master -- the information returned is + about the operation log stored at local.oplog.$main on the replication master. (It also + works on a machine in a replica pair: for replica pairs, both machines are "masters" from + an internal database perspective. +

+ * @return Object timeSpan: time span of the oplog from start to end if slave is more out + * of date than that, it can't recover without a complete resync +*/ +DB.prototype.getReplicationInfo = function() { + if( "local" != this ) + return { errmsg : "this command only works for database local" }; + + var result = { }; + var db = this; + var ol = db.system.namespaces.findOne({name:"local.oplog.$main"}); + if( ol && ol.options ) { + result.logSizeMB = ol.options.size / 1000 / 1000; + } else { + result.errmsg = "local.oplog.$main, or its options, not found in system.namespaces collection"; + return result; + } + + var firstc = db.oplog.$main.find().sort({$natural:1}).limit(1); + var lastc = db.oplog.$main.find().sort({$natural:-1}).limit(1); + if( !firstc.hasNext() || !lastc.hasNext() ) { + result.errmsg = "objects not found in local.oplog.$main -- is this a new and empty db instance?"; + result.oplogMainRowCount = db.oplog.$main.count(); + return result; + } + + var first = firstc.next(); + var last = lastc.next(); + { + var tfirst = first.ts; + var tlast = last.ts; + if( tfirst && tlast ) { + tfirst = tfirst / 4294967296; // low 32 bits are ordinal #s within a second + tlast = tlast / 4294967296; + result.timeDiff = tlast - tfirst; + result.timeDiffHours = result.timeDiff / 3600; + result.tFirst = (new Date(tfirst*1000)).toString(); + result.tLast = (new Date(tlast*1000)).toString(); + result.now = Date(); + } + else { + result.errmsg = "ts element not found in oplog objects"; + } + } + + return result; +}