Permalink
Browse files

Add support for specifying a pid. Add CLI tool. Expose module version.

  • Loading branch information...
1 parent c42c95c commit 4e52b670b2735bdbf24cc2330bc3334f58ec1107 @dshaw committed Apr 25, 2012
Showing with 76 additions and 10 deletions.
  1. +9 −1 Readme.md
  2. +46 −0 bin/proc-cli.js
  3. +12 −3 package.json
  4. +1 −0 proc.js
  5. +8 −6 usage.js
View
@@ -4,7 +4,7 @@ Expose system state via the /proc file system.
## Known limitations
-This was written for us to use at Voxer on Joyent. It has not been tested outside of SmartOS and I have no expectations that this should run on anything but some flavor of Solaris.
+This was written for Voxer to use on Joyent. It has not been tested outside of SmartOS and there really are no expectations that this should run on anything but some flavor of Solaris. I am open to having this module supporting Linux, too and I have talked to multiple people who would be interested in having that exist. I leave it up to you, the contributor, to decide if you'd rather contribute that to this module or run with it in your own module.
## usage
@@ -18,6 +18,14 @@ Expose `/proc/self/usage`
console.dir(usage_obj);
});
+## CLI install
+
+ npm install -g proc
+
+## CLI usage
+
+ proc usage 10008
+
## Underlying data structure
<pre>
View
@@ -0,0 +1,46 @@
+#!/usr/bin/env node
+;(function () { // wrapper in case we're in module_context mode
+
+ process.title = 'proc' // set process title
+
+ var proc = require('../proc.js')
+ , args = Array.prototype.splice.call(process.argv, 2)
+ , cmd = args.shift()
+
+ args.push(cb)
+
+ if (proc[cmd]) {
+ if (typeof proc[cmd] === 'function') {
+ proc[cmd].apply(proc, args)
+ } else {
+ cb(null, proc[cmd])
+ }
+ } else {
+ cb(null, help())
+ }
+
+})()
+
+/**
+ * help
+ */
+
+function help (cmd) {
+ return [
+ 'usage:'
+ , ' proc [command] [pid]'
+ , ''
+ , 'examples:'
+ , ' proc usage 10008'
+ , ' proc version'
+ ].join('\n')
+}
+
+/**
+ * Callback handler
+ */
+
+function cb (err, data) {
+ if (err) console.error(err)
+ if (data) console.log((typeof data === "string") ? data : JSON.stringify(data))
+}
View
@@ -1,15 +1,24 @@
{
"name": "proc",
- "version": "0.2.1",
+ "version": "0.2.2-pre",
"description": "Expose system state via the /proc file system.",
- "keywords": ["proc", "smartos", "solaris"],
+ "keywords": ["proc", "prusage", "ptime", "smartos", "solaris"],
"author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)",
+ "contributors": [
+ "Matt Ranney",
+ "Dave Eddy",
+ "Mike Zeller"
+ ],
"repository": {
"type": "git",
"url": "https://github.com/dshaw/proc"
},
"bugs": {
"url": "http://github.com/dshaw/proc/issues"
},
- "main": "./proc"
+ "main": "./proc",
+ "bin": { "proc": "./bin/proc-cli.js" },
+ "scripts": {
+ "test": "make test"
+ }
}
View
@@ -8,4 +8,5 @@
* Exports.
*/
+exports.version = require('./package.json').version;
exports.usage = require('./usage');
View
@@ -2,16 +2,13 @@
* proc
* Copyright(c) 2012 Daniel D. Shaw <dshaw@dshaw.com>
* MIT Licensed
- * Contributors
- * Dave Eddy <dave@daveeddy.com>
- * Mike Zeller <mike@mikezeller.net>
*/
/**
* Module dependencies.
*/
-var fs = require("fs");
+var fs = require('fs');
/**
* Exports.
@@ -21,8 +18,13 @@ function readTimespec(buf, offset) {
return buf.readInt32LE(offset) + (buf.readInt32LE(offset + 4) / 1000000000);
}
-module.exports = function usage(callback) {
- fs.readFile("/proc/self/usage", function (err, buf) {
+module.exports = function usage (pid, callback) {
+ if (typeof pid === 'function') {
+ callback = pid;
+ pid = 'self';
+ }
+
+ fs.readFile('/proc/'+pid+'/usage', function (err, buf) {
if (err) return callback(err);
var data = {};

0 comments on commit 4e52b67

Please sign in to comment.