I want to write a script which will kill all mongo queries running for more than 1min. The script is working fine for queries running on primary machine but not working when queries are running on secondary machines as it is not able to get Ops running on secondaries.
The below script is working fine if queries are running on primary machine but it's not working when the queries are running on any one of the two secondary machines.
client = pymongo.MongoClient(
"connection")
admin_db = client.get_database("admin")
pipeline = [
{"$currentOp": {"allUsers": True, "idleSessions": True}},
{
"$match": {
"active": True,
"secs_running": {
"$gte": 2
},
"ns": {"$in": ["test.fruits", "mydb.collection2"]},
"op": {"$in": ["query", "command"]},
}
},
]
ops = admin_db.aggregate(pipeline)
count = 0
for op in ops:
admin_db.command({"killOp": 1, "op": op["opid"]})
print(op)
count += 1
print(count)
logging.info("ops found: %d" % count)