-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
67 lines (59 loc) · 1.26 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
'use strict';
var childProcess = require('child_process');
function run(args, cb) {
childProcess.execFile('free', args, function (err, stdout) {
if (err) {
cb(err);
return;
}
var memInfo = {};
stdout.trim().split('\n').slice(1).map(function (el) {
var cl = el.split(/\s+(?=[\d\/])/).map(function(i, idx) { return idx ? parseInt(i, 10) : i; });
switch(cl[0]) {
case "Mem:":
memInfo.mem = {
total: cl[1],
used: cl[2],
free: cl[3],
shared: cl[4],
buffers: cl[5],
cached: cl[6],
usable: cl[3] + cl[5] + cl[6]
};
break;
case "-/+ buffers/cache:":
memInfo.buffer = memInfo.cache = {
used: cl[1],
free: cl[2]
};
break;
case "Swap:":
memInfo.swap = {
total: cl[1],
used: cl[2],
free: cl[3]
};
break;
}
});
if (!memInfo.buffer) {
memInfo.buffer = memInfo.cache = {
used: memInfo.mem.total - memInfo.mem.usable,
free: memInfo.mem.usable
};
}
return cb(null, memInfo);
});
}
var free = module.exports = function (cb) {
run([], cb);
};
free.m = function (cb) {
run(['-m'], cb);
};
free.k = function (cb) {
run(['-k'], cb);
};
free.g = function (cb) {
run(['-g'], cb);
};