-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkstat.js
104 lines (77 loc) · 2 KB
/
kstat.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* A simple demonstration of the kstat reader that (if given no argument) reads
* every kstat and prints module, class, module, name and instance. If an
* argument is provided, only kstats that have the specified class, module,
* name and/or instance will be displayed. If the "-v" option is specified,
* all fields in the named kstat (and their values) will be printed.
*/
var kstat = require('bindings')('kstat');
var options = { c: 'class', n: 'name', m: 'module', i: 'instance', v: true };
var stats = {};
var verbose = false;
var i;
for (i = 2; i < process.argv.length; i++) {
var arg = process.argv[i];
var opt = arg.charAt(1);
if (arg.charAt(0) != '-' || !opt || !options[opt]) {
console.log('invalid option "' + arg + '"');
process.exit(1);
}
if (opt == 'v') {
verbose = true;
continue;
}
if (!(arg = process.argv[++i])) {
console.log('expected argument for option "' + opt + '"');
process.exit(1);
}
stats[options[opt]] = arg;
}
if (stats.hasOwnProperty('instance'))
stats.instance = parseInt(stats.instance, 10);
var fixed = function (str, len) {
var rval = str, j;
for (j = 0; j < len - str.length; j++)
rval += ' ';
return (rval);
};
var reader = new kstat.Reader(stats);
var data = reader.read();
var fields = {
module: 20,
'class': 20,
name: 30,
instance: 8
};
var header = function () {
var str = '';
for (f in fields)
str += fixed(f.toUpperCase(), fields[f]);
console.log(str);
};
header();
data.sort(function (l, r) {
for (f in fields) {
if (f == 'instance')
return (l[f] - r[f]);
var rval = l[f].localeCompare(r[f]);
if (rval)
return (rval);
}
return (0);
});
for (i = 0; i < data.length; i++) {
var s = '', f;
for (f in fields)
s += fixed(data[i][f], fields[f] + '');
console.log(s);
if (!verbose || !data[i].data)
continue;
console.log('|\n+--> ' + fixed('FIELD', 35) + 'VALUE');
for (f in data[i].data)
console.log(' ' + fixed(f, 35) + data[i].data[f] + '');
if (i < data.length - 1) {
console.log('');
header();
}
}