-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (106 loc) · 3.3 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Generated by CoffeeScript 1.9.2
/*
s m a r t c t l
*/
(function() {
var exec, execSmart;
exec = require('child_process').exec;
/*
R A W C M D
*/
module.exports.raw = execSmart = function(args, cb) {
return exec("smartctl " + args, {
maxBuffer: 1024 * 1024 * 24
}, function(e, stdout, stderr) {
var lines;
lines = stdout.split('\n').slice(0, -1);
if (e != null) {
return cb(lines.slice(3), []);
} else {
return cb(null, lines.slice(4));
}
});
};
/*
D E V I C E I N F O S
*/
module.exports.info = function(devicePath, cb) {
return execSmart("-i " + devicePath, function(e, lines) {
var deviceInfos, i, len, line, ref;
if (e != null) {
return cb(e, lines);
}
deviceInfos = {};
ref = lines.slice(0, -1);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
deviceInfos[line.substring(0, line.search(': ')).trim().replace(/\ +/g, '_').toLowerCase()] = line.substring(1 + line.search(': ')).trim();
}
return cb(null, deviceInfos);
});
};
/*
S M A R T A T T R S
*/
module.exports.smartAttrs = function(devicePath, cb) {
return execSmart("-A -f brief " + devicePath, function(e, lines) {
var attr, head, i, infos, len, line;
if (e != null) {
return cb(e, lines);
}
lines = lines.slice(2, -1);
head = lines.shift();
infos = [];
for (i = 0, len = lines.length; i < len; i++) {
line = lines[i];
attr = line.substring(head.indexOf('ATTRIBUTE_NAME'), head.indexOf('FLAGS')).trim().toLowerCase();
if (attr === '') {
continue;
}
infos.push({
attr: attr,
id: Number(line.substring(0, head.indexOf('ATTRIBUTE_NAME')).trim()),
flags: line.substring(head.indexOf('FLAGS'), head.indexOf('VALUE')).trim(),
value: line.substring(head.indexOf('VALUE'), head.indexOf('WORST')).trim(),
worst: line.substring(head.indexOf('WORST'), head.indexOf('THRESH')).trim(),
thresh: line.substring(head.indexOf('THRESH'), head.indexOf('FAIL')).trim(),
fail: line.substring(head.indexOf('FAIL'), head.indexOf('RAW_VALUE')).trim(),
raw: Number(line.substring(head.indexOf('RAW_VALUE')).trim().split(' ')[0])
});
}
return cb(null, infos);
});
};
/*
S M A R T H E A L T H
*/
module.exports.health = function(devicePath, cb) {
return execSmart("-H " + devicePath, function(e, lines) {
var status;
if (e != null) {
return cb(e, lines);
}
lines = lines.slice(0, -1);
if (0 === lines[0].search('SMART overall-health self-assessment test result: ')) {
status = lines[0].split(' ').pop().toLowerCase();
return cb(null, status);
} else {
return cb(null, lines);
}
});
};
module.exports.scan = function(cb) {
return exec('smartctl --scan-open', {
maxBuffer: 1024 * 1024 * 24
}, function(e, stdout, stderr) {
var devices, i, len, n, ref;
devices = [];
ref = stdout.split('\n').slice(0, -1);
for (i = 0, len = ref.length; i < len; i++) {
n = ref[i];
devices.push(n.split(' ')[0]);
}
return cb(devices);
});
};
}).call(this);