usage
Advanced tools
Comparing version 0.3.6 to 0.3.7
@@ -0,1 +1,3 @@ | ||
var exec = require('child_process').exec; | ||
module.exports = function(sysinfo) { | ||
@@ -16,4 +18,34 @@ | ||
sysinfo.getUsage(pid, callback); | ||
exec('ps -vp ' + pid, function(err, stdout, stderr) { | ||
if (err || stderr) return callback(err || stderr); | ||
var usage = parsePS(pid, stdout); | ||
if (!usage) | ||
return callback('INVALID_PID'); | ||
callback(null, usage); | ||
}); | ||
}; | ||
} | ||
function parsePS(pid, output) { | ||
var lines = output.trim().split('\n'); | ||
if (lines.length !== 2) | ||
return null; | ||
var labelsMap = {}; | ||
var labels = lines[0].trim().split(/[ \t]+/g); | ||
for (var i = 0; i < labels.length; i++) | ||
labelsMap[labels[i]] = i; | ||
var values = lines[1].trim().split(/[ \t]+/g); | ||
var foundPID = parseInt(values[labelsMap['PID']], 10); | ||
var rss = parseInt(values[labelsMap['RSS']], 10); | ||
var cpu = parseFloat(values[labelsMap['%CPU']]); | ||
if (pid != foundPID || isNaN(rss) || isNaN(cpu)) | ||
return null; | ||
return { memory: rss, cpu: cpu }; | ||
} |
{ | ||
"name": "usage", | ||
"version": "0.3.6", | ||
"version": "0.3.7", | ||
"description": "simple way to lookup linux process usage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -52,5 +52,2 @@ # node-usage [![Build Status](https://travis-ci.org/arunoda/node-usage.png?branch=master)](https://travis-ci.org/arunoda/node-usage) | ||
## Notes on OSX | ||
if you are going to monitor usage of a foriegn pid, please do it as `sudo`. See [here](https://github.com/arunoda/node-usage/pull/5) for more details. | ||
## Motivation | ||
@@ -57,0 +54,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
270
17265
24
57
2