标签:mongodb的监控
1、使用管理接口
1、Web控制台
MongoDB在老版本中默认情况下会启动基本的http服务,但是 新版本中已经关闭该选项,要是想用此功能,重启mongod,并加入--httpinterface选项。
[root@gflinux102 bin]# mongod -f /opt/mongo/data/mongod.conf --httpinterface
2015-02-10T15:55:10.423+0800
2015-02-10T15:55:10.424+0800 warning: 32-bit servers don‘t have journaling enabled by default. Please use --journal if you want durability.
2015-02-10T15:55:10.425+0800
about to fork child process, waiting until server is ready for connections.
forked process: 4062
child process started successfully, parent exiting
该服务器监听的端口号比主服务器的端口号大1000。可以查看MongoDB的一些基本信息。比如端口号是10001,则监听的端口号是11001。如果要关闭管理接口,可以在启动时使用--nohttpinterface参数
http://10.100.11.102:11001/
List all commands | Replica set status
Commands: buildInfo cursorInfo features hostInfo isMaster listDatabases replSetGetStatus serverStatus top
db version v2.6.6 git hash: 608e8bc319627693b04cc7da29ecc300a5f45a1f sys info: Linux ip-10-123-131-43 2.6.18-194.el5xen #1 SMP Tue Mar 16 22:08:06 EDT 2010 i686 BOOST_LIB_VERSION=1_49 uptime: 41 seconds
overview (only reported if can acquire read lock quickly)
time to get readlock: 0ms # databases: 2 # Cursors: 0 replication: master: 0 slave: 0
clients
Client | OpId | Locking | Waiting | SecsRunning | Op | Namespace | Query | client | msg | progress |
---|---|---|---|---|---|---|---|---|---|---|
DataFileSync | 1 | { waitingForLock: false } | 0 | :27017 | ||||||
signalProcessingThread | 0 | { waitingForLock: false } | 0 | :27017 | ||||||
initandlisten | 8 | { waitingForLock: false } | 2002 | local.startup_log | 0.0.0.0:0 | |||||
TTLMonitor | 6 | { waitingForLock: false } | 0 | :27017 | ||||||
snapshotthread | 4 | { waitingForLock: false } | 0 | :27017 | ||||||
clientcursormon | 5 | { waitingForLock: false } | 0 | :27017 | ||||||
RangeDeleter | 9 | { waitingForLock: false } | 0 | :27017 | ||||||
websvr | 11 | { waitingForLock: false } | 0 | admin.system.users | :27017 |
dbtop (occurrences|percent of elapsed)
NS | total | Reads | Writes | Queries | GetMores | Inserts | Updates | Removes | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TOTAL | 3 | 0.0% | 3 | 0.0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% |
3 | 0.0% | 3 | 0.0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% | 0 | 0% |
write lock % time in write lock, by 4 sec periods
0 0 0 0 0 0 0 0 0 0
write locked now: false
Log
2015-02-10T15:55:10.453+0800 [initandlisten] MongoDB starting : pid=4062 port=10001 dbpath=/opt/mongo/data 32-bit host=gflinux102 15:55:10.454+0800 [initandlisten] 15:55:10.454+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. 15:55:10.454+0800 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal). 15:55:10.454+0800 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off. 15:55:10.454+0800 [initandlisten] ** See http://dochub.mongodb.org/core/32bit 15:55:10.454+0800 [initandlisten] 15:55:10.454+0800 [initandlisten] db version v2.6.6 15:55:10.454+0800 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f 15:55:10.454+0800 [initandlisten] build info: Linux ip-10-123-131-43 2.6.18-194.el5xen #1 SMP Tue Mar 16 22:08:06 EDT 2010 i686 BOOST_LIB_VERSION=1_49 15:55:10.454+0800 [initandlisten] allocator: system 15:55:10.454+0800 [initandlisten] options: { config: "/opt/mongo/data/mongod.conf", net: { http: { enabled: true }, port: 10001 }, processManagement: { fork: true }, storage: { dbPath: "/opt/mongo/data" }, systemLog: { destination: "file", logAppend: true, path: "/opt/mongo/logs/mongodb.log" } } 15:55:10.524+0800 [initandlisten] waiting for connections on port 10001 15:55:10.549+0800 [websvr] admin web console waiting for connections on port 11001 15:55:31.814+0800 [initandlisten] connection accepted from 10.100.11.1:60455 #1 (1 connection now open) 15:55:47.013+0800 [conn1] end connection 10.100.11.1:60455 (0 connections now open)
2、获取MongoDB服务器的统计信息(serverStatus)
要获取运行中的MongoDB服务器的统计信息,最基本工具就是serverStatus命令:
> db.runCommand({"serverStatus":1})
或者
> db.serverStatus()
{
"host" : "gflinux102:10001",
"version" : "2.6.6",
"process" : "mongod",
"pid" : NumberLong(4062),
"uptime" : 2898,
"uptimeMillis" : NumberLong(2898078),
"uptimeEstimate" : 2883,
"localTime" : ISODate("2015-02-10T08:43:28.491Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 0,
"rollovers" : 0
},
"backgroundFlushing" : {
"flushes" : 48,
"total_ms" : 0,
"average_ms" : 0,
"last_ms" : 0,
"last_finished" : ISODate("2015-02-10T08:43:10.526Z")
},
"connections" : {
"current" : 1,
"available" : 818,
"totalCreated" : NumberLong(14)
},
"cursors" : {
"note" : "deprecated, use server status metrics",
"clientCursors_size" : 0,
"totalOpen" : 0,
"pinned" : 0,
"totalNoTimeout" : 0,
"timedOut" : 0
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 23439096,
"page_faults" : 0
},
"globalLock" : {
"totalTime" : NumberLong("2898079000"),
"lockTime" : NumberLong(19959),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 0,
"readers" : 0,
"writers" : 0
}
},
"indexCounters" : {
"accesses" : 2,
"hits" : 2,
"misses" : 0,
"resets" : 0,
"missRatio" : 0
},
"locks" : {
"." : {
"timeLockedMicros" : {
"R" : NumberLong(132),
"W" : NumberLong(19959)
},
"timeAcquiringMicros" : {
"R" : NumberLong(45),
"W" : NumberLong(70)
}
},
"admin" : {
"timeLockedMicros" : {
"r" : NumberLong(7286),
"w" : NumberLong(0)
},
"timeAcquiringMicros" : {
"r" : NumberLong(1503),
"w" : NumberLong(0)
}
},
"local" : {
"timeLockedMicros" : {
"r" : NumberLong(109992),
"w" : NumberLong(94)
},
"timeAcquiringMicros" : {
"r" : NumberLong(15939),
"w" : NumberLong(11)
}
}
},
"network" : {
"bytesIn" : 9622,
"bytesOut" : 355275,
"numRequests" : 150
},
"opcounters" : {
"insert" : 1,
"query" : 97,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 151
},
"opcountersRepl" : {
"insert" : 0,
"query" : 0,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 0
},
"recordStats" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0,
"admin" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0
},
"local" : {
"accessesNotInMemory" : 0,
"pageFaultExceptionsThrown" : 0
}
},
"writeBacksQueued" : false,
"mem" : {
"bits" : 32,
"resident" : 39,
"virtual" : 241,
"supported" : true,
"mapped" : 80
},
"metrics" : {
"cursor" : {
"timedOut" : NumberLong(0),
"open" : {
"noTimeout" : NumberLong(0),
"pinned" : NumberLong(0),
"total" : NumberLong(0)
}
},
"document" : {
"deleted" : NumberLong(0),
"inserted" : NumberLong(1),
"returned" : NumberLong(0),
"updated" : NumberLong(0)
},
"getLastError" : {
"wtime" : {
"num" : 0,
"totalMillis" : 0
},
"wtimeouts" : NumberLong(0)
},
"operation" : {
"fastmod" : NumberLong(0),
"idhack" : NumberLong(0),
"scanAndOrder" : NumberLong(0)
},
"queryExecutor" : {
"scanned" : NumberLong(0),
"scannedObjects" : NumberLong(0)
},
"record" : {
"moves" : NumberLong(0)
},
"repl" : {
"apply" : {
"batches" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0)
},
"buffer" : {
"count" : NumberLong(0),
"maxSizeBytes" : 268435456,
"sizeBytes" : NumberLong(0)
},
"network" : {
"bytes" : NumberLong(0),
"getmores" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0),
"readersCreated" : NumberLong(0)
},
"preload" : {
"docs" : {
"num" : 0,
"totalMillis" : 0
},
"indexes" : {
"num" : 0,
"totalMillis" : 0
}
}
},
"storage" : {
"freelist" : {
"search" : {
"bucketExhausted" : NumberLong(0),
"requests" : NumberLong(0),
"scanned" : NumberLong(0)
}
}
},
"ttl" : {
"deletedDocuments" : NumberLong(0),
"passes" : NumberLong(48)
}
},
"ok" : 1
}
>
原始的统计信息同样可以有http接口以Json的形式得到,位置在/_status(http://10.100.11.102:11001/_status)。
serverStatus呈现了MongoDB内部的详细信息,比如当前服务器版本、运行时间、当前连接数等。
"globalLock"的值表示全局写入锁占用了服务器多少时间(单位微秒)
"mem"包含服务器内存映射了多少数据,服务器进程的虚拟内存和常驻内存的占用情况(单位MB)
"indexCounters"表示B树在磁盘检索("misses")和内存检索("hits")的次数.如果这个比值开始上升,就要考虑加内存了.
"backgroundFlushing"表示后台做了多少次fsync以及用了多少时间
"opcounters"文档包含了每种主要操作的次数
"asserts"统计了断言的次数
3、获取当前数据库的信息,比如obj总数、数据库总大小和平均obj。
> use test;
switched to db test
> db.stats()
{
"db" : "test",
"collections" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"dataFileVersion" : {
},
"ok" : 1
}
>
4、获取当前正在执行的操作
> db.currentOp()
{ "inprog" : [ ] }
>
Mongodb 的命令一般很快就完成,但是在一台繁忙的机器或者有比较慢的命令时,你可以通过db.currentOp()获取当前正在执行的操作。
在没有负载的机器上,该命令基本上都是返回空的
以下是一个有负载的机器上得到的返回值样例:
> db.currentOp()
{ "opid" : "shard3:466404288", "active" : false, "waitingForLock" : false, "op" : "query", "ns" : "sd.usersEmails", "query" : { }, "client_s" : "10.121.13.8:34473", "desc" : "conn" },
如果你发现一个操作太长,把数据库卡死的话,可以用这个命令杀死他
> db.killOp("shard3:466404288")
5、使用profiling
似于MySQL的slow log, MongoDB可以监控所有慢的以及不慢的查询。Profiler默认是关闭的,你可以选择全部开启,或者有慢查询的时候开启。
> use test
switched to db test
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "ok" : 1 }
> db.getProfilingLevel()
2
查看Profile日志
> db.system.profile.find().sort({$natural:-1})
{"ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" :
"query test.$cmd ntoreturn:1 reslen:66 nscanned:0 query: { profile: 2 } nreturned:1 bytes:50" ,
"millis" : 0} ...
3个字段的意义
ts:时间戳
info:具体的操作
millis:操作所花时间,毫秒
注意,造成满查询可能是索引的问题,也可能是数据不在内存造成因此磁盘读入造成。
http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
6、mongostat
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 时间戳
[root@gflinux102 bin]# ./mongostat --port 10001
connected to: 127.0.0.1:10001
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:41
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 admin:0.0% 0 0|0 0|0 62b 2k 2 17:41:42
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:43
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:44
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:45
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:46
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:47
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:48
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:49
*0 *0 *0 *0 0 1|0 0 160m 322m 41m 0 test:0.0% 0 0|0 0|0 62b 2k 2 17:41:50
7、MMS
MongoDB Monitoring Service(MMS)是Mongodb厂商提供的监控服务,可以在网页和Android客户端上监控你的MongoDB状况。
标签:mongodb的监控
原文地址:http://gfsunny.blog.51cto.com/990565/1613421