我正在使用 db.currentOp() 检查 Mongodb 中的当前连接。如何获得所有记录的总数以及如何在一列中获得所有“客户端”IP?谢谢
db.currentOp()
{
"inprog" : [
{
"desc" : "WT RecordStoreThread: local.oplog.rs",
"threadId" : "1068",
"active" : true,
"opid" : 77300,
"secs_running" : 712,
"microsecs_running" : NumberLong(712734301),
"op" : "none",
"ns" : "local.oplog.rs",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
}
},
{
"desc" : "conn52",
"threadId" : "4692",
"connectionId" : 52,
"client" : "10.10.50.218:60909",
"active" : true,
"opid" : 101277,
"secs_running" : 2,
"microsecs_running" : NumberLong(2883686),
"op" : "getmore",
"ns" : "local.oplog.rs",
"query" : {
"getMore" : NumberLong("15696192304"),
"collection" : "oplog.rs",
"maxTimeMS" : NumberLong(5000),
"term" : NumberLong(117),
"lastKnownCommittedOpTime" : {
"ts" : Timestamp(1501842736, 18),
"t" : NumberLong(117)
}
},
"planSummary" : "COLLSCAN",
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(2)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"r" : NumberLong(1)
}
}
}
},
....
...
..
Run Code Online (Sandbox Code Playgroud)
这将有助于汇总信息 -
ClientIP 的连接数,总计
对于 Mongo 外壳:
db.currentOp(true).inprog.reduce((accumulator, connection) => { ipaddress = connection.client ? connection.client.split(":")[0] : "unknown"; accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1; accumulator["TOTAL_CONNECTION_COUNT"]++; return accumulator; }, { TOTAL_CONNECTION_COUNT: 0 })
Run Code Online (Sandbox Code Playgroud)
格式化:
db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "unknown";
accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)
Run Code Online (Sandbox Code Playgroud)
示例输出:
{
"TOTAL_CONNECTION_COUNT" : 331,
"192.168.253.72" : 8,
"192.168.254.42" : 17,
"127.0.0.1" : 3,
"192.168.248.66" : 2,
"11.178.12.244" : 2,
"unknown" : 41,
"3.100.12.33" : 86,
"11.148.23.34" : 168,
"81.127.34.11" : 1,
"84.147.25.17" : 3
}
Run Code Online (Sandbox Code Playgroud)
(192.xxx地址是Atlas内部监控)
| 归档时间: |
|
| 查看次数: |
20559 次 |
| 最近记录: |