Search code examples
jsonmongodbstatisticsreal-timemonitoring

How could I monitor mongodb in real-time?


I would like to display real time mongodb monitoring statistics on a website. I looked at mongostat, but it just does not seem to provide a real time rest API or any sort of json output. Is there any way I could retrieve real time data from mongostat?

I was thinking about using some tool to stream stdout to a json file, but I thought maybe some of you had another idea.

Thanks in advance!

japel


Solution

  • You've got a couple of options here. You can run mongod with options to expose an http interface that can return some stats in json format. Start mondod with the --httpinterface option and it will expose some stats on the instance at a port 1000 higher than your normal access port. So, if I run:

    mongod --httpinterface
    

    In addition to getting mongodb access at localhost:27017 I'll also get this http interface at localhost:27018.

    For your use case I'd try hitting the serverStatus endpoint - example call and returned status below:

    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 }
    

    Second option would be to enable the REST api inteface for mongod, option is --rest. Documentation on it is here: http://docs.mongodb.org/ecosystem/tools/http-interfaces/ . However, it does open some security holes and it's not recommended for production use: http://docs.mongodb.org/manual/core/security-interface/