diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-06-08 21:37:55 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-06-08 21:37:55 +0200 |
| commit | 2d72aab0a88baa906127489e1c25fd11881bdc35 (patch) | |
| tree | 0f213b96c74fc6465e4ea07123a39f8dc469c857 /lib/index.js | |
Add the monitor
Diffstat (limited to 'lib/index.js')
| -rw-r--r-- | lib/index.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..d5d9668 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,44 @@ +const { promisify } = require('util'); +const { exec } = require('child_process'); + +const internals = { + kActiveIndicator: 'active', + kFieldSeparator: '\n', + kKeyValueSeparator: '=', + kBlockSeparator: '\n\n', + statusCommand: 'systemctl show --no-page -p Id -p ActiveState', + + formatStatusOutput(systemctlText) { + + const blocks = systemctlText.trim().split(internals.kBlockSeparator); + return blocks + .map((block) => { + + const fields = block.split(internals.kFieldSeparator); + return fields.reduce((fieldObject, field) => { + + const [key, value] = field.split(internals.kKeyValueSeparator); + return { + ...fieldObject, + [key]: value + } + }, {}) + + }) + .reduce((statusObject, service) => { + + return { + ...statusObject, + [service.Id]: service.ActiveState === internals.kActiveIndicator + } + }, {}); + }, + + exec: promisify(exec) +}; + +module.exports = async (services) => { + + const { stdout } = await internals.exec(`${internals.statusCommand} ${services.join(' ')}`); + return internals.formatStatusOutput(stdout); +}; |