Permalink
Browse files

Simplify things with better use of split().

  • Loading branch information...
1 parent baaeb33 commit e86977f2bed532a344b2df4c09ed84a9b04fee17 @jperkin committed Feb 29, 2012
Showing with 8 additions and 15 deletions.
  1. +8 −15 probes/cpu.js
View
@@ -13,24 +13,17 @@ function get_cpu_utilization(callback)
switch (process.platform) {
case 'linux':
exec('mpstat -u -P ALL 1 1', function (err, stdout, stderr) {
- var capture = 0;
- stdout.split('\n').forEach(function (line) {
+ stdout.split('\n\n')[1].split('\n').forEach(function (line) {
var ret = {};
- if (line.length === 0) {
- capture = (capture) ? 0 : 1;
+ var vals = line.split(/\s+/);
+ if (!vals[1].match(/\d+/)) {
return;
}
- if (capture) {
- var vals = line.split(/\s+/);
- if (!vals[1].match(/\d+/)) {
- return;
- }
- ret['cpu.utilization.cpu' + vals[1] + '.user'] = vals[2];
- ret['cpu.utilization.cpu' + vals[1] + '.system'] = vals[4];
- ret['cpu.utilization.cpu' + vals[1] + '.iowait'] = vals[5];
- ret['cpu.utilization.cpu' + vals[1] + '.idle'] = vals[10];
- callback(ret);
- }
+ ret['cpu.utilization.cpu' + vals[1] + '.user'] = vals[2];
+ ret['cpu.utilization.cpu' + vals[1] + '.system'] = vals[4];
+ ret['cpu.utilization.cpu' + vals[1] + '.iowait'] = vals[5];
+ ret['cpu.utilization.cpu' + vals[1] + '.idle'] = vals[10];
+ callback(ret);
});
});
break;

0 comments on commit e86977f

Please sign in to comment.