forked from reklatsmasters/node-process-list
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
77 lines (66 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const ps = require('bindings')('processlist')
const then = require('pify')
const es6snapshot = then(ps.snapshot)
const allowedFields = Object.freeze([
'name',
'pid',
'ppid',
'path',
'threads',
'owner',
'priority',
'cmdline',
'starttime',
'vmem',
'pmem',
'cpu',
'utime',
'stime'
])
const defaultFields = {
name: true,
pid: true,
ppid: true,
path: true,
threads: true,
owner: true,
priority: true,
cmdline: true,
starttime: true,
vmem: true,
pmem: true,
cpu: true,
utime: true,
stime: true
}
module.exports = {
snapshot,
allowedFields
}
/**
* get process list
* @param {Object} args
* @param {bool} opts.pid
* @param {bool} opts.ppid
* @param {bool} opts.name
* @param {bool} opts.path
* @param {bool} opts.owner
* @param {bool} opts.threads
* @param {bool} opts.priority
* @param {bool} opts.cmdline
*/
function snapshot (args) {
let opts = {}
args = Array.isArray(args) ? args : Array.from(arguments)
if (!args.length) {
opts = defaultFields
}
for (let i = 0; i < args.length; ++i) {
if (allowedFields.indexOf(args[i]) === -1) {
throw new Error(`Unknown field "${args[i]}"`)
}
opts[args[i]] = true
}
return es6snapshot(opts)
}