[root@bfd-ango-01 shell]# mongostat -h 47.105.115.143:27017 -udolphin -pk8DDg9sd --authenticationDatabase admin
insert query update delete getmore command flushes mapped vsize res faults qrw arw net_in net_out conn time
*0 *0 *0 *0 0 12|0 0 0B 272M 22.0M 0 0|0 0|0 1.61k 24.1k 6 Jan 14 15:29:31.648
*0 *0 *0 *0 0 26|0 0 0B 272M 22.0M 0 0|0 0|0 3.84k 39.0k 6 Jan 14 15:29:32.642
*0 *0 *0 *0 0 12|0 0 0B 272M 22.0M 0 0|0 0|0 1.69k 23.3k 6 Jan 14 15:29:33.643
*0 *0 *0 *0 0 24|0 0 0B 272M 22.0M 0 0|0 0|0 3.59k 37.7k 6 Jan 14 15:29:34.663
*0 *0 *0 *0 0 11|0 0 0B 272M 22.0M 0 0|0 0|0 1.51k 22.7k 6 Jan 14 15:29:35.675
*0 *0 *0 *0 0 12|0 0 0B 272M 22.0M 0 0|0 0|0 1.55k 23.1k 6 Jan 14 15:29:36.666
*0 *0 *0 *0 0 25|0 0 0B 272M 22.0M 0 0|0 0|0 3.75k 39.4k 6 Jan 14 15:29:37.642
*0 13 *0 *0 0 12|0 0 0B 272M 22.0M 0 0|0 0|0 5.76k 36.3k 6 Jan 14 15:29:38.641
*0 *0 *0 *0 0 11|0 0 0B 272M 22.0M 0 0|0 0|0 1.53k 22.9k 6 Jan 14 15:29:39.642
*0 *0 *0 *0 0 11|0 0 0B 272M 22.0M 0 0|0 0|0 1.53k 22.9k 6 Jan 14 15:29:40.644
mongostat是mongdb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。
它的输出有以下几列:
inserts/s 每秒插入次数
query/s 每秒查询次数
update/s 每秒更新次数
delete/s 每秒删除次数
getmore/s 每秒执行getmore次数
command/s 每秒的命令数,比以上插入、查找、更新、删除的综合还多,还统计了别的命令
flushs/s 每秒执行fsync将数据写入硬盘的次数。
mapped/s 所有的被mmap的数据量,单位是MB,
vsize 虚拟内存使用量,单位MB
res 物理内存使用量,单位MB
faults/s 每秒访问失败数(只有Linux有),数据被交换出物理内存,放到swap。不要超过100,否则就是机器内存太小,造成频繁swap写入。此时要升级内存或者扩展
locked % 被锁的时间百分比,尽量控制在50%以下吧
idx miss % 索引不命中所占百分比。如果太高的话就要考虑索引是不是少了
q t|r|w 当Mongodb接收到太多的命令而数据库被锁住无法执行完成,它会将命令加入队列。这一栏显示了总共、读、写3个队列的长度,都为0的话表示mongo毫无压力。高并发时,一般队列值会升高。
conn 当前连接数
time 时间戳
这里我们不会很详细描述每一步操作,只是告诉如何去监控这些指标,实际情况根据自己的需要去监控,首先我们看看mongodb有那些指标
[root@bfd-ango-01 shell]# echo "db.serverStatus()"|mongo 47.105.115.143:27017/app -udolphin -pk8DDg9sd --authenticationDatabase admin
MongoDB shell version v4.0.4
connecting to: mongodb://47.105.115.143:27017/app
Implicit session: session { "id" : UUID("49e995c3-4836-4b51-a52a-7b3b4900a7c8") }
MongoDB server version: 4.0.4
{
"host" : "bfd-ango-01",
"version" : "4.0.4",
"process" : "mongos",
"pid" : NumberLong(675154),
"uptime" : 3534927,
"uptimeMillis" : NumberLong("3534927010"),
"uptimeEstimate" : NumberLong(3534927),
"localTime" : ISODate("2019-01-14T07:35:20.453Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 241,
"rollovers" : 0
},
"connections" : {
"current" : 6,
"available" : 524274,
"totalCreated" : 158240
},
"extra_info" : {
"note" : "fields vary by platform",
"page_faults" : 4
},
"network" : {
"bytesIn" : NumberLong(470734593),
"bytesOut" : NumberLong("2771880916"),
"physicalBytesIn" : NumberLong(470734593),
"physicalBytesOut" : NumberLong("2771880916"),
"numRequests" : NumberLong(2826205),
"compression" : {
"snappy" : {
"compressor" : {
"bytesIn" : NumberLong(1124249067),
"bytesOut" : NumberLong(973585235)
},
"decompressor" : {
"bytesIn" : NumberLong(1332237452),
"bytesOut" : NumberLong(1968198165)
}
}
},
"serviceExecutorTaskStats" : {
"executor" : "passthrough",
"threadsRunning" : 6
}
},
"opcounters" : {
"insert" : 10459,
"query" : 71434,
"update" : 132,
"delete" : 261,
"getmore" : 4,
"command" : 2754551
},
"sharding" : {
"configsvrConnectionString" : "bfd/127.0.0.1:28017,127.0.0.1:28018,127.0.0.1:28019",
"lastSeenConfigServerOpTime" : {
"ts" : Timestamp(1547451317, 1),
"t" : NumberLong(3)
},
"maxChunkSizeInBytes" : NumberLong(67108864)
},
"shardingStatistics" : {
"catalogCache" : {
"numDatabaseEntries" : NumberLong(3),
"numCollectionEntries" : NumberLong(3),
"countStaleConfigErrors" : NumberLong(0),
"totalRefreshWaitTimeMicros" : NumberLong(21910182),
"numActiveIncrementalRefreshes" : NumberLong(0),
"countIncrementalRefreshesStarted" : NumberLong(11783),
"numActiveFullRefreshes" : NumberLong(0),
"countFullRefreshesStarted" : NumberLong(3),
"countFailedRefreshes" : NumberLong(0)
}
},
"transportSecurity" : {
"1.0" : NumberLong(0),
"1.1" : NumberLong(0),
"1.2" : NumberLong(0),
"1.3" : NumberLong(0),
"unknown" : NumberLong(0)
},
"mem" : {
"bits" : 64,
"resident" : 22,
"virtual" : 272,
"supported" : true
},
"metrics" : {
"cursor" : {
"timedOut" : NumberLong(0),
"open" : {
"multiTarget" : NumberLong(0),
"singleTarget" : NumberLong(0),
"pinned" : NumberLong(0),
"total" : NumberLong(0)
}
},
............ #省略部分代码
"ok" : 1,
"operationTime" : Timestamp(1547451317, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1547451317, 1),
"signature" : {
"hash" : BinData(0,"DFAR9j+CHFQiZazFoItzOenSw9g="),
"keyId" : NumberLong("6631044199212908573")
}
}
}
[root@bfd-ango-01 shell]# echo "db.stats()"|mongo 47.105.115.143:27017/app -udolphin -pk8DDg9sd --authenticationDatabase admin
MongoDB shell version v4.0.4
connecting to: mongodb://47.105.115.143:27017/app
Implicit session: session { "id" : UUID("f980ae05-e0ee-4bfe-9ed4-90ed36e0589a") }
MongoDB server version: 4.0.4
{
"raw" : {
"shard3/127.0.0.1:29015,127.0.0.1:29018" : {
"db" : "app",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
},
"shard1/127.0.0.1:29013,127.0.0.1:29016" : {
"db" : "app",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
},
"shard2/127.0.0.1:29014,127.0.0.1:29017" : {
"db" : "app",
"collections" : 5,
"views" : 0,
"objects" : 5052,
"avgObjSize" : 87.85550277117973,
"dataSize" : 443846,
"storageSize" : 311296,
"numExtents" : 0,
"indexes" : 7,
"indexSize" : 282624,
"fsUsedSize" : 7236730880,
"fsTotalSize" : 27510337536,
"ok" : 1
}
},
"objects" : 5052,
"avgObjSize" : 87,
"dataSize" : 443846,
"storageSize" : 311296,
"numExtents" : 0,
"indexes" : 7,
"indexSize" : 282624,
"fileSize" : 0,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"ok" : 1,
"operationTime" : Timestamp(1547451364, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1547451368, 2),
"signature" : {
"hash" : BinData(0,"L6gkXKxh9FgoJzf2PWP785YMkEc="),
"keyId" : NumberLong("6631044199212908573")
}
}
}
到zabbix_agentd.d目录下,创建mongodb_status.conf文件,内容如下图所示
cd /etc/zabbix/zabbix_agentd.d
这是zabbix自定义监控,根据上面的内容可以看出,是根据脚本执行返回的结果进行监控,所以我们还要写shell脚本,脚本内容如下:
#!/bin/bash
MONGODBPATH="/opt/mongodb/bin/mongo"
HOST="47.105.115.143"
PORT="27017"
echo "$1()"|$MONGODBPATH $HOST:$PORT/app -udolphin -pk8DDg9sd --authenticationDatabase admin |grep -w $2|awk -F ':' '{print $2}'|head -n 1| sed "s#,##"
这时候一切都配置好了,我们重启zabbix_agent,例用之前安装的zabbix-get 工具进行检查一下,能够获取到值,则表示ok
[root@bfd-ango-01 shell]# zabbix_get -s 47.105.115.143 -k mongodb_stats[db.serverStatus,totalCreated]
158287
**注意: