Mongodb性能监控

1.mongosniff工具

首先了解一下sniffer的概念,百度百科解释:Sniffer,中文可以翻译为嗅探器,是一种基于被动侦听原理的网络分析方式。使用这种技术方式,可以监视网络的状态、数据流动情况以及网络上传输的信息。

sniffer既可以被犯罪分子利用进行违法活动,也可以被网络管理员利用来进行网络系统的维护和故障排除。目前,它已经被广泛应用于网络故障诊断、协议分析、应用性能分析和网络安全保障等各个领域。

Mongodb提供了一个sniffer工具:mongosniff,这个工具可以从底层监控有哪些命令传送给了Mongodb,启动该工具的语法如下面所示:

./mongosniff --source NET lo

注:lo代表Loopback,指IP数据包回送到本机上。通常使用的是127.0.0.1作为回送地址。

示例:

1)在服务端启动mongosniff

[root@localhost mongodb]# ./bin/mongosniff --source NET lo
sniffing... 27017 

2)在客户端执行操作

> show dbs
admin	0.0625GB
local	(empty)
results	0.0625GB
test	0.0625GB
> 

3)服务端输出了如下的消息

127.0.0.1:51869  -->> 127.0.0.1:27017 admin.$cmd  67 bytes  id:4c	76
	query: { listDatabases: 1.0 }  ntoreturn: -1 ntoskip: 0
127.0.0.1:27017  <<--  127.0.0.1:51869   297 bytes  id:a1	161 - 76
	reply n:1 cursorId: 0
	{ databases: [ { name: "test", sizeOnDisk: 67108864.0, empty: false }, { name: "admin", sizeOnDisk: 67108864.0, empty: false }, { name: "results", sizeOnDisk: 67108864.0, empty: false }, { name: "local", sizeOnDisk: 1.0, empty: true } ], totalSize: 201326592.0, ok: 1.0 }
127.0.0.1:51869  -->> 127.0.0.1:27017 admin.$cmd  80 bytes  id:4d	77
	query: { replSetGetStatus: 1, forShell: 1 }  ntoreturn: 1 ntoskip: 0
127.0.0.1:27017  <<--  127.0.0.1:51869   92 bytes  id:a2	162 - 77
	reply n:1 cursorId: 0
	{ errmsg: "not running with --replSet", ok: 0.0 }

相当于是数据库把系统执行命令的日志都记录了下来,如果我们对这些日志通过文件保存下来,就可以保存数据操作的历史记录,为数据库的性能分析提供原始材料。

2.mongostat

mongostat是Mongodb的一个监控工具,每秒更新一次,通过监控信息可以快速地对数据库进行性能分析。通过mongostat工具可以快速查看运行中的Mongodb实例的统计信息,用法如下:

[root@localhost mongodb]# ./bin/mongostat 
connected to: 127.0.0.1
insert  query update delete getmore command flushes mapped  vsize    res faults locked % idx miss %     qr|qw   ar|aw  netIn netOut  conn       time 
     0      0      0      0       0       1       0    96m   191m    61m      0        0          0       0|0     0|0    62b     1k     2   00:16:30 
     0      0      0      0       0       1       1    96m   191m    61m      0        0          0       0|0     0|0    62b     1k     2   00:16:31 
     0      0      0      0       0       1       0    96m   191m    61m      0        0          0       0|0     0|0    62b     1k     2   00:16:32 

由上可以看出,执行mongostat命令后,输出了系统内部的运行状态,mongostat具体返回的字段如下:

 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
   faults   	- # of pages faults per sec (linux only)
   locked   	- percent of time in global write lock
   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 - bits
   netOut   	- network traffic out - bits
   conn     	- number of open connections
   set      	- replica set name
   repl     	- replication type 
            	    M   - master
            	    SEC - secondary
            	    REC - recovering
            	    UNK - unknown
            	    SLV - slave

3.db.serverStatus命令 

我们可以在客户端执行db.serverStatus命令来查看服务器运行状态,用法如下:

> db.serverStatus()
{
	"host" : "localhost.localdomain",
	"version" : "2.0.6",              #服务器版本
	"process" : "mongod",
	"uptime" : 3937,                  #启动时间
	"uptimeEstimate" : 3232,
	"localTime" : ISODate("2012-08-21T16:21:07.562Z"),
	"globalLock" : {
		"totalTime" : 3937151885,
		"lockTime" : 354898,
		"ratio" : 0.00009014079475879808,
		"currentQueue" : {
			"total" : 0,                #当前全部队列量
			"readers" : 0,           #读请求队列量
			"writers" : 0             #写请求队列量
		},
		"activeClients" : {         
			"total" : 0,                #当前客户端连接量
			"readers" : 0,           #客户端读请求量
			"writers" : 0             #客户端写请求量
		}
	},
	"mem" : {
		"bits" : 32,                       #32位系统
		"resident" : 61,                #占用物理内存量
		"virtual" : 191,                 #占用虚拟内存量
		"supported" : true,          #是否支持扩展内存
		"mapped" : 96
	},
	"connections" : {
		"current" : 1,                    #当前活动连接数
		"available" : 818              #剩余空闲连接数
	},
	"extra_info" : {
		"note" : "fields vary by platform",
		"heap_usage_bytes" : 986232,
		"page_faults" : 1
	},
	"indexCounters" : {
		"btree" : {
			"accesses" : 0,         #索引被访问量
			"hits" : 0,                 #索引命中量
			"misses" : 0,            #索引偏差量
			"resets" : 0,
			"missRatio" : 0         #索引偏差率
		}
	},
	"backgroundFlushing" : {
		"flushes" : 65,
		"total_ms" : 6,
		"average_ms" : 0.09230769230769231,
		"last_ms" : 0,
		"last_finished" : ISODate("2012-08-21T16:20:30.560Z")
	},
	"cursors" : {
		"totalOpen" : 0,
		"clientCursors_size" : 0,
		"timedOut" : 0
	},
	"network" : {
		"bytesIn" : 12264,         #发给服务器的数据量(byte)
		"bytesOut" : 171619,    #此服务器发出的数据量(byte)
		"numRequests" : 193    #发给此服务器的请求量
	},
	"opcounters" : {
		"insert" : 1,                    #插入操作的量
		"query" : 44,                  #查询操作的量
		"update" : 0,                  #更新操作的量
		"delete" : 0,                   #删除操作的量
		"getmore" : 0,
		"command" : 152           #其它操作的量
	},
	"asserts" : {
		"regular" : 0,
		"warning" : 0,
		"msg" : 0,
		"user" : 0,
		"rollovers" : 0
	},
	"writeBacksQueued" : false,
	"ok" : 1
}
> 

db.serverStatus与mongostat命令类似,db.serverStatus提示的信息更加具体、全面,不过db.serverStatus命令查看到的数据是静态的,不是实时的。

更多关于服务器状态监控信息,参见官方文档:http://cn.docs.mongodb.org/master/reference/server-status-index/

4.db.stats命令

与db.serverStatus命令查看服务器实例信息不同,db.stats命令是用来查看特定数据库的详细运行状态,分析粒度更细。具体使用方法如下(目前连接的是test库):

> db.stats()
{
	"db" : "test",               #查看的数据库名称
	"collections" : 19,        #数据库中的集合数
	"objects" : 119,           #对象的数量
	"avgObjSize" : 612.4033613445379,   #对象平均大小
	"dataSize" : 72876,                  #数据大小
	"storageSize" : 1196032,         #占用存储空间大小
	"numExtents" : 21,                   #数据库所有集合中的片区计数
	"indexes" : 21,                         #索引数量
	"indexSize" : 171696,               #索引大小
	"fileSize" : 50331648,               #文件大小
	"nsSizeMB" : 16,                       #数据库命名空间文件的总大小
	"ok" : 1
}
>  

更多关于数据库统计信息,参加官方文档:http://cn.docs.mongodb.org/master/reference/database-statistics/

猜你喜欢

转载自chenzhou123520.iteye.com/blog/1651494