This repository was archived by the owner on Oct 26, 2021. It is now read-only.
generated from server-state/template-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (63 loc) · 2.36 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
const fs = require('fs');
const path = require('path');
const parser = require('./grammar.pegjs');
const defaultOptions = {
filter: [],
invert: false
};
function validateOptions(options) {
if (!Array.isArray(options.filter))
throw new Error('Wrong argument type. Expected options: filter to be an array, but found ' + typeof options.filter);
if (typeof options.invert !== 'boolean')
throw new Error('Wrong argument type. Expected options: invert to be a boolean, but found ' + typeof options.invert);
}
/**
* A module for the server-state system
*
* Parse the file /proc/mdstat generated from the linux raid kernel module and extract useful information
*
* @throws if invalid options format is given
* @throws if kernel module not loaded and file /proc/mdstat not created
* @throws if parser can not parse current /proc/mdstat
*
* @argument options given options to specifiy module task
*
* @returns {object|array|string|number|boolean} A JSON-serializable (via `JSON.stringify()`) vers{object|array|string|number|boolean}ion information about the raids in the linux system
*/
module.exports = async function (options) {
options = Object.assign({}, defaultOptions, options);
validateOptions(options);
let file;
// pull file from filesystem
try {
file = fs.readFileSync('/proc/mdstat', { encoding: 'utf-8' });
} catch (e) {
e.message = 'File not found: /proc/mdstat.\n' + e.message;
throw (e);
}
// parse file into json format
let result;
try {
result = parser.parse(file);
} catch (e) {
e.message = 'Can not parse: /proc/mdstat.\n' + e.message;
throw (e);
}
// resolve block name with unique linkage
const names = fs.readdirSync('/dev/md');
let raidNames = {};
for (const name of names) {
const blockName = path.basename(fs.readlinkSync('/dev/md/' + name));
raidNames[blockName] = name;
}
// insert uniqueName into result
for (const raid of result['raids']) {
raid['unique'] = raidNames[raid['name']];
}
// filter through valid raids
// passes filter, if name is unique or name of raid inserted (with invert option)
result.raids = result.raids.filter(elem =>
options.invert ^ !(options.filter.includes(elem.unique) || options.filter.includes(elem.name))
);
return result;
};