Skip to content

Instantly share code, notes, and snippets.

@akira-kurogane
Forked from normgraham/checkAllVersions.js
Last active February 12, 2016 02:35
Show Gist options
  • Save akira-kurogane/48b03a1d33695b890d93 to your computer and use it in GitHub Desktop.
Save akira-kurogane/48b03a1d33695b890d93 to your computer and use it in GitHub Desktop.
This script traverses all the hosts in a sharded cluster; collects cmdLineOpts, buildInfo, hostInfo, and serverStatus, and makes some basic checks. To use it, save the script on one of your machines and execute it with the following command, ensuring that $HOST and $PORT specify one of your mongos processes: {code} mongo --host $HOST --port $PO…
var checkAllVersions = function (verbose) {
var equalOrLaterVersion = function (v1, v2) {
return (v1[0] > v2[0]) ||
(v1[0] == v2[0] && v1[1] > v2[1]) ||
(v1[0] == v2[0] && v1[1] == v2[1] && v1[2] >= v2[2]);
}
var getHostInfo = function (info) {
try {
var connection = new Mongo(info["config host"]);
var admin = connection.getDB("admin");
info.cmdLineOpts = admin.runCommand("getCmdLineOpts").parsed;
info.buildInfo = admin.runCommand("buildInfo");
if (equalOrLaterVersion(info.buildInfo.versionArray, [2,2,0,0]))
info.hostInfo = admin.runCommand("hostInfo");
if (verbose)
info.serverStatus = admin.serverStatus();
else
info.serverStatus = admin.serverStatus({
asserts: 0,
backgroundFlushing: 0,
connections: 0,
cursors: 0,
dur: 0,
extra_info: 0,
globalLock: 0,
indexCounters: 0,
locks: 0,
network: 0,
opcounters: 0,
opcountersRepl: 0,
recordStats: 0,
metrics: 0,
ops: 0,
shardCursorType: 0,
replNetworkQueue: 0,
writeBacksQueued: 0,
wiredTiger: 0,
tcmalloc: 0,
ok: 0
});
info.process = info.serverStatus.process;
info.version = info.serverStatus.version;
if (equalOrLaterVersion(info.buildInfo.versionArray, [2,6,0,0])) {
if ("sharding" in info.cmdLineOpts && "configDB" in info.cmdLineOpts.sharding)
info.configdb = info.cmdLineOpts.sharding.configDB;
}
else if ("configdb" in info.cmdLineOpts)
info.configdb = info.cmdLineOpts.configdb;
return info;
}
catch (err) {
info.error = err.toString();
return info;
}
}
var versions = [{"SUMMARY":{}}];
var config = db.getSiblingDB("config");
var lastPing = config.mongos.find().sort({ping:-1}).limit(1).next().ping;
var firstPing = new Date(lastPing);
firstPing.setMinutes(lastPing.getMinutes() - 20);
config.mongos.find({"ping" : {"$gte" : firstPing}}).sort({"_id" : 1}).forEach(function (doc) {
versions.push(getHostInfo({
"config host" : doc._id,
"config source" : "mongos",
"configdb" : "*****",
"process" : "*****",
"version" : "*****"
}));
});
config.shards.find().sort({"host" : 1}).forEach(function(shard) {
shard.host.split("/")[1].split(",").forEach(function(host) {
versions.push(getHostInfo({
"config host" : host,
"config source" : "shards",
"config replset" : shard.host.split("/")[0],
"process" : "*****",
"version" : "*****"
}));
});
});
var admin = db.getSiblingDB("admin");
var configdbs = [];
if (equalOrLaterVersion(db.runCommand("buildInfo").versionArray, [2,6,0,0]))
configdbs = admin.runCommand("getCmdLineOpts").parsed.sharding.configDB.split(",");
else
configdbs = admin.runCommand("getCmdLineOpts").parsed.configdb.split(",");
configdbs.forEach(function(config) {
versions.push(getHostInfo({
"config host" : config,
"config source" : "cmdLineOpts.parsed.configdb",
"process" : "*****",
"version" : "*****"
}));
});
var check = function (condition) {
var allGood = true;
versions.slice(2).forEach(function (doc) {allGood = (allGood && condition(doc))});
return allGood;
}
versions[0].SUMMARY["all mongos --configdb options match"] = check(function (doc) {
return !("configdb" in doc) || doc.configdb === versions[1].configdb;
});
versions[0].SUMMARY["all observed mongos and mongod versions match"] = check(function (doc) {
return doc.version === "*****" || doc.version === versions[1].version;
});
versions[0].SUMMARY["all 64 bit binaries"] = check(function (doc) {
return doc.buildInfo.bits == 64;
});
versions[0].SUMMARY["all stable releases"] = check(function (doc) {
return !(doc.buildInfo.versionArray[1] & 1);
})
printjson(versions);
}
checkAllVersions();
@akira-kurogane
Copy link
Author

Adding "wiredTiger" and "tcmalloc" as serverStatus output to suppress in non-verbose mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment