`
LJ你是唯一LT
  • 浏览: 238400 次
社区版块
存档分类
最新评论

mongodb监控

阅读更多
1.mongosniff工具
首先了解一下sniffer的概念,百度百科解释:Sniffer,中文可以翻译为嗅探器,是一种基于被动侦听原理的网络分析方式。使用这种技术方式,可以监视网络的状态、数据流动情况以及网络上传输的信息。
sniffer既可以被犯罪分子利用进行违法活动,也可以被网络管理员利用来进行网络系统的维护和故障排除。目前,它已经被广泛应用于网络故障诊断、协议分析、应用性能分析和网络安全保障等各个领域。
Mongodb提供了一个sniffer工具:mongosniff,这个工具可以从底层监控有哪些命令传送给了Mongodb,启动该工具的语法如下面所示:
./mongosniff --source NET lo
注:lo代表Loopback,指IP数据包回送到本机上。通常使用的是127.0.0.1作为回送地址。
示例:
1)在服务端启动mongosniff
Shell代码  收藏代码
[root@localhost mongodb]# ./bin/mongosniff --source NET lo 
sniffing... 27017  
2)在客户端执行操作
Js代码  收藏代码
> show dbs 
admin   0.0625GB 
local   (empty) 
results 0.0625GB 
test    0.0625GB 
>  
3)服务端输出了如下的消息
Shell代码  收藏代码
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实例的统计信息,用法如下:
Shell代码  收藏代码
[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具体返回的字段如下:
Shell代码  收藏代码
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命令来查看服务器运行状态,用法如下:
Js代码  收藏代码
> 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库):
Js代码  收藏代码
> 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 

>   





---

监控
1、http://192.168.12.107:28021/   比mongos端口大1000
mongos viptest2:27021
List all commands | Replica set status
Commands: buildInfo cursorInfo features hostInfo listDatabases serverStatus top
db version v2.4.6
git hash: nogitversion
sys info: Linux buildvm-04.phx2.fedoraproject.org 3.12.8-300.fc20.x86_64 #1 SMP Thu Jan 16 01:07:50 UTC 2014 x86_64 BOOST_LIB_VERSION=1_41
uptime: 288 seconds

点开每个命令都显示如下:
REST is not enabled.  use --rest to turn on.
check that port 28021 is secured for the network too.


将所有的都关掉,重启启动,mongod命令后面添加 --rest  
例如:mongod --dbpath=/mongo1 --port 27017 --rest
三个分片和配置数据库都可以打开,只有mongos好像打不开
http://192.168.12.104:28017/  
http://192.168.12.104:28019/
http://192.168.12.104:28023/
http://192.168.12.107:28018/

使用管理接口,默认情况下,启动mongod会启动基本的http服务器,该服务的默认端口是28017.可以在浏览器中输入
localhost:28017.有些链接需要在mongod启动时,用--rest选项开启rest支持 才能进去.当开启rest支持后,可以
在mongod启动时使用--nohttpinterface来关闭管理接口.

2、serverStatus
要获得运行中的mongodb服务器统计信息,最基本工具就是serverStatus
mongos> db.runCommand({"serverStatus":1})
{
"host" : "viptest2:27021",
"version" : "2.4.6",
"process" : "mongos",
"pid" : 15353,
"uptime" : 616,
"uptimeMillis" : NumberLong(616245),
"uptimeEstimate" : 609,
"localTime" : ISODate("2014-09-23T09:14:53.601Z"),
"asserts" : {.........

3、mongostat
mongostat输出一些serverStatus提供的重要信息,它会每秒钟输出新的一行,比之前看到的静态计数实时性更好,它输出多个列,分别是inserts  commands  vsize locked
[root@viptest2 ~]# mongostat -h 192.168.12.107 --port 27021
connected to: 192.168.12.107:27021
insert  query update delete getmore command  vsize    res faults  netIn netOut  conn repl       time
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:13
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:14
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:15
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:16
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:17
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:18
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:19
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:20
     0      0      0      0       0       1   227m    16m      0    62b   717b     1  RTR   17:17:21


4、第三方插件
nagios、munin、ganglia、cacti等
------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics