Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* Removes database user and role information from the output. #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 21 additions & 52 deletions getMongoData/getMongoData.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,17 @@
* limitations under the License.
*/

var _version = "2.6.0";
var _version = "3.0.0";

// Limit the number of collections this script gathers stats on in order
// to avoid the possibility of running out of file descriptors. This has
// been set to an extremely conservative number.
var _LIMIT_COLLECTIONS = 2500;

(function () {
"use strict";
}());

// For MongoDB 2.4 and before
if (DB.prototype.getUsers == null) {
DB.prototype.getUsers = function (args) {
var cmdObj = {usersInfo: 1};
Object.extend(cmdObj, args);
var res = this.runCommand(cmdObj);
if (!res.ok) {
var authSchemaIncompatibleCode = 69;
if (res.code == authSchemaIncompatibleCode ||
(res.code == null && res.errmsg == "no such cmd: usersInfo")) {
// Working with 2.4 schema user data
return this.system.users.find({}).toArray();
}
throw Error(res.errmsg);
}
return res.users;
}
}

// For MongoDB 2.4 and before
if (DB.prototype.getRoles == null) {
DB.prototype.getRoles = function (args) {
return "No custom roles";
}
}

// Taken from the >= 3.1.9 shell to capture print output
if (typeof print.captureAllOutput === "undefined") {
print.captureAllOutput = function (fn, args) {
Expand Down Expand Up @@ -279,6 +258,7 @@ function printReplicaSetInfo() {
function printDataInfo(isMongoS) {
section = "data_info";
var dbs = printInfo('List of databases', function(){return db.getMongo().getDBs()}, section);
var collections_counter = 0;

if (dbs.databases) {
dbs.databases.forEach(function(mydb) {
Expand All @@ -296,6 +276,10 @@ function printDataInfo(isMongoS) {
collections.forEach(function(col) {
printInfo('Collection stats (MB)',
function(){return db.getSiblingDB(mydb.name).getCollection(col).stats(1024*1024)}, section);
collections_counter++;
if (collections_counter > _LIMIT_COLLECTIONS) {
throw(`Already asked for stats on ${collections_counter} collections which is above the max allowed for this script.`);
}
if (isMongoS) {
printInfo('Shard distribution',
function(){return db.getSiblingDB(mydb.name).getCollection(col).getShardDistribution()}, section, true);
Expand Down Expand Up @@ -329,18 +313,6 @@ function printDataInfo(isMongoS) {

return res;
}, section);
if (col != "system.users") {
printInfo('Sample document',
function(){
var lastValCursor = db.getSiblingDB(mydb.name).getCollection(col).find().sort({'$natural': -1}).limit(-1);
if (lastValCursor.hasNext()) {
return lastValCursor.next()
}
else {
return null;
}
}, section);
}
});
}
});
Expand Down Expand Up @@ -375,14 +347,6 @@ function printShardOrReplicaSetInfo() {
return false;
}

function printAuthInfo() {
section = "auth_info";
db = db.getSiblingDB('admin');
printInfo('Users', function(){return db.getUsers()}, section);
printInfo('Custom roles', function(){return db.getRoles()}, section);
}


if (typeof _printJSON === "undefined") var _printJSON = false;
if (typeof _ref === "undefined") var _ref = null;
var _output = [];
Expand All @@ -394,8 +358,13 @@ if (! _printJSON) {
print("================================");
}
var _host = hostname();
printServerInfo();
var isMongoS = printShardOrReplicaSetInfo();
printAuthInfo();
printDataInfo(isMongoS);
if (_printJSON) print(JSON.stringify(_output, jsonStringifyReplacer, 4));

try {
printServerInfo();
var isMongoS = printShardOrReplicaSetInfo();
printDataInfo(isMongoS);
if (_printJSON) print(JSON.stringify(_output, jsonStringifyReplacer, 4));
} catch(e) {
print(`ERROR: ${e}`);
quit(1);
}