]> git.r.bdr.sh - rbdr/monitorcito/blob - lib/index.js
Add link home
[rbdr/monitorcito] / lib / index.js
1 const { promisify } = require('util');
2 const { exec } = require('child_process');
3
4 const internals = {
5 kActiveIndicator: 'active',
6 kFieldSeparator: '\n',
7 kKeyValueSeparator: '=',
8 kBlockSeparator: '\n\n',
9 statusCommand: 'systemctl show --no-page -p Description -p ActiveState',
10
11 formatStatusOutput(systemctlText) {
12
13 const blocks = systemctlText.trim().split(internals.kBlockSeparator);
14 return blocks
15 .map((block) => {
16
17 const fields = block.split(internals.kFieldSeparator);
18 return fields.reduce((fieldObject, field) => {
19
20 const [key, value] = field.split(internals.kKeyValueSeparator);
21 return {
22 ...fieldObject,
23 [key]: value
24 }
25 }, {})
26
27 })
28 .reduce((statusObject, service) => {
29
30 return {
31 ...statusObject,
32 [service.Description]: service.ActiveState === internals.kActiveIndicator
33 }
34 }, {});
35 },
36
37 exec: promisify(exec)
38 };
39
40 module.exports = async (services) => {
41
42 const { stdout } = await internals.exec(`${internals.statusCommand} ${services.join(' ')}`);
43 return internals.formatStatusOutput(stdout);
44 };