services() added possibility to specify ALL services (*) for linux

This commit is contained in:
Sebastian Hildebrandt
2018-11-23 14:08:10 +01:00
parent 9e78be48dd
commit e8c3101a99
3 changed files with 29 additions and 7 deletions
+19 -3
View File
@@ -14,6 +14,8 @@
const os = require('os');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const util = require('./util');
let _platform = process.platform;
@@ -86,20 +88,34 @@ function services(srv, callback) {
let srvs = srv.split('|');
let data = [];
let dataSrv = [];
let allSrv = [];
if (_linux || _freebsd || _openbsd || _darwin) {
let comm = (_darwin) ? 'ps -caxo pcpu,pmem,comm' : 'ps -axo pcpu,pmem,comm';
if ((_linux || _freebsd || _openbsd) && srv === '*') {
srv = '';
let tmpsrv = execSync('service --status-all 2> /dev/null').toString().split('\n');
for (const s of tmpsrv) {
const parts = s.split(']');
if (parts.length === 2) {
srv += (srv !== '' ? '|' : '') + parts[1].trim();
allSrv.push({ name: parts[1].trim(), running: parts[0].indexOf('+') > 0 });
}
}
srvs = srv.split('|');
}
let comm = (_darwin) ? 'ps -caxo pcpu,pmem,comm' : 'ps -axo pcpu,pmem,command';
if (srv !== '' && srvs.length > 0) {
exec(comm + ' | grep -v grep | egrep "' + srv + '"', { maxBuffer: 1024 * 2000 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
srvs.forEach(function (srv) {
let ps = lines.filter(function (e) {
return ((e + ' ').toLowerCase().indexOf(' ' + srv + ' ') !== -1) || (e.toLowerCase().indexOf(' ' + srv + ':') !== -1);
return (e.toLowerCase().indexOf(' ' + srv + ':') !== -1) || (e.toLowerCase().indexOf('/' + srv) !== -1);
});
let singleSrv = allSrv.filter(item => { return item.name === srv; });
data.push({
'name': srv,
'running': ps.length > 0,
'running': (allSrv.length && singleSrv.length ? singleSrv[0].running : ps.length > 0),
'pcpu': parseFloat((ps.reduce(function (pv, cv) {
return pv + parseFloat(cv.trim().split(' ')[0]);
}, 0)).toFixed(2)),