From 39a70285187c7b8bce87a445571ca0013274421e Mon Sep 17 00:00:00 2001 From: James Hall Date: Thu, 19 Jun 2014 20:24:31 +0100 Subject: [PATCH 1/3] Early integration attempt for current-process lib #35 --- package.json | 4 ++- sensors/process.js | 71 ++++++++++++---------------------------------- 2 files changed, 21 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index 7a9e737..edeb66c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ "read": "1.0.5", "blessed": "0.0.33", "commander": "2.2.0", - "sudo": "1.0.3" + "sudo": "1.0.3", + "current-processes": "0.1.0", + "lodash": "^2.4.1" } } diff --git a/sensors/process.js b/sensors/process.js index b251088..d169761 100644 --- a/sensors/process.js +++ b/sensors/process.js @@ -7,7 +7,9 @@ var os = require('os'), fs = require('fs'), - child_process = require('child_process'); + child_process = require('child_process'), + _ = require('lodash'), + ps = require('current-processes'); var plugin = { /** @@ -50,62 +52,25 @@ var plugin = { */ poll: function() { var stats = {}; - // @todo If you can think of a better way of getting process stats, - // then please feel free to send me a pull request. This is version 0.1 - // and needs some love. - var ps = child_process.exec('ps -ewwwo %cpu,%mem,comm', function (error, stdout, stderr) { - var lines = stdout.split("\n"); - // Ditch the first line - lines[0] = ''; - for (var line in lines) { - var currentLine = lines[line].trim().replace(' ', ' '); - //console.log(currentLine); - var words = currentLine.split(" "); - if (typeof words[0] !== 'undefined' && typeof words[1] !== 'undefined' ) { - var cpu = words[0].replace(',', '.'); - var mem = words[1].replace(',', '.'); - var offset = cpu.length + mem.length + 2; - var comm = currentLine.slice(offset); - // If we're on Mac then remove the path - if (/^darwin/.test(process.platform)) { - comm = comm.split('/'); - comm = comm[comm.length - 1]; - } else { - // Otherwise assume linux and remove the unnecessary /1 info like - // you get on kworker - comm = comm.split('/'); - comm = comm[0]; - } - // If already exists, then add them together - if (typeof stats[comm] !== 'undefined') { - stats[comm] = { - cpu: parseFloat(stats[comm].cpu, 10) + parseFloat(cpu), - mem: parseFloat(stats[comm].mem, 10) + parseFloat(mem), - comm: comm, - count: parseInt(stats[comm].count, 10) + 1 - }; - } else { - stats[comm] = { - cpu: cpu, - mem: mem, - comm: comm, - count: 1 - }; - } - } - } + + // This uses the https://github.com/branneman/current-processes + // written by @branneman to factor this code out, and support multiple OS + // adapters. + ps.get(function(err, processes) { var statsArray = []; - for (var stat in stats) { - // Divide by nuber of CPU cores - var cpuRounded = parseFloat(stats[stat].cpu / os.cpus().length).toFixed(1); - var memRounded = parseFloat(stats[stat].mem).toFixed(1); + + //console.log(processes); + for (var p in processes) { + var process = processes[p]; + var cpuRounded = parseFloat(process.cpu / os.cpus().length).toFixed(1); + var memRounded = parseFloat(process.mem).toFixed(1); statsArray.push({ - 'Command': stats[stat].comm, - 'Count': stats[stat].count, + 'Command': process.name, + 'Count': 1, 'CPU %': cpuRounded, 'Memory %': memRounded, - 'cpu': stats[stat].cpu, - 'mem': stats[stat].mem // exact cpu for comparison + 'cpu': process.cpu, + 'mem': process.mem // exact cpu for comparison }); } statsArray.sort(function(a, b) { From 434bf4ce5497fdebf01dd6192688f30d36ec4d90 Mon Sep 17 00:00:00 2001 From: James Hall Date: Thu, 19 Jun 2014 20:34:52 +0100 Subject: [PATCH 2/3] Make sure they get added together in the same way #35 --- sensors/process.js | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/sensors/process.js b/sensors/process.js index d169761..c3dced6 100644 --- a/sensors/process.js +++ b/sensors/process.js @@ -8,7 +8,6 @@ var os = require('os'), fs = require('fs'), child_process = require('child_process'), - _ = require('lodash'), ps = require('current-processes'); var plugin = { @@ -52,25 +51,41 @@ var plugin = { */ poll: function() { var stats = {}; - - // This uses the https://github.com/branneman/current-processes - // written by @branneman to factor this code out, and support multiple OS - // adapters. + // @todo If you can think of a better way of getting process stats, + // then please feel free to send me a pull request. This is version 0.1 + // and needs some love. ps.get(function(err, processes) { - var statsArray = []; - - //console.log(processes); for (var p in processes) { var process = processes[p]; - var cpuRounded = parseFloat(process.cpu / os.cpus().length).toFixed(1); - var memRounded = parseFloat(process.mem).toFixed(1); + // If already exists, then add them together + if (typeof stats[process.name] !== 'undefined') { + stats[process.name] = { + cpu: parseFloat(stats[process.name].cpu, 10) + parseFloat(process.cpu), + mem: parseFloat(stats[process.name].mem, 10) + parseFloat(process.mem), + comm: process.name, + count: parseInt(stats[process.name].count, 10) + 1 + }; + } else { + stats[process.name] = { + cpu: process.cpu, + mem: process.mem, + comm: process.name, + count: 1 + }; + } + } + var statsArray = []; + for (var stat in stats) { + // Divide by nuber of CPU cores + var cpuRounded = parseFloat(stats[stat].cpu / os.cpus().length).toFixed(1); + var memRounded = parseFloat(stats[stat].mem).toFixed(1); statsArray.push({ - 'Command': process.name, - 'Count': 1, + 'Command': stats[stat].comm, + 'Count': stats[stat].count, 'CPU %': cpuRounded, 'Memory %': memRounded, - 'cpu': process.cpu, - 'mem': process.mem // exact cpu for comparison + 'cpu': stats[stat].cpu, + 'mem': stats[stat].mem // exact cpu for comparison }); } statsArray.sort(function(a, b) { @@ -82,4 +97,4 @@ var plugin = { } }; -module.exports = exports = plugin; +module.exports = exports = plugin; \ No newline at end of file From aa57d397437234e0e0afc8d8b76392747bb625b2 Mon Sep 17 00:00:00 2001 From: James Hall Date: Thu, 19 Jun 2014 21:00:09 +0100 Subject: [PATCH 3/3] Divide by total mem #35 --- sensors/process.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sensors/process.js b/sensors/process.js index c3dced6..a899996 100644 --- a/sensors/process.js +++ b/sensors/process.js @@ -78,7 +78,7 @@ var plugin = { for (var stat in stats) { // Divide by nuber of CPU cores var cpuRounded = parseFloat(stats[stat].cpu / os.cpus().length).toFixed(1); - var memRounded = parseFloat(stats[stat].mem).toFixed(1); + var memRounded = parseFloat((stats[stat].mem / os.totalmem()) * 100).toFixed(1); statsArray.push({ 'Command': stats[stat].comm, 'Count': stats[stat].count,