Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve db.admin is not a function error #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ $ pm2 install pm2-mongodb

**NODE** : User should have access to `admin` database to query statistics (see [mongo doc](http://mongodb.github.io/node-mongodb-native/2.2/api/Admin.html))

* `ip` (Defaults to `127.0.0.1`) IP of mongodb server
* `port` (Defaults to `27017`) Port of mongodb server
* `username` (Defaults to `none`) used for authentication
* `password` (Defaults to `none`) used for authentication
* `authDB` (Defaults to `none`) used for authentication
* `MONGODB_URI` (Defaults to `mongodb://localhost:27017/`)
* `DB_NAME` (Defaults to `none`) used for authentication
* `refresh_rate` (Defaults to `5000` in ms): Control the refresh rate of the worker

#### How to set these values ?
Expand Down
98 changes: 46 additions & 52 deletions lib/stats/client.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,54 @@
var MongoClient = require('mongodb').MongoClient;

var client;
var conf;

module.exports.refresh = function refresh(metrics, refresh_rate) {
client.serverStatus().then(function (data) {
metrics.mapped.set(data.mem.mapped);
metrics.vsize.set(data.mem.virtual);
metrics.conn.set(data.connections.current);

if (typeof lastInsert != 'undefined') {
metrics.insert.set(Math.round((data.opcounters.insert - lastInsert) * 1000 / refresh_rate));
metrics.query.set(Math.round((data.opcounters.query - lastQuery) * 1000 / refresh_rate));
metrics.update.set(Math.round((data.opcounters.update - lastUpdate) * 1000 / refresh_rate));
metrics.deleted.set(Math.round((data.opcounters.delete - lastDelete) * 1000 / refresh_rate));
metrics.command.set(Math.round((data.opcounters.command - lastCommand) * 1000 / refresh_rate));
metrics.netIn.set(Math.round((data.network.bytesIn - lastBytesIn) * 1000 / refresh_rate));
metrics.netOut.set(Math.round((data.network.bytesOut - lastBytesOut) * 1000 / refresh_rate));
}
lastInsert = data.opcounters.insert;
lastQuery = data.opcounters.query;
lastUpdate = data.opcounters.update;
lastDelete = data.opcounters.delete;
lastCommand = data.opcounters.command;
lastBytesIn = data.network.bytesIn;
lastBytesOut = data.network.bytesOut;

if (data.repl) {
metrics.replName.set(data.repl.setName);
if (data.repl.ismaster)
metrics.replStatus.set("PRIMARY");
else if (data.repl.secondary)
metrics.replStatus.set("SECONDARY");
else
metrics.replStatus.set("UNKNOWN");
}
module.exports.refresh = function refresh(metrics, refresh_rate) {
MongoClient.connect((process.env.MONGODB_URI || conf.MONGODB_URI) || 'mongodb://localhost:27017/', {
useNewUrlParser: true
}, (err, cl) => {
// Client returned
var db = cl.db(process.env.DB_NAME || conf.DB_NAME);
client = db.admin()
client.serverStatus().then(function (data) {
metrics.mapped.set(data.mem.mapped);
metrics.vsize.set(data.mem.virtual);
metrics.conn.set(data.connections.current);
if (typeof lastInsert != 'undefined') {
metrics.insert.set(Math.round((data.opcounters.insert - lastInsert) * 1000 / refresh_rate));
metrics.query.set(Math.round((data.opcounters.query - lastQuery) * 1000 / refresh_rate));
metrics.update.set(Math.round((data.opcounters.update - lastUpdate) * 1000 / refresh_rate));
metrics.deleted.set(Math.round((data.opcounters.delete - lastDelete) * 1000 / refresh_rate));
metrics.command.set(Math.round((data.opcounters.command - lastCommand) * 1000 / refresh_rate));
metrics.netIn.set(Math.round((data.network.bytesIn - lastBytesIn) * 1000 / refresh_rate));
metrics.netOut.set(Math.round((data.network.bytesOut - lastBytesOut) * 1000 / refresh_rate));
}
lastInsert = data.opcounters.insert;
lastQuery = data.opcounters.query;
lastUpdate = data.opcounters.update;
lastDelete = data.opcounters.delete;
lastCommand = data.opcounters.command;
lastBytesIn = data.network.bytesIn;
lastBytesOut = data.network.bytesOut;

if (data.repl) {
metrics.replName.set(data.repl.setName);
if (data.repl.ismaster)
metrics.replStatus.set("PRIMARY");
else if (data.repl.secondary)
metrics.replStatus.set("SECONDARY");
else
metrics.replStatus.set("UNKNOWN");
}
cl.close()
}).catch(function (err) {
throw err
});
});
};

module.exports.init = function init(conf, done) {
var dbName = process.env.MONGODB_DBNAME || conf.authDB;
var host = process.env.MONGODB_HOST || conf.ip;
var port = process.env.MONGODB_PORT || conf.port;
var username = process.env.MONGODB_USERNAME || conf.username;
var password = process.env.MONGODB_PASSWORD || conf.password;
var login = '';

if (username && password) {
login = username + ':' + password + '@';
}
var url = 'mongodb://' + login + host + ':' + port + '/' + dbName;

MongoClient.connect(url).then(function (db) {
client = db.admin();
done();
}).catch(function (err) {
done(err);
});

};
module.exports.init = function init(config, done) {
conf = config
done()
};
218 changes: 218 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "PM2 MongoDB Module",
"main": "app.js",
"dependencies": {
"pmx": "latest",
"mongodb": "latest"
"pmx": "^1.6.7",
"mongodb": "^3.1.8"
},
"repository": {
"type": "git",
Expand All @@ -19,13 +19,11 @@
"authDB": "",
"refresh_rate": 5000
},
"apps": [
{
"merge_logs": true,
"max_memory_restart": "200M",
"script": "app.js"
}
],
"apps": [{
"merge_logs": true,
"max_memory_restart": "200M",
"script": "app.js"
}],
"author": "Keymetrics Inc.",
"license": "MIT"
}
}