From bda5ddcd3bba8bbd758768ac27781daec5ef4b89 Mon Sep 17 00:00:00 2001 From: Tjatse Date: Sun, 27 Dec 2015 22:14:36 +0800 Subject: [PATCH] dashboard with specific socket connection --- lib/blessed-widget/layout.js | 50 ++++++++++++++++++++++++++---------- lib/monitor.js | 42 ++++++++++++------------------ pm2-gui | 9 ++++--- pm2-gui.ini | 1 + pm2-gui.js | 26 ++++++++++++++++--- 5 files changed, 81 insertions(+), 47 deletions(-) diff --git a/lib/blessed-widget/layout.js b/lib/blessed-widget/layout.js index a1d8517..152da43 100644 --- a/lib/blessed-widget/layout.js +++ b/lib/blessed-widget/layout.js @@ -18,7 +18,7 @@ function Layout(options) { if (!(this instanceof Layout)) { return new Layout(options); } - options = options || {}; + options = _.clone(options || {}); if (!options.host) { options.host = '127.0.0.1'; } @@ -40,12 +40,11 @@ Layout.prototype.render = function () { // Preparing all socket.io clients. Object.keys(conf.NSP).forEach(function (ns) { jobs[ns.toLowerCase()] = function (next) { - console.info('connecting to socket.io/' + ns); self._connectSocketIO(conf.NSP[ns], function (err, socket) { if (err) { - console.error('failed due to', err.message); + console.error('Failed due to', err.message); } else { - console.info('successful!'); + console.info('Successful!'); } next(err, socket); }); @@ -172,13 +171,16 @@ Layout.prototype._bindProcesses = function () { * */ Layout.prototype._describeInfo = function (index) { - var jsonData = this._procs.data[index]; - if (jsonData.pm2_env && jsonData.pm2_env.env) { - // Remove useless large-bytes attributes. - delete jsonData.pm2_env.env['LS_COLORS']; + var pm2 = this._dataOf(index); + if (!pm2) { + return this._eles.json.setContent(_formatJSON({message: 'There is no process running!'})); } - delete jsonData.monit; - this._eles.json.setContent(_formatJSON(jsonData)); + if (pm2.pm2_env && pm2.pm2_env.env) { + // Remove useless large-bytes attributes. + delete pm2.pm2_env.env['LS_COLORS']; + } + delete pm2.monit; + this._eles.json.setContent(_formatJSON(pm2)); }; /** @@ -187,7 +189,10 @@ Layout.prototype._describeInfo = function (index) { * */ Layout.prototype._cpuAndMemUsage = function (index) { - var pm2 = this._procs.data[index]; + var pm2 = this._dataOf(index); + if (!pm2) { + return; + } if (!this._usages) { this._usages = { mem: [], @@ -219,8 +224,8 @@ Layout.prototype._cpuAndMemUsage = function (index) { * @return {[type]} [description] */ Layout.prototype._displayLogs = function (index) { - var pm2 = this._procs.data[index]; - if (this._lastLogPMId == pm2.pm_id) { + var pm2 = this._dataOf(index); + if (!pm2 || this._lastLogPMId == pm2.pm_id) { return; } this._killLogs(); @@ -238,6 +243,18 @@ Layout.prototype._killLogs = function () { this._socket(conf.NSP.LOG).emit('tail_kill', this._lastLogPMId); }; +/** + * Get data by index. + * @param {Number} index + * @return {Object} + */ +Layout.prototype._dataOf = function (index) { + if (!this._procs || !Array.isArray(this._procs.data) || index >= this._procs.data.length) { + return null; + } + return this._procs.data[index]; +}; + /** * Draw elements. */ @@ -307,10 +324,15 @@ Layout.prototype._draw = function () { * @param {Function} callback */ Layout.prototype._connectSocketIO = function (ns, callback) { - var socket = io('http://' + this.options.host + ':' + this.options.port + ns); + var serverUri = 'http://' + this.options.host + ':' + this.options.port + ns; + console.info('Connecting to', serverUri); + var socket = io(serverUri); socket.on('connect', function () { callback(null, socket); }); + socket.on('error', function (err) { + console.log(err); + }); }; /** diff --git a/lib/monitor.js b/lib/monitor.js index 4c7f7d3..246d3a6 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -134,21 +134,11 @@ Monitor.prototype.quit = function () { /** * Monitor dashboard. + * @param {Object} options */ -Monitor.prototype.dashboard = function () { - Log({ - level: 1000 - }); - // Socket.io server. - var port = this.options.port; - this.sockio = require('socket.io')(); - this._sockio.listen(port); - this.run(); - +Monitor.prototype.dashboard = function (options) { // Render screen. - layout({ - port: port - }).render(); + layout(options).render(); }; /** @@ -189,7 +179,6 @@ Monitor.prototype._connectSysSock = function (socket) { // Grep system states once and again. (this._status != 'R') && this._nextTick(this.options.refresh || 5000); - console.info('connected to ' + socket.nsp.name + '!'); }; /** @@ -251,7 +240,7 @@ Monitor.prototype._connectLogSock = function (socket) { socket.on('disconnect', self._killTailProcess.bind(self)); socket.on('tail_kill', self._killTailProcess.bind(self)); socket.on('tail', startTailProcess); - console.info('connected to ' + socket.nsp.name + '!'); + console.info('Connected to ' + socket.nsp.name + '!'); }; /** @@ -323,7 +312,7 @@ Monitor.prototype._connectProcSock = function (socket) { socket.on('disconnect', killObserver); socket.on('proc', runObserver); - console.info('connected to ' + socket.nsp.name + '!'); + console.info('Connected to ' + socket.nsp.name + '!'); }; /** @@ -499,14 +488,21 @@ Monitor.prototype._killTailProcess = function (pm_id) { * Listening all the nsp. */ Monitor.prototype._listeningSocketIO = function () { - if (!this._sockio) { + if (!this._sockio || this._sockio._listening) { + console.warn('Avoid duplicated listening!'); return; } - // Listen connection event. - this._sockio.of(conf.NSP.SYS).on('connection', this._connectSysSock.bind(this)); - this._sockio.of(conf.NSP.LOG).on('connection', this._connectLogSock.bind(this)); - this._sockio.of(conf.NSP.PROC).on('connection', this._connectProcSock.bind(this)); + this._sockio._listening = true; + for (var nsp in conf.NSP) { + this._sockio.of(conf.NSP[nsp]).on('connection', this['_connect' + (nsp[0] + nsp.toLowerCase().slice(1)) + 'Sock'].bind(this)); + console.info('Listening connection event on socket.io/' + nsp.toLowerCase()); + } + + this._sockio.use(function (socket, next) { + // console.log(socket.handshake); + next(); + }) }; Object.defineProperty(Monitor.prototype, 'sockio', { @@ -516,10 +512,6 @@ Object.defineProperty(Monitor.prototype, 'sockio', { } this._sockio = io; this._listeningSocketIO(); - this._sockio.use(function (socket, next) { - // console.log(socket.handshake); - next(); - }) }, get: function () { return this._sockio; diff --git a/pm2-gui b/pm2-gui index a145903..623bfcc 100755 --- a/pm2-gui +++ b/pm2-gui @@ -2,7 +2,7 @@ node="$(which node)" pidfile="pm2-gui.pid" prefixINFO="\033[1;32m[INFO]\033[0m" -prefixWARN="\033[1;33m[WARNING]\033[0m" +prefixWARN="\033[1;33m[WARN]\033[0m" function isRunning() { if [ ! -f $pidfile ]; @@ -69,13 +69,14 @@ EOF case "$1" in start) - server "$@"; + server "$@"; # bash < v4.1 did not support ;& ;; - agent) server "$@"; ;; - + mon) + server "$@"; + ;; stop) isRunning; if [ 0 -eq $? ]; diff --git a/pm2-gui.ini b/pm2-gui.ini index 481949f..e7e2bfb 100644 --- a/pm2-gui.ini +++ b/pm2-gui.ini @@ -11,6 +11,7 @@ level = log [agent] authorization = AuTh +offline = false [remotes] pm_loc = AuTh@127.0.0.1:8088 diff --git a/pm2-gui.js b/pm2-gui.js index 4a87b3c..a28b589 100644 --- a/pm2-gui.js +++ b/pm2-gui.js @@ -68,6 +68,10 @@ function startAgent(confFile) { }); var options = monitor.options; + if (options.agent && options.agent.offline) { + console.error('Agent is offline, can not start it.'); + return process.exit(0); + } options.port = options.port || 8088; var sockio = socketIO(); sockio.listen(options.port); @@ -76,13 +80,27 @@ function startAgent(confFile) { } function dashboard(confFile) { + var monitor = slave({ + confFile: confFile + }), + options = monitor.options; + + if (options.agent && options.agent.offline) { + console.error('Agent is offline, can not start it.'); + return process.exit(0); + } + + options.port = options.port || 8088; + var sockio = socketIO(); + sockio.listen(options.port); + monitor.sockio = sockio; +/* Log({ level: 1000 }); - var monitor = slave({ - confFile: confFile - }); - monitor.dashboard(); +*/ + monitor.run(); + monitor.dashboard(options); } function exitGraceful(code, signal) {