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

View File

@ -100,6 +100,7 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.50.0 | 2018-11-23 | `services()` added possibility to specify ALL services "*" for linux |
| 3.49.4 | 2018-11-21 | `battery()` timeremaining optimization (linux) thanks to Jorai Rijsdijk |
| 3.49.3 | 2018-11-20 | `memLayout()` optimized parsing (win) |
| 3.49.2 | 2018-11-19 | code cleanup |

View File

@ -16,7 +16,12 @@ Simple system and OS information library for [node.js][nodejs-url]
## Quick Start
Lightweight collection of 35+ functions to retrieve detailed hardware, system and OS information (Linux, macOS, partial Windows, FreeBSD and SunOS support) - no npm dependencies.
Lightweight collection of 35+ functions to retrieve detailed hardware, system and OS information.
- simple to use
- get detailed information about system, cpu, baseboard, battery, memory, disks/filesystem, network, docker, software, services and processes
- supports Linux, macOS, partial Windows, FreeBSD and SunOS support
- no npm dependencies
### Installation
@ -59,13 +64,13 @@ async function cpu() {
(last 7 major and minor version releases)
- Version 3.50.0: `services()` added possibility to specify ALL services "*" for linux
- Version 3.49.0: `uuid()` added - os specific uuid (per installation)
- Version 3.48.0: `osInfo()` added build, serial (Windows/macOS)
- Version 3.47.0: `version()` added docker, postfix
- Version 3.46.0: `version()` added system openssl version (besides the one inside node.js)
- Version 3.45.0: `diskLayout()` added S.M.A.R.T. status
- Version 3.44.0: `battery()` added type, model, manufacturer, serial, timeremaining
- Version 3.43.0: added speed per CPU core `cpuCurrentspeed()`
- Version 3.42.0: added parent process PID `processes()`
- ...
You can find all changes here: [detailed changelog][changelog-url]
@ -373,7 +378,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (
| | pid | X | X | X | | | PID |
| | cpu | X | X | X | | | process % CPU |
| | mem | X | X | X | | | process % MEM |
| si.services('mysql, apache2', cb) | [{...}] | X | X | X | X | | pass comma separated string of services |
| si.services('mysql, apache2', cb) | [{...}] | X | X | X | X | | pass comma separated string of services<br>pass "*" for ALL services (linux only) |
| | [0].name | X | X | X | X | | name of service |
| | [0].running | X | X | X | X | | true / false |
| | [0].pcpu | X | X | X | | | process % CPU |

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)),