Comparing version 0.1.4 to 0.2.1
108
lib/usage.js
@@ -1,16 +0,108 @@ | ||
var binding = require('bindings')('node_usage'); | ||
var sysinfo = require('bindings')('sysinfo'); | ||
var fs = require('fs'); | ||
exports.lookup = function(pid, callback) { | ||
var UPTIME_FILE = '/proc/uptime'; | ||
var STAT_FILE = '/proc/$pid/stat'; | ||
var STAT_INDEXES = { | ||
STIME: 13, | ||
UTIME: 14, | ||
START_TIME: 21, | ||
RSS: 23 | ||
}; | ||
binding.lookup(parseInt(pid), function(err, cpu, memory) { | ||
function usageLookup(pid, callback) { | ||
if(!sysinfo.IS_SUPPORTED) { | ||
return callback(new Error('NOT_SUPPORTED_ON_THIS_PLATFORM')); | ||
} | ||
var uptime; | ||
getUptime(UPTIME_FILE, function(err, value) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
callback(null, { | ||
cpu: cpu, | ||
memory: memory | ||
}); | ||
uptime = value; | ||
getStat(STAT_FILE, pid, calculateUsage); | ||
} | ||
}); | ||
}; | ||
function calculateUsage(err, stat) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
var usageData = { | ||
cpu: calculateCpuUsage(uptime, stat), | ||
memory: calculateMemoryUsage(stat) | ||
}; | ||
callback(null, usageData); | ||
} | ||
} | ||
} | ||
function calculateCpuUsage(uptime, stat) { | ||
// unsigned long long total_time; /* jiffies used by this process */ | ||
// unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */ | ||
// unsigned long long seconds; /* seconds of process life */ | ||
// unsigned int seconds_since_boot = uptime(0, 0); | ||
// total_time = pp->utime + pp->stime; | ||
// seconds = seconds_since_boot - pp->start_time / Hertz; | ||
// if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds; | ||
// return (float) pcpu/10U; | ||
var totalTime = stat.stime + stat.utime; | ||
var seconds = uptime - stat.startTime / sysinfo.HERTZ; | ||
var pcpu = (totalTime * 1000 / sysinfo.HERTZ ) / seconds; | ||
return pcpu / 10; | ||
} | ||
function calculateMemoryUsage(stat) { | ||
return stat.rss * sysinfo.PAGE_SIZE; | ||
} | ||
function getUptime(file, callback) { | ||
fs.readFile(file, 'utf8', function(err, data) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
var matched = data.match(/^(.*) /); | ||
if(matched) { | ||
var uptime = parseFloat(matched[1]); | ||
callback(null, uptime); | ||
} else { | ||
callback(new Error('Invalid formatted uptime file')); | ||
} | ||
} | ||
}); | ||
} | ||
function getStat(file, pid, callback) { | ||
file = file.replace('$pid', pid); | ||
fs.readFile(file, 'utf8', function(err, data) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
var parts = data.split(' '); | ||
var statObject = { | ||
stime: parseFloat(parts[STAT_INDEXES.STIME]), | ||
utime: parseFloat(parts[STAT_INDEXES.UTIME]), | ||
startTime: parseFloat(parts[STAT_INDEXES.START_TIME]), | ||
rss: parseFloat(parts[STAT_INDEXES.RSS]) | ||
}; | ||
callback(null, statObject); | ||
} | ||
}); | ||
} | ||
exports.lookup = usageLookup; | ||
exports._getUptime= getUptime; | ||
exports._getStat = getStat; |
{ | ||
"name": "usage", | ||
"version": "0.1.4", | ||
"version": "0.2.1", | ||
"description": "simple way to lookup linux process usage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,4 +0,32 @@ | ||
node-usage | ||
========== | ||
# node-usage | ||
Linux Process Usage Lookup via NodeJS | ||
### linux process usage lookup with nodejs | ||
* With `node-usage` we can lookup cpu and memory usage of any accessible process on the system. | ||
* This is 98% JavaScript and logic is based on [procps](http://procps.sourceforge.net/) | ||
## Example | ||
### Code | ||
~~~js | ||
var usage = require('usage'); | ||
var pid = process.pid // you can use any valid PID instead | ||
usage.lookup(pid, function(err, result) { | ||
}); | ||
~~~ | ||
### Result Object | ||
~~~js | ||
{ | ||
cpu: 10.6, //in percentage | ||
memory: 100065280 //in no of bytes | ||
} | ||
~~~ | ||
## Motivation | ||
* Some NodeJS PAAS providers do not expose us an interface to monitor CPU and Memory usage of our apps. | ||
* They asks us to go for somewhere else for that which I don't want to go (or simply I cannot go) | ||
* So `node-usage` was born to tackle on this. |
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code which could be a vector to obscure malicious code, and generally decrease the likelihood of reproducible or reliable installs.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
128
33
302211
54
1
2