1. REST接口

    MongoDB提供一个简单的REST接口可用于配置监控和告警脚本和其他一些管理任务。

    可以通过在命令行添加 --rest参数或在配置文件加上rest=true开启REST接口支持。

    具体可以参见文档http://docs.mongodb.org/ecosystem/tools/http-interfaces/

  2. HTTP接口

    MongoDB提供一个简单的HTTP接口,如当前MongoDB实例运行的端口是28018,那么可以通过http://127.0.0.1:38018 访问

    具体可以参见文档http://docs.mongodb.org/ecosystem/tools/http-interfaces/

  3. MongoDB数据库命令

    MongoDB提供一些数据库命令可以细粒度的反映MongoDB的运行情况,可以将这些命令的输出结果用于编写自定义监控脚本。

    1)db.currentOp()  返回一个文档记录数据库实例当前正在执行的操作

    db.currentOp()可以不带参数或带一个true参数,带true参数可以显示更多详细的信息,包括空闲的连接和系统操作。

    db.currentOp(true)

    只有具有管理员权限的用户才可以使用db.currentOp()

    可以使用db.killOp()加上opid参数结束掉正在执行的操作,但是这样操作需要特别小心。

    db.currentOp()返回一个包含一个叫inprog数组的文档。

    opid                     表示当前操作的ID

    active                   值为true表示操作已经开始执行,为false表示操作正在排队等待执行

    secs_running             当前操作的持续时间,以秒为单位。从开始操作到当前的时间间隔。

    op                       操作类型,query表示查询操作,insert表示插入操作,update表示更新操作,remove表示删除操作  还可能的值为getmore和command

    ns                       操作的库名和表名

    query                    表示查询的具体操作,getmore,insert和command操作会显示空

    client                   显示客户端连接的IP和端口

    desc                     表示客户端连接的描述信息

    threadId                 处理当前操作的线程ID

    connectionId             当前操作发起的连接ID

    locks                    locks文档报告当前操作持有的锁的类型。

    waitingForLock           值为true表示当前操作正在等待一个锁,值为false表示已经得到需要的锁

    numYields                这个字段表示当前操作让步其他操作完成的次数。通常情况下,当一个操作需要的数据MongoDB还没有全部读入内存时,这个操作会让步于其他需要的数据已经全部读入内存的操作

    lockStats                这里数据可以反映出当前操作花销在获取和持有锁上的时间。根据每个锁的类别显示数据

                       R  代表全局读锁

                       W  代表全局写锁

                       r  代表数据库指定的读锁

                       w  代表数据库指定的写锁

        timeLockedMicros 这个文档记录当前操作在持有一个指定锁上的花销时间

                       R 当前操作持有全局读锁的花销时间,以微妙表示

                       W 当前操作持有全局写锁的花销时间,以微妙表示

                       r 当前操作持有数据库指定读锁的花销时间,以微妙表示

                       w 当前操作持有数据库指定写锁的花销时间,以微妙表示

        timeAcquiringMicros 这个文档记录当前操作在等待一个特定锁的花销时间

                       R 当前操作等待全局读锁的花销时间,以微妙表示

                       W 当前操作等待全局写锁的花销时间,以微妙表示

                       r 当前操作等待数据库指定读锁的花销时间,以微妙表示

                       w 当前操作等待数据库指定写锁的花销时间,以微妙表示


例子

{
	"inprog" : [
		{
			"opid" : 4293208,
			"active" : false,
			"op" : "query",
			"ns" : "",
			"query" : {
				"getlasterror" : 1
			},
			"client" : "10.4.13.130:36112",
			"desc" : "conn105082",
			"threadId" : "0x7feace984700",
			"connectionId" : 105082,
			"waitingForLock" : false,
			"numYields" : 0,
			"lockStats" : {
				"timeLockedMicros" : {
					"R" : NumberLong(0),
					"W" : NumberLong(166),
					"r" : NumberLong(304),
					"w" : NumberLong(0)
				},
				"timeAcquiringMicros" : {
					"R" : NumberLong(0),
					"W" : NumberLong(3),
					"r" : NumberLong(6),
					"w" : NumberLong(0)
				}
			}
		},

      

   2)serverStatus命令 db.serverStatus()

   serverStatus命令可以返回一些关于MongoDB实例状态的信息,有助于了解诊断和性能分析。

    执行db.runCommand('serverStatus') 或 db.serverStatus()

    host               主机名

    version            运行的mongod或mongos实例版本

    process            mongod或mongos

    pid                进程ID

    uptime             启动时间,以秒为单位

    locks              这个文档的字段描述和db.currentOp()中的字段描述相同

    globalLock         报告MongoDB的全局系统锁

                 totalTime  代表数据库从启动并创建globalLock的时间,以微妙表示,这个值粗略等于uptime的时间

                 lockTime   代表数据库从启动,持有globalLock的时间,以微妙表示

                 currentQueue  表示排队等待锁的操作的数量

                           total  排队等待锁的总的操作数量

                           readers  排队等待读锁的操作数量

                           writers  排队等待写锁的操作数量

                 activeClients  连接的客户端数量和操作类型

                           total  执行读操作和写操作的客户端总数

                           readers 执行读操作的客户端总数

                           writers 执行写操作的客户端总数


    mem    显示mongod 当前的内存使用情况

           bits            显示32位还是64位

           resident        数据库进程当前使用的内存,以MB表示

           virtual         数据库进程当前使用的虚拟内存,以MB表示,当开启journaling(日志)功能时,virtual大小至少是mapped的2倍左右,如果virtual的值明显大于mapped的值,如virtual的大小事mapped大小的3倍或更多,说明有内存泄露

           supported       是否支持扩展内存

           mapped          映射内存大小,以MB表示,因为MongoDB使用内存映射文件,这个值大概等于数据库的总大小

           mappedWithJournal  映射的内存大小,包括用于journaling功能的内存

    

   connections    这个文档显示当前连接和可用连接信息

           current         从客户端连接到数据库的数量

           available       还可以使用的连接数量

           totalCreated    数据库总共创建的连接数,包括已经关闭的连接

   extra_info     这个文档显示底层操作系统的一些信息

           note    显示 fields vary by platform

           heap_usage_bytes     数据库进程使用的堆空间的总大小,以字节表示

           page_faults          需要磁盘操作的页面错误的总数量,如果某些操作需要的数据不在活动内存中就会产生页面错误。




   indexCounters        报告索引的状态和使用情况

           accesses     访问索引的次数,和hits和misses的总数。如果这个值不随着时间增长,可能是使用的索引不是很有效

           hits         索引被访问并从内存中返回的次数

           misses       索引被访问,但是不在内存中的次数

           resets       索引计数器重置的次数,通常情况下,值为0

           mi***atio    hits和misses的比率,通常情况下,值为0

   

   

  backgroundFlushing   MongoDB会间隔的刷新写操作到磁盘,默认配置是60秒

           flushes     刷新所有写操作到磁盘的次数

           total_ms    mongod进程花费在写数据到磁盘的总共时间,以毫秒计算

           average_ms  每次写数据到磁盘的平均时间,以毫秒计算

           last_ms     上次刷新写操作数据到磁盘的用时,以毫秒计算

           last_finished  上次刷新写操作数据到磁盘完成的时间戳,如果这个时间比服务器的当前时间旧,重新启动数据库可能会导致一些数据丢失


  cursors     这个文档包含一些游标的使用和状态信息

           totalOpen   MongoDB为客户端维护的游标数量

           clientCursors_size    

           timeOut     过期的游标的数量

 

  

  network  有关MongoDB网络情况的使用

           bytesIn    进入流量

           bytesOut   进出流量

           numRequests   接收到不同请求的总数


  repl     有关复制集的信息

           setName     当前复制集的名称

           isMaster    是否是Master或Primary

           secondary   是否是secondary 

           hosts       复制集中的其他成员信息

  

  opcounters   这个文档显示数据库从启动后各种操作总共的数量

           insert 数据库从启动后插入操作的总数

           query  数据库从启动后查询操作的总数

           update 数据库从启动后更新操作的总数

           delete 数据库从启动后删除操作的总数

           getmore 数据库从启动后执行getmore操作的总数,这里值可能大于query的值,因为secondary发送getmore操作

           command 数据库从启动后执行各种数据库命令的总数

  

  opcountersRepl   复制相关各种操作总共的数量

           insert 数据库从启动后复制插入操作的总数

           query  数据库从启动后复制查询操作的总数

           update 数据库从启动后复制更新操作的总数

           delete 数据库从启动后复制删除操作的总数

           getmore 数据库从启动后执行复制getmore操作的总数,这里值可能大于query的值

           command 数据库从启动后执行复制数据库命令操作的总数

  

  writeBacksQueued 通常值为false

   


  dur   显示日志功能相关操作和性能,mongodb以3秒间隔时间报告数据

        commits         在上一个journal group commit interval之间写入到journal的事务数量

        journaledMB     在上一个journal group commit interval之间写入到journal的数据总量,以MB表示

        writeToDataFilesMB     在上一个journal group commit interval之间从journal写入数据到数据文件的数据总量,以MB表示

        compression      写入到journal的压缩比率

                         journaled_size_of_data / uncompressed_size_of_data 

        commitsInWriteLock   当持有一个写锁的时候,出现commit操作的数量,如果不为0代表MongoDB节点的写操作负载高需要进一步的诊断

  

        earlyCommits     The earlyCommits value reflects the number of times MongoDB requested a commit before the scheduled journal group commit interval

        




 timeMs           提供mongod实例在上一个journal group commit interval的各个阶段中性能信息

                              dt                                MongoDB收集timeMs数据耗费的时间,以毫秒计算

                              prepLogBuffer            准备写数据到journal的耗费时间,以毫秒计算,值越小,journal的性能越好

                              writeToJournal            实际写数据到journal的耗费时间,以毫秒计算,文件系统和设备接口可能会影响性能

                             writeToDataFiles    实际写数据到数据文件的耗费时间,以毫秒计算,文件系统和设备接口可能会影响性能

                            remapPrivateView      重新映射copy-on-write 内存的耗费时间,数字越小代表日志功能性能越小

                  


  recordStats   显示数据库级别的页面错误信息,MongoDB使用一个读锁来返回recordStats的信息,为了减小数据库的压力,可以禁用掉这部分内容显示

                 db.serverStatus( { recordStats: 0 } )

  

                 accessesNotInMemory   mongod进程需要访问一个内存页面不驻留在内存的次数,针对于mongod管理的所有数据库

                 pageFaultExceptionsThrown  mongod进程访问所有数据库的数据抛出页面错误异常的次数

                 然后分别是local和其他数据库的相关信息


  

  workingSet    默认情况下db.serverStatus()命令是不会显示workingSet的信息,除非明显地开启显示,使用

                db.serverStatus( { workingSet: 1 } )或

                db.runCommand( { serverStatus: 1, workingSet: 1 } )

                workingSet可以用来评估工作集的大小,即mongodb活跃使用的数据,mongodb使用一个内部数据结构来跟踪mongod进程的内存访问情况

                报告workingSet可能会影响MongoDB实例上其他操作的性能,因为mongodb必须收集一些一个锁的上下文中的数据

                

                note   

                pagesInMemory  mongod进程在overSeconds这个时间间隔内,mongod进程访问内存页面的总量,默认情况下,一个页面大小是4KB,用这个值乘以4KB就是这些数据在内存中的总量,如果working set的值小于物理内存,pagesInMemory 的值就反映了数据大小

                computationTimeMicros  mongod实例用于计算workingSet中其他字段的时间

                overSeconds   跟踪pagesInMemory最新和最旧的内存页面经过的时间.如果overSeconds数值在减小,或者pagesInMemory接近于物理内存和overSeconds非常小,working set的值可能会远大于物理内存,当overSeconds的值很大,MongoDB的数据集等于或小于物理内存

                

                

     

   metrics    显示大量能够反映当前mongod进程的使用和状态信息的统计数据

           document           反映文档的的访问和修改

           getLastError       反映getLastError的使用情况 

db.serverStatus().metrics.getLastError
{
	"wtime" : {
		"num" : 484,
		"totalMillis" : 576
	},
	"wtimeouts" : NumberLong(0)
}

   

            operation   包含一些MongoDB处理特殊更新或查询操作类型的计数

                    fastmod    记录那些既没有导致文档增加或要求更新索引的操作数量,例如$inc操作

                    idhack           包含_id字段查询的数量

                    scanAndOrder     使用索引无法执行排序操作的返排序后的数字的查询数量

                    

db.serverStatus().metrics.operation
{
	"fastmod" : NumberLong(473645281),
	"idhack" : NumberLong(712767706),
	"scanAndOrder" : NumberLong(38065635)
}

          

         queryExecutor   reports data from the query execution system.

                scanned      reports the total number of index items scanned during queries and query-plan evaluation. This counter is the same as nscanned in the output of explain().

 db.serverStatus().metrics.queryExecutor
{ "scanned" : NumberLong(123146918) }

           

         

db.serverStatus().metrics.record
{ "moves" : NumberLong(391) }

           record     a document that reports data related to record allocation in the on-disk memory files

           moves      moves reports the total number of times documents move within the on-disk representation of the MongoDB data set. Documents move as a result of operations that increase the size of the document beyond their allocated record size.

           





            repl    报告有关复制进程的度量值,所有的mongod进程都会有这个数据,即使不是复制集的成员

                  

                   


          

> db.serverStatus().metrics.repl
{
	"apply" : {
		"batches" : {
			"num" : 10487,
			"totalMillis" : 28729
		},
		"ops" : NumberLong(42483)
	},
	"buffer" : {
		"count" : NumberLong(0),
		"maxSizeBytes" : 268435456,
		"sizeBytes" : NumberLong(0)
	},
	"network" : {
		"bytes" : NumberLong(9037524),
		"getmores" : {
			"num" : 24119,
			"totalMillis" : 10584615
		},
		"ops" : NumberLong(42484),
		"readersCreated" : NumberLong(16)
	},
	"oplog" : {
		"insert" : {
			"num" : 678942519,
			"totalMillis" : 8357729
		},
		"insertBytes" : NumberLong("131171694581")
	},
	"preload" : {
		"docs" : {
			"num" : 37178,
			"totalMillis" : 45049
		},
		"indexes" : {
			"num" : 168494,
			"totalMillis" : 85002
		}
	}
}










                       

PRIMARY> db.serverStatus()
{
	"host" : "0f9699dbc4b1:28018",
	"version" : "2.4.9",
	"process" : "mongod",
	"pid" : 15,
	"uptime" : 2419083,
	"uptimeMillis" : NumberLong("2419082442"),
	"uptimeEstimate" : 2402136,
	"localTime" : ISODate("2014-10-09T09:49:14.386Z"),
	"asserts" : {
		"regular" : 0,
		"warning" : 0,
		"msg" : 0,
		"user" : 76,
		"rollovers" : 0
	},
	"backgroundFlushing" : {
		"flushes" : 40317,
		"total_ms" : 64175,
		"average_ms" : 1.591760299625468,
		"last_ms" : 0,
		"last_finished" : ISODate("2014-10-09T09:48:16.874Z")
	},
	"connections" : {
		"current" : 24,
		"available" : 795,
		"totalCreated" : NumberLong(106136)
	},
	"cursors" : {
		"totalOpen" : 0,
		"clientCursors_size" : 0,
		"timedOut" : 6
	},
	"dur" : {
		"commits" : 30,
		"journaledMB" : 0,
		"writeToDataFilesMB" : 0,
		"compression" : 0,
		"commitsInWriteLock" : 0,
		"earlyCommits" : 0,
		"timeMs" : {
			"dt" : 3066,
			"prepLogBuffer" : 0,
			"writeToJournal" : 0,
			"writeToDataFiles" : 0,
			"remapPrivateView" : 0
		}
	},
	"extra_info" : {
		"note" : "fields vary by platform",
		"heap_usage_bytes" : 67194072,
		"page_faults" : 2893
	},
	"globalLock" : {
		"totalTime" : NumberLong("2419082442000"),
		"lockTime" : NumberLong(92562313),
		"currentQueue" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		},
		"activeClients" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		}
	},
	"indexCounters" : {
		"accesses" : 4593835,
		"hits" : 4593834,
		"misses" : 0,
		"resets" : 0,
		"mi***atio" : 0
	},
	"locks" : {
		"." : {
			"timeLockedMicros" : {
				"R" : NumberLong(43622184),
				"W" : NumberLong(92562313)
			},
			"timeAcquiringMicros" : {
				"R" : NumberLong(29823813),
				"W" : NumberLong(6174897)
			}
		},
		"admin" : {
			"timeLockedMicros" : {
				"r" : NumberLong(21975506),
				"w" : NumberLong(0)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(472073),
				"w" : NumberLong(0)
			}
		},
		"local" : {
			"timeLockedMicros" : {
				"r" : NumberLong(6267650),
				"w" : NumberLong(19032226)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(319982),
				"w" : NumberLong(985298)
			}
		},
		"game_server_1" : {
			"timeLockedMicros" : {
				"r" : NumberLong(52552366),
				"w" : NumberLong(127582978)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(3835069),
				"w" : NumberLong(5748264)
			}
		},
		"account" : {
			"timeLockedMicros" : {
				"r" : NumberLong(8857784),
				"w" : NumberLong(2171916)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(207578),
				"w" : NumberLong(49281)
			}
		},
		"code" : {
			"timeLockedMicros" : {
				"r" : NumberLong(402952),
				"w" : NumberLong(0)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(33449),
				"w" : NumberLong(0)
			}
		},
		"test" : {
			"timeLockedMicros" : {
				"r" : NumberLong(778650),
				"w" : NumberLong(0)
			},
			"timeAcquiringMicros" : {
				"r" : NumberLong(61204),
				"w" : NumberLong(0)
			}
		}
	},
	"network" : {
		"bytesIn" : 492307177,
		"bytesOut" : 5355630338,
		"numRequests" : 3980776
	},
	"opcounters" : {
		"insert" : 18086,
		"query" : 1371383,
		"update" : 1018080,
		"delete" : 29,
		"getmore" : 1673,
		"command" : 1777642
	},
	"opcountersRepl" : {
		"insert" : 0,
		"query" : 0,
		"update" : 0,
		"delete" : 0,
		"getmore" : 0,
		"command" : 0
	},
	"recordStats" : {
		"accessesNotInMemory" : 14,
		"pageFaultExceptionsThrown" : 2,
		"account" : {
			"accessesNotInMemory" : 0,
			"pageFaultExceptionsThrown" : 0
		},
		"admin" : {
			"accessesNotInMemory" : 0,
			"pageFaultExceptionsThrown" : 0
		},
		"code" : {
			"accessesNotInMemory" : 0,
			"pageFaultExceptionsThrown" : 0
		},
		"game_server_1" : {
			"accessesNotInMemory" : 3,
			"pageFaultExceptionsThrown" : 0
		},
		"local" : {
			"accessesNotInMemory" : 8,
			"pageFaultExceptionsThrown" : 0
		},
		"test" : {
			"accessesNotInMemory" : 0,
			"pageFaultExceptionsThrown" : 0
		}
	},
	"repl" : {
		"setName" : "bd33b575-c3f1-4881-a5e2-784fa184beba",
		"ismaster" : true,
		"secondary" : false,
		"hosts" : [
			"10.4.9.112:28018"
		],
		"primary" : "10.4.9.112:28018",
		"me" : "10.4.9.112:28018"
	},
	"writeBacksQueued" : false,
	"mem" : {
		"bits" : 64,
		"resident" : 186,
		"virtual" : 11316,
		"supported" : true,
		"mapped" : 5341,
		"mappedWithJournal" : 10682
	},
	"metrics" : {
		"document" : {
			"deleted" : NumberLong(101),
			"inserted" : NumberLong(18086),
			"returned" : NumberLong(2534892),
			"updated" : NumberLong(1018080)
		},
		"getLastError" : {
			"wtime" : {
				"num" : 12,
				"totalMillis" : 0
			},
			"wtimeouts" : NumberLong(0)
		},
		"operation" : {
			"fastmod" : NumberLong(536606),
			"idhack" : NumberLong(1776607),
			"scanAndOrder" : NumberLong(5756)
		},
		"queryExecutor" : {
			"scanned" : NumberLong(3525325)
		},
		"record" : {
			"moves" : NumberLong(30856)
		},
		"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(3)
			},
			"oplog" : {
				"insert" : {
					"num" : 1036268,
					"totalMillis" : 4329
				},
				"insertBytes" : NumberLong(299823813)
			},
			"preload" : {
				"docs" : {
					"num" : 0,
					"totalMillis" : 0
				},
				"indexes" : {
					"num" : 0,
					"totalMillis" : 0
				}
			}
		},
		"ttl" : {
			"deletedDocuments" : NumberLong(0),
			"passes" : NumberLong(40317)
		}
	},
	"ok" : 1
}