Skip to content

Instantly share code, notes, and snippets.

@phaus phaus/plotPS.sh Secret
Last active Dec 22, 2015

Embed
What would you like to do?
#!/usr/bin/env node
var sys = require('sys')
var exec = require('child_process').exec;
var fs = require('fs');
var cmd;
switch(process.platform){
case 'darwin':
cmd = 'ps -A -c -o pid,ppid,pcpu,comm,uid -r';
break;
}
function parse(line){
var parts = line.split(" ");
var out = [];
var c = 0;
for(var i in parts){
if(parts[i].trim().length > 0){
out[c] = parts[i].trim();
c++;
}
}
return out;
}
function header(){
console.log("graph G {");
console.log("layout=twopi;");
console.log("rankdir=TB;");
console.log("nodesep=5.0;");
console.log("ratio=auto;");
console.log("ranksep=15.0;");
console.log("root=\"0\";");
console.log("0 [label=\"0\"]");
}
function scanCli(error, stdout, stderr) {
if (error) {
return console.log(error);
}
header();
var lines = stdout.split("\n");
for(var c in lines){
if(lines[c].length > 0 && c > 0){
var parts = parse(lines[c]);
var entry = {
pid: parts[0].trim(),
ppid: parts[1].trim(),
pcpu: parts[2].trim(),
cmd: parts[3].trim(),
uid: parts[4].trim()
};
console.log(entry.pid+" [label=\""+entry.cmd+" ("+entry.uid+")\"]");
console.log(entry.ppid+" -- "+entry.pid);
}
}
console.log("}");
}
function scanFile(err,data){
if (err) {
return console.log(err);
}
header();
var lines = data.split("\n");
for(var c in lines){
if(lines[c].length > 0 && c > 0){
var parts = parse(lines[c]);
var entry = {
pid: parts[0].trim(),
ppid: parts[1].trim(),
pcpu: parts[2].trim(),
cmd: parts[3].trim(),
uid: parts[4].trim()
};
console.log(entry.pid+" [label=\""+entry.cmd+" ("+entry.uid+")\"]");
console.log(entry.ppid+" -- "+entry.pid);
}
}
console.log("}");
}
if(process.argv.length == 2){
exec(cmd, scanCli);
} else {
fs.readFile(process.argv[2], 'utf8', scanFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.