Skip to content

Instantly share code, notes, and snippets.

@loopj
Created June 8, 2012 18:58
Show Gist options
  • Save loopj/2897572 to your computer and use it in GitHub Desktop.
Save loopj/2897572 to your computer and use it in GitHub Desktop.
Useful Mongo Shell Functions
function getCurrentOpStats() {
intervals = [1,5,10,30]
waitingForLock = 0;
secsRunningStats = {};
inProg = db.currentOp()["inprog"]
inProg.forEach(function (op) {
if(op["waitingForLock"]) {
waitingForLock += 1;
}
if(op["secs_running"]) {
intervals.forEach(function (interval) {
if(op["secs_running"] > interval) {
secsRunningStats[interval] = secsRunningStats[interval] || 0;
secsRunningStats[interval] += 1;
}
});
}
});
print("total = " + inProg.length);
print("waitingForLock = " + waitingForLock);
for(var i in secsRunningStats) {
print("secs_running > " + i + " = " + secsRunningStats[i]);
};
}
function killLongRunningOps(time) {
db.currentOp()["inprog"].forEach(function (op) {
if(op["secs_running"]) {
if(op["secs_running"] > time) {
print("Killing this op (running " + op["secs_running"] + " seconds)");
print(tojson(op))
print("")
db.killOp(op["opid"])
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment