我怎么能实时监控mongodb?

时间:2014-05-23 12:38:51

标签: json mongodb statistics real-time monitoring

我想在网站上显示实时mongodb监控统计信息。 我看了mongostat,但它似乎没有提供实时休息API或任何类型的json输出。 有什么方法可以从mongostat中检索实时数据吗?

我正在考虑使用一些工具将stdout传输到json文件,但我想也许你们中的一些人有另一个想法。

提前致谢!

japel

1 个答案:

答案 0 :(得分:1)

你在这里有几个选择。您可以使用选项运行mongod以公开可以以json格式返回某些统计信息的http接口。使用--httpinterface选项启动mondod,它将在比正常访问端口高1000的端口上公开实例上的一些统计信息。所以,如果我跑:

mongod --httpinterface

除了在localhost:27017获取mongodb访问权限外,我还将在localhost:27018获得此http接口。

对于您的用例,我尝试点击serverStatus端点 - 示例调用并返回以下状态:

http://localhost:28017/serverStatus?text=1

{ "host" : "myhost",
  "version" : "2.6.0",
  "process" : "mongod",
  "pid" : { "$numberLong" : "2871" },
  "uptime" : 27,
  "uptimeMillis" : { "$numberLong" : "27468" },
  "uptimeEstimate" : 25,
  "localTime" : { "$date" : "2014-05-23T07:25:05.793-0700" },
  "asserts" : { "regular" : 0,
    "warning" : 0,
    "msg" : 0,
    "user" : 0,
    "rollovers" : 0 },
  "backgroundFlushing" : { "flushes" : 0,
    "total_ms" : 0,
    "average_ms" : 0,
    "last_ms" : 0,
    "last_finished" : { "$date" : "1969-12-31T16:00:00.000-0800" } },
  "connections" : { "current" : 0,
    "available" : 26214,
    "totalCreated" : { "$numberLong" : "4" } },
  "cursors" : { "note" : "deprecated, use server status metrics",
    "clientCursors_size" : 0,
    "totalOpen" : 0,
    "pinned" : 0,
    "totalNoTimeout" : 0,
    "timedOut" : 0 },
  "dur" : { "commits" : 29,
    "journaledMB" : 0,
    "writeToDataFilesMB" : 0,
    "compression" : 0,
    "commitsInWriteLock" : 0,
    "earlyCommits" : 0,
    "timeMs" : { "dt" : 3013,
      "prepLogBuffer" : 0,
      "writeToJournal" : 0,
      "writeToDataFiles" : 0,
      "remapPrivateView" : 0 } },
  "extra_info" : { "note" : "fields vary by platform",
    "page_faults" : 12 },
  "globalLock" : { "totalTime" : { "$numberLong" : "27468000" },
    "lockTime" : { "$numberLong" : "476591" },
    "currentQueue" : { "total" : 0,
      "readers" : 0,
      "writers" : 0 },
    "activeClients" : { "total" : 0,
      "readers" : 0,
      "writers" : 0 } },
  "indexCounters" : { "accesses" : 0,
    "hits" : 0,
    "misses" : 0,
    "resets" : 0,
    "missRatio" : 0 },
  "locks" : { "." : { "timeLockedMicros" : { "R" : { "$numberLong" : "1104" },
        "W" : { "$numberLong" : "476591" } },
      "timeAcquiringMicros" : { "R" : { "$numberLong" : "351819" },
        "W" : { "$numberLong" : "2178" } } },
    "admin" : { "timeLockedMicros" : { "r" : { "$numberLong" : "96" },
        "w" : { "$numberLong" : "0" } },
      "timeAcquiringMicros" : { "r" : { "$numberLong" : "6" },
        "w" : { "$numberLong" : "0" } } },
    "local" : { "timeLockedMicros" : { "r" : { "$numberLong" : "6082" },
        "w" : { "$numberLong" : "11" } },
      "timeAcquiringMicros" : { "r" : { "$numberLong" : "15" },
        "w" : { "$numberLong" : "2" } } },
     <LIST OF DATABASES REMOVED>
  "network" : { "bytesIn" : 0,
    "bytesOut" : 0,
    "numRequests" : 0 },
  "opcounters" : { "insert" : 1,
    "query" : 1,
    "update" : 0,
    "delete" : 0,
    "getmore" : 0,
    "command" : 2 },
  "opcountersRepl" : { "insert" : 0,
    "query" : 0,
    "update" : 0,
    "delete" : 0,
    "getmore" : 0,
    "command" : 0 },
  "recordStats" : { "accessesNotInMemory" : 0,
    "pageFaultExceptionsThrown" : 0,
    "admin" : { "accessesNotInMemory" : 0,
      "pageFaultExceptionsThrown" : 0 },
   <LIST OF DATABASES REMOVED>
  "writeBacksQueued" : false,
  "mem" : { "bits" : 64,
    "resident" : 162,
    "virtual" : 5762,
    "supported" : true,
    "mapped" : 1616,
    "mappedWithJournal" : 3232 },
  "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" : "0" } } },
  "ok" : 1 }

第二个选项是为mongod启用REST api接口,选项为--rest。有关它的文档在这里:http://docs.mongodb.org/ecosystem/tools/http-interfaces/。但是,它确实会打开一些安全漏洞,不建议用于生产:http://docs.mongodb.org/manual/core/security-interface/