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

Feature current process #41

Open
wants to merge 3 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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
64 changes: 22 additions & 42 deletions sensors/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

var os = require('os'),
fs = require('fs'),
child_process = require('child_process');
child_process = require('child_process'),
ps = require('current-processes');

var plugin = {
/**
Expand Down Expand Up @@ -53,52 +54,31 @@ var plugin = {
// @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
};
}
ps.get(function(err, processes) {
for (var p in processes) {
var process = processes[p];
// 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);
var memRounded = parseFloat((stats[stat].mem / os.totalmem()) * 100).toFixed(1);
statsArray.push({
'Command': stats[stat].comm,
'Count': stats[stat].count,
Expand All @@ -117,4 +97,4 @@ var plugin = {
}
};

module.exports = exports = plugin;
module.exports = exports = plugin;