diff --git a/lib/API/Monit.js b/lib/API/Monit.js index ed2dff80..9e5a3018 100644 --- a/lib/API/Monit.js +++ b/lib/API/Monit.js @@ -11,6 +11,7 @@ var multimeter = require('pm2-multimeter'); var os = require('os'); var p = require('path'); var chalk = require('chalk'); +var blessed = require('blessed'); var CliUx = require('./CliUx'); @@ -26,6 +27,7 @@ const RATIO_T3 = Math.floor(os.totalmem() / 5); const RATIO_T4 = Math.floor(os.totalmem()); var Monit = {}; +var totalMem = os.totalmem(); //helper to get bars.length (num bars printed) Object.size = function(obj) { @@ -45,7 +47,7 @@ Monit.reset = function(msg) { this.multi.charm.reset(); - this.multi.write('\x1B[32m⌬ PM2 \x1B[39mmonitoring\x1B[96m (To go further check out https://app.keymetrics.io) \x1B[39m\n\n'); + //this.multi.write('\x1B[32m⌬ PM2 \x1B[39mmonitoring\x1B[96m (To go further check out https://app.keymetrics.io) \x1B[39m\n\n'); if(msg) { this.multi.write(msg); @@ -62,6 +64,69 @@ Monit.reset = function(msg) { * @return Monit */ Monit.init = function() { + this.screen = blessed.screen({ + smartCSR: true + }); + this.screen.title = 'PM2 Monitor'; + + this.box1 = blessed.box({ + top: '0', + left: '0', + width: '60%', + height: '100%', + tags: true, + scrollbar: true, + border: { + type: 'line' + }, + style: { + scrollbar: { + bg: 'black', + fg: 'red' + }, + fg: 'white', + border: { + fg: '#f0f0f0' + } + } + }); + this.box2 = blessed.box({ + top: '0%', + left: '60%', + width: '40%', + height: '50%', + tags: true, + border: { + type: 'line' + }, + style: { + fg: 'white', + border: { + fg: '#f0f0f0' + } + } + }); + this.box3 = blessed.box({ + top: '50%', + left: '60%', + width: '40%', + height: '50%', + tags: true, + border: { + type: 'line' + }, + style: { + fg: 'white', + border: { + fg: '#f0f0f0' + } + } + }); + + this.screen.append(this.box1); + this.screen.append(this.box2); + this.screen.append(this.box3); + this.screen.render(); this.multi = multimeter(process); @@ -78,6 +143,7 @@ Monit.init = function() { */ Monit.stop = function() { this.multi.charm.destroy(); + this.screen.destroy(); process.exit(0); } @@ -92,39 +158,83 @@ Monit.refresh = function(processes) { debug('Monit refresh'); if(!processes) { - processes = []; + this.box1.setLine(0, 'No process available'); + this.screen.render(); + return; } - var num = processes.length; - this.num_bars = Object.size(this.bars); + this.box3.setLine(0, '{center}{bold}Server{/bold}{/center}'); - if(num !== this.num_bars) { - debug('Monit addProcesses - actual: %s, new: %s', this.num_bars, num); - return this.addProcesses(processes); - } else { - - if(num === 0) { - return; - } - - debug('Monit refresh'); - var proc; - - for(var i = 0; i < num; i++) { - proc = processes[i]; - - //this is to avoid a print issue when the process is restarted for example - //we might also check for the pid but restarted|restarting will be rendered bad - if(this.bars[proc.pm_id] && proc.pm2_env.status !== this.bars[proc.pm_id].status) { - debug('bars for %s does not exists', proc.pm_id); - this.addProcesses(processes); - break; - } - - this.updateBars(proc); - - } + var cpus = os.cpus(); + for (var i = 0; i < cpus.length; i++) { + this.box3.setLine(i + 2, 'Core ' + (i + 1) + '{|}' + cpus[i].times.idle); } + this.box3.setLine(i + 2, 'Memory{|}' + String(Math.round(((totalMem - os.freemem()) / 1000000))) + ' MB'); + + this.memBar = blessed.ProgressBar({ + parent: this.box2, + top: '5%', + left: '65%', + orientation: 'vertical', + width: '10%', + height: '40%', + barBg: 'green' + }); + this.cpuBar = blessed.ProgressBar({ + parent: this.box2, + top: '5%', + left: '85%', + orientation: 'vertical', + width: '10%', + height: '40%', + barBg: 'red' + }); + this.screen.append(this.memBar); + this.screen.append(this.cpuBar); + + this.box1.setLine(0, '{center}{bold}PM2 Monitoring{/bold}{/center}'); + for (var i = 0; i < processes.length; i++) { + var memPercent = (processes[i].monit.memory) * 100 / totalMem; + //console.log(Math.round(memPercent * 10)) + var status = processes[i].pm2_env.status == 'online' ? '{green-fg}' : '{red-fg}'; + status = status + '{bold}' + processes[i].pm2_env.status + '{/}'; + + this.box1.setLine(i + 2, '[' + String(processes[i].pm2_env.pm_id) + '] ' + processes[i].pm2_env.name + '{|}' + status + ' ' + processes[i].pm2_env.exec_mode); + this.memBar.setProgress(Math.round(memPercent * 100)); + this.cpuBar.setProgress(processes[i].monit.cpu); + this.screen.render(); + } + // var num = processes.length; + // this.num_bars = Object.size(this.bars); + + // if(num !== this.num_bars) { + // debug('Monit addProcesses - actual: %s, new: %s', this.num_bars, num); + // return this.addProcesses(processes); + // } else { + + // if(num === 0) { + // return; + // } + + // debug('Monit refresh'); + + // var proc; + + // for(var i = 0; i < num; i++) { + // proc = processes[i]; + + // //this is to avoid a print issue when the process is restarted for example + // //we might also check for the pid but restarted|restarting will be rendered bad + // if(this.bars[proc.pm_id] && proc.pm2_env.status !== this.bars[proc.pm_id].status) { + // debug('bars for %s does not exists', proc.pm_id); + // this.addProcesses(processes); + // break; + // } + + // this.updateBars(proc); + + // } + // } return this; }