Watch keynotes and sessions from, our virtual developer conference.

mongostat data passing in nodejs

how can i pass mongostat data in nodejs backend or is there some function to get mongostat data in nodejs?


The output of mongostat are just post-processed output of db.serverStatus() command.

You can determine which part of serverStatus forms which part of mongostat output from the source (which is in go, but should be quite readable): stat_headers.go

The source also contains which part of serverStatus is used: server_status.go

Note that the raw output of serverStatus is showing absolute numbers. For example, the insert, query, update, delete, getmore, command output:

> db.serverStatus().opcounters
	"insert" : NumberLong(27975),
	"query" : NumberLong(5636),
	"update" : NumberLong(3991),
	"delete" : NumberLong(76),
	"getmore" : NumberLong(262),
	"command" : NumberLong(137933)

However mongostat takes the delta of those numbers each seconds, that’s why it’s showing 0 if there’s no server activity. Which numbers are deltas and which are not should be explained in stat_headers.go.

Quick example using node for getting the opcounters number and the dirty number:

  let res = await conn.db('admin').command({serverStatus: 1})
  let cache_dirty = res.wiredTiger.cache['tracked dirty bytes in the cache']
  let cache_max = res.wiredTiger.cache['maximum bytes configured']
  console.log(100 * cache_dirty / cache_max)

This code in my PC outputs:

  insert: 27975,
  query: 5635,
  update: 3989,
  delete: 73,
  getmore: 262,
  command: 137876

where the 0.75... number corresponds with the dirty number shown in mongostat.

Best regards,

1 Like