MongoDB自带了mongostat 和 mongotop 这两个命令来监控MongoDB的运行情况。这两个命令对于我们处理MongoDB数据库变慢等等问题非常有用,能详细的统计MongoDB当前的状态信息。除此之外,还可以用db.serverStatus()、db.stats()、开启profile功能通过查看日志进行监控分析。
一、mongotopmongotop用来跟踪MongoDB的实例,提供每个集合的统计数据。默认情况下,mongotop每一秒刷新一次。
./bin/mongotop --help View live MongoDB collection statistics. Options: --help 显示帮助信息 -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --quiet silence all non error diagnostic messages --version 版本号 -h [ --host ] arg 主机地址( <set name>/s1,s2 for sets) --port arg 服务端口,也可以使用 --host hostname:port --ipv6 开启IPV6,默认关闭 -u [ --username ] arg 用户名 -p [ --password ] arg 密码 --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authentication --locks 查看数据库锁的情况如:
ns total read write 2015-03-21T04:27:30 User.User.user 0ms 0ms 0ms User.system.indexes 0ms 0ms 0ms User.system.namespaces 0ms 0ms 0ms User.user 0ms 0ms 0ms admin.system.indexes 0ms 0ms 0ms admin.system.roles 0ms 0ms 0ms admin.system.users 0ms 0ms 0ms输出字段说明:
二、mongostat
它每秒钟刷新一次状态值,提供良好的可读性,通过这些参数可以观察到一个整体的性能情况。
./bin/mongostat --help View live MongoDB performance statistics. usage: mongostat [options] [sleep time] sleep time: time to wait (in seconds) between calls Options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --quiet silence all non error diagnostic messages --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authentication --noheaders don't output column names -n [ --rowcount ] arg (=0) number of stats lines to print (0 for indefinite) --http use http instead of raw db connection --discover discover nodes and display stats for all --all all optional fields Fields inserts - # of inserts per second (* means replicated op) query - # of queries per second update - # of updates per second delete - # of deletes per second getmore - # of get mores (cursor batch) per second command - # of commands per second, on a slave its local|replicated flushes - # of fsync flushes per second mapped - amount of data mmaped (total data size) megabytes vsize - virtual size of process in megabytes res - resident size of process in megabytes non-mapped - amount virtual memeory less mapped memory (only with --all) faults - # of pages faults per sec locked - name of and percent time for most locked database idx miss - percent of btree page misses (sampled) qr|qw - queue lengths for clients waiting (read|write) ar|aw - active clients (read|write) netIn - network traffic in - bytes netOut - network traffic out - bytes conn - number of open connections set - replica set name repl - replication type PRI - primary (master) SEC - secondary REC - recovering UNK - unknown SLV - slave b RTR - mongos process ("router")如:
./mongostat --host 192.168.100.90:10000 -vvvvv insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn set repl time *0 *0 *0 *0 0 2|0 0 448m 3.22g 74m 0 test:0.0% 0 0|0 0|0 120b 4k 26 shard_a PRI 17:43:51 *0 *0 *0 *0 0 3|0 0 448m 3.22g 74m 0 local:0.0% 0 0|0 0|0 353b 4k 26 shard_a PRI 17:43:52 *0 *0 *0 *0 1 2|0 0 448m 3.22g 74m 0 test:0.0% 0 0|0 0|0 167b 4k 26 shard_a PRI 17:43:53字段说明:
三、实时监控工具motop
mongodb实时监控工具,可以同时对多个MongoDB服务器进行监控。显示当前操作。
项目地址:https://github.com/tart/motop
1.安装
依赖包
python 2.6 或以上
pymongo 2.0 或以上
1)使用easy_install安装
# wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
# easy_install motop
2)源码安装
# wget https://github.com/tart/motop/archive/master.zip
# unzip master.zip
# ./setup.py install
3.帮助
# motop -h
4.监控多台
# motop 192.168.124.50 192.158.124.51
动作:
q Quit
p Pause
e Explain the query
k Kill operation using “mongo” executable
K Kill operations older than given seconds using “mongo” executable
r Try to reconnect to disconnected servers
R Try to reconnect to all servers
5.配置
配置文件可以创建在/etc/motop.conf。可以有多个配置短,每一节都可以包含以下参数:
address: 服务器的地址(必需)
username: 登陆用户名
password:登陆用户密码
status:显示状态(默认开启)
replicationInfo :显示复制状态(默认值:开启)
replicaSet :显示副本集的状态(默认值:开启)
operations:显示操作(默认值:开启)
replicationOperations :不断展现主和从的复制操作(默认值:开启)
“DEFAULT”是特殊的部分。本节参数可以设置为默认。
配置实例:
[MongoDB01]
address=10.42.2.121
replicationOperations=off
[MongoDB02]
address=10.42.2.122
[MongoDB03]
address=10.42.2.123
[MongoDB04]
address=10.42.2.124
username=foo
password=bar