extended docker stats, and no longer relying on curl

This commit is contained in:
Sebastian Hildebrandt 2016-11-02 12:25:06 +01:00
parent 645a39991b
commit 90ddf2ec96
4 changed files with 285 additions and 140 deletions

122
CHANGELOG.md Normal file
View File

@ -0,0 +1,122 @@
# Changelog
### Major (breaking) Changes - Version 3
- works only with [node.js][nodejs-url] **v4.0.0** and above (using now internal ES6 promise function, arrow functions, ...)
- **Promises**. As you can see above, you can now also use it in a promise oriented way. But callbacks are still supported.
- `cpuCurrentspeed`: now returns an object with current minimal, maximal and average CPU frequencies of all cores.
- `mem`: now supports also newer versions of `free` (Version 3.3.10 and above); extended information `avaliable` (potentially available memory)
- `fsStats`: added information sum bytes read + write (tx) and sum transfer rate/sec (tx_sec)
- `networkInterfaces`: now providing one more detail: internal - true if this is an internal interface like "lo"
- `networkConnections`: instead of only counting sockets, you now get an array of objects with connection details for each socket (protocol, local and peer address, state)
- `users`: now provides an array of objects with users online including detailed session information (login date/time, ip address, terminal, command)
- `inetLatency`: now you can provide a host against which you want to test latency (default is 8.8.8.8)
- `getDynamicData`: changed order of parameters (callback - if provided - is now the last one): `getDynamicData(srv, network, callback)`
- `getAllData`: changed order of parameters (callback - if provided - is now the last one): `getAllData(srv, network, callback)`
New Functions
- `versions`: returns object of versions - kernel, ssl, node, npm, ...(new in version 3.6)
- `graphics`: returns arrays of graphics controllers and displays (new in version 3.5)
- `networkInterfaceDefault`: returns default network interface (new in version 3.4)
- `processes`: now returns also a process list with all process details (new in version 3.3)
- `battery`: retrieves battery status and charging level (new in version 3.2)
- `dockerContainers`: returns a list of all docker containers (new in version 3.1)
- `dockerContainerStats`: returns statistics for a specific docker container (new in version 3.1)
- `dockerAll`: returns a list of all docker containers including their stats (new in version 3.1)
- `disksIO`: returns overall diskIO and IOPS values for all mounted volumes (new in version 3.0)
Bug Fixes
- bugfix `disksIO` - on OSX read and write got mixed up
- several bug fixes (like assess errors in `cpuCurrentspeed`, potentially incorrect results in `users`, ...)
- testet on even more platforms and linux distributions
**Be aware**, that the new version 3.x is **NOT fully backward compatible** to version 2.x ...
### Major (breaking) Changes - Version 2
There had been a lot of changes in version 2 of systeminformation! Here is a quick overview (for those who come from version 1):
New Functions
- `version`: returns systeminformation version (semver) of this library
- `system`: hardware info (manufacturer, product/model name, version, serial, uuid)
- `networkConnections`: number of active connections
- `inetLatency`: latency in ms to external resource (internet)
- `getStaticData`: returns on json object with static data at once (OS, CPU, Network Interfaces - they should not change until restarted)
- `getDynamicData`: returns on json object with all dynamic data at once (e.g. for monitoring agents)
- `getAllData`: returns on json object with all data (static and dynamic) at once
Renamed Functions (now all camelCase)
- `osinfo`: renamed to `osInfo`
- `cpu_currentspeed`: renamed to `cpuCurrentspeed`
- `cpu_temperature`: renamed to `cpuTemperature`
- `fs_size`: renamed to `fsSize`
- `fs_speed`: renamed to `fsStats`
- `network_interfaces`: renamed to `networkInterfaces`
- `network_speed`: renamed to `networkStats`
- `network_connections`: renamed to `networkConnections`
- `currentload`: renamed to `currentLoad`
- `fullload`: renamed to `fullLoad`
- `processload`: renamed to `processLoad`
- `checksite`: renamed to `inetChecksite`
Function Changes
- `cpu_temperature`/`cpuTemperature`: -1 is new default (and indicates that non sensors are installed)
- `cpu_temperature`/`cpuTemperature`: new result `max` which returns max temperature of all cores
- `cpu_currentspeed`/`cpuCurrentspeed`: now in GHz
- `cpu`: splitted `manufacturer` (e.g. Intel) and `brand` (e.g. Core 2 Duo)
- `network_speed`/`networkStats`: now better support for OS X (also support for `operstate`)
- `network_speed`/`networkStats`: overall received and transferred bytes (rx, tx)
- `mem`: now better support for OS X (also support for `swaptotal`, `swapused`, `swapfree`)
- `fs_size`/`fsSize`: use-values now in % (0 - 100% instead of 0 - 1)
- `fs_speed`/`fsStats`: now also full support for OS X
- `checksite`/`inetChecksite`: new result structure - see command reference
- `checksite`/`inetChecksite`: ms (former `response_ms`): -1 if not ok
Other changes
- no more external dependencies: `request` is not longer needed
- where possible results are now integer or float values (instead of strings) because it is easier to calculate with numbers ;-)
## Version history
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.7.0 | 2016-11-02 | extended docker stats, and no longer relying on curl |
| 3.6.0 | 2016-09-14 | added versions (kernel, ssl, node, npm, pm2, ...) |
| 3.5.1 | 2016-09-14 | bugfix graphics info |
| 3.5.0 | 2016-09-14 | added graphics info (controller, display) |
| 3.4.4 | 2016-09-02 | tiny fixes system.model, getDefaultNetworkInterface |
| 3.4.3 | 2016-09-02 | tiny bug fix fsStats, disksIO OSX |
| 3.4.2 | 2016-09-01 | improved default network interface |
| 3.4.1 | 2016-08-30 | updated docs |
| 3.4.0 | 2016-08-30 | rewritten processes current cpu usage |
| 3.3.0 | 2016-08-24 | process list added to processes |
| 3.2.1 | 2016-08-19 | updated docs, improvement system |
| 3.2.0 | 2016-08-19 | added battery information |
| 3.1.1 | 2016-08-18 | improved system and os detection (vm, ...), bugfix disksIO |
| 3.1.0 | 2016-08-18 | added Docker stats |
| 3.0.1 | 2016-08-17 | Bug-Fix disksIO, users, updated docs |
| 3.0.0 | 2016-08-03 | new major version 3.0 |
| 2.0.5 | 2016-03-02 | changed .gitignore |
| 2.0.4 | 2016-02-22 | tiny correction - removed double quotes CPU brand, ... |
| 2.0.3 | 2016-02-22 | optimized cpuCurrentspeed |
| 2.0.2 | 2016-02-22 | added CoreOS identification |
| 2.0.1 | 2016-01-07 | minor patch |
| 2.0.0 | 2016-01-07 | new major version 2.0 |
| 1.0.7 | 2015-11-27 | fixed: si.network_speed() |
| 1.0.6 | 2015-09-17 | fixed: si.users() |
| 1.0.5 | 2015-09-14 | updated dependencies |
| 1.0.4 | 2015-07-18 | updated docs |
| 1.0.3 | 2015-07-18 | bugfix cpu cores |
| 1.0.2 | 2015-07-18 | bugfix cpu_currentspeed, cpu_temperature |
| 1.0.1 | 2015-07-18 | documentation update |
| 1.0.0 | 2015-07-18 | bug-fixes, version bump, published as npm component |
| 0.0.3 | 2014-04-14 | bug-fix (cpu_speed) |
| 0.0.2 | 2014-03-14 | Optimization FS-Speed & CPU current speed |
| 0.0.1 | 2014-03-13 | initial release |

137
README.md
View File

@ -42,6 +42,7 @@ si.cpu()
### Latest Activity
- Version 3.7.0: extended docker stats.
- Version 3.6.0: added versions (kernel, ssl, node, npm, pm2, ...).
- Version 3.5.0: added graphics info (controller and display).
- Version 3.4.0: rewritten currentLoad and CPU load for processes (linux). This is now much more accurate.
@ -50,92 +51,9 @@ si.cpu()
- Version 3.1.0: added [Docker][docker-url] support. Now you can scan your docker containers and get their stats
- Version 3.0.0: added DisksIO - overall diskIO and IOPS values for all mounted volumes
I also created a little command line tool called [mmon][mmon-github-url] (micro-monitor), also available via [github][mmon-github-url] and [npm][mmon-npm-url]
### Changelog
Here all changes more detailed:
### Major (breaking) Changes - Version 3
- works only with [node.js][nodejs-url] **v4.0.0** and above (using now internal ES6 promise function, arrow functions, ...)
- **Promises**. As you can see above, you can now also use it in a promise oriented way. But callbacks are still supported.
- `cpuCurrentspeed`: now returns an object with current minimal, maximal and average CPU frequencies of all cores.
- `mem`: now supports also newer versions of `free` (Version 3.3.10 and above); extended information `avaliable` (potentially available memory)
- `fsStats`: added information sum bytes read + write (tx) and sum transfer rate/sec (tx_sec)
- `networkInterfaces`: now providing one more detail: internal - true if this is an internal interface like "lo"
- `networkConnections`: instead of only counting sockets, you now get an array of objects with connection details for each socket (protocol, local and peer address, state)
- `users`: now provides an array of objects with users online including detailed session information (login date/time, ip address, terminal, command)
- `inetLatency`: now you can provide a host against which you want to test latency (default is 8.8.8.8)
- `getDynamicData`: changed order of parameters (callback - if provided - is now the last one): `getDynamicData(srv, network, callback)`
- `getAllData`: changed order of parameters (callback - if provided - is now the last one): `getAllData(srv, network, callback)`
New Functions
- `versions`: returns object of versions - kernel, ssl, node, npm, ...(new in version 3.6)
- `graphics`: returns arrays of graphics controllers and displays (new in version 3.5)
- `networkInterfaceDefault`: returns default network interface (new in version 3.4)
- `processes`: now returns also a process list with all process details (new in version 3.3)
- `battery`: retrieves battery status and charging level (new in version 3.2)
- `dockerContainers`: returns a list of all docker containers (new in version 3.1)
- `dockerContainerStats`: returns statistics for a specific docker container (new in version 3.1)
- `dockerAll`: returns a list of all docker containers including their stats (new in version 3.1)
- `disksIO`: returns overall diskIO and IOPS values for all mounted volumes (new in version 3.0)
Bug Fixes
- bugfix `disksIO` - on OSX read and write got mixed up
- several bug fixes (like assess errors in `cpuCurrentspeed`, potentially incorrect results in `users`, ...)
- testet on even more platforms and linux distributions
**Be aware**, that the new version 3.x is **NOT fully backward compatible** to version 2.x ...
### Major (breaking) Changes - Version 2
There had been a lot of changes in version 2 of systeminformation! Here is a quick overview (for those who come from version 1):
New Functions
- `version`: returns systeminformation version (semver) of this library
- `system`: hardware info (manufacturer, product/model name, version, serial, uuid)
- `networkConnections`: number of active connections
- `inetLatency`: latency in ms to external resource (internet)
- `getStaticData`: returns on json object with static data at once (OS, CPU, Network Interfaces - they should not change until restarted)
- `getDynamicData`: returns on json object with all dynamic data at once (e.g. for monitoring agents)
- `getAllData`: returns on json object with all data (static and dynamic) at once
Renamed Functions (now all camelCase)
- `osinfo`: renamed to `osInfo`
- `cpu_currentspeed`: renamed to `cpuCurrentspeed`
- `cpu_temperature`: renamed to `cpuTemperature`
- `fs_size`: renamed to `fsSize`
- `fs_speed`: renamed to `fsStats`
- `network_interfaces`: renamed to `networkInterfaces`
- `network_speed`: renamed to `networkStats`
- `network_connections`: renamed to `networkConnections`
- `currentload`: renamed to `currentLoad`
- `fullload`: renamed to `fullLoad`
- `processload`: renamed to `processLoad`
- `checksite`: renamed to `inetChecksite`
Function Changes
- `cpu_temperature`/`cpuTemperature`: -1 is new default (and indicates that non sensors are installed)
- `cpu_temperature`/`cpuTemperature`: new result `max` which returns max temperature of all cores
- `cpu_currentspeed`/`cpuCurrentspeed`: now in GHz
- `cpu`: splitted `manufacturer` (e.g. Intel) and `brand` (e.g. Core 2 Duo)
- `network_speed`/`networkStats`: now better support for OS X (also support for `operstate`)
- `network_speed`/`networkStats`: overall received and transferred bytes (rx, tx)
- `mem`: now better support for OS X (also support for `swaptotal`, `swapused`, `swapfree`)
- `fs_size`/`fsSize`: use-values now in % (0 - 100% instead of 0 - 1)
- `fs_speed`/`fsStats`: now also full support for OS X
- `checksite`/`inetChecksite`: new result structure - see command reference
- `checksite`/`inetChecksite`: ms (former `response_ms`): -1 if not ok
Other changes
- no more external dependencies: `request` is not longer needed
- where possible results are now integer or float values (instead of strings) because it is easier to calculate with numbers ;-)
You can find all changes here: [detailed changelog][changelog-url]
## Core concept
@ -148,6 +66,9 @@ Since version 2 nearly all functionality is available on OS X/Darwin platforms.
If you have comments, suggestions & reports, please feel free to contact me!
I also created a little command line tool called [mmon][mmon-github-url] (micro-monitor), also available via [github][mmon-github-url] and [npm][mmon-npm-url]
## Reference
### Sections
@ -171,8 +92,8 @@ This library is splitted in several sections:
| function | Linux | OS X | Comments |
| -------------- | ------ | ------ | ------- |
| si.version() | X | X | systeminformation version (no callback!) |
| si.time() | X | X | time information (no callback!) |
| si.version() | X | X | library version (no callback/promise) |
| si.time() | X | X | time information (no callback/promise) |
| - current | X | X | local time |
| - uptime | X | X | uptime |
| si.system(cb) | X | X | hardware information |
@ -335,6 +256,10 @@ This library is splitted in several sections:
| - netIO.wx | X | X | sent bytes via network |
| - blockIO.r | X | X | bytes read from BlockIO |
| - blockIO.w | X | X | bytes written to BlockIO |
| - cpu_stats | X | X | detailed cpu stats |
| - percpu_stats | X | X | detailed per cpu stats |
| - memory_stats | X | X | detailed memory stats |
| - networks | X | X | detailed network stats per interface |
| si.dockerAll(cb) | X | X | list of all containers including their stats<br>in one single array |
| si.getStaticData(cb) | X | X | all static data at once |
| si.getDynamicData(srv,iface,cb) | X | X | all dynamic data at once |
@ -393,43 +318,6 @@ For OS X, I did not find a reliable way to get the CPU temperature. All suggesti
I am happy to discuss any comments and suggestions. Please feel free to contact me if you see any possibility of improvement!
## Version history
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.6.0 | 2016-09-14 | added versions (kernel, ssl, node, npm, pm2, ...) |
| 3.5.1 | 2016-09-14 | bugfix graphics info |
| 3.5.0 | 2016-09-14 | added graphics info (controller, display) |
| 3.4.4 | 2016-09-02 | tiny fixes system.model, getDefaultNetworkInterface |
| 3.4.3 | 2016-09-02 | tiny bug fix fsStats, disksIO OSX |
| 3.4.2 | 2016-09-01 | improved default network interface |
| 3.4.1 | 2016-08-30 | updated docs |
| 3.4.0 | 2016-08-30 | rewritten processes current cpu usage |
| 3.3.0 | 2016-08-24 | process list added to processes |
| 3.2.1 | 2016-08-19 | updated docs, improvement system |
| 3.2.0 | 2016-08-19 | added battery information |
| 3.1.1 | 2016-08-18 | improved system and os detection (vm, ...), bugfix disksIO |
| 3.1.0 | 2016-08-18 | added Docker stats |
| 3.0.1 | 2016-08-17 | Bug-Fix disksIO, users, updated docs |
| 3.0.0 | 2016-08-03 | new major version 3.0 |
| 2.0.5 | 2016-03-02 | changed .gitignore |
| 2.0.4 | 2016-02-22 | tiny correction - removed double quotes CPU brand, ... |
| 2.0.3 | 2016-02-22 | optimized cpuCurrentspeed |
| 2.0.2 | 2016-02-22 | added CoreOS identification |
| 2.0.1 | 2016-01-07 | minor patch |
| 2.0.0 | 2016-01-07 | new major version 2.0 |
| 1.0.7 | 2015-11-27 | fixed: si.network_speed() |
| 1.0.6 | 2015-09-17 | fixed: si.users() |
| 1.0.5 | 2015-09-14 | updated dependencies |
| 1.0.4 | 2015-07-18 | updated docs |
| 1.0.3 | 2015-07-18 | bugfix cpu cores |
| 1.0.2 | 2015-07-18 | bugfix cpu_currentspeed, cpu_temperature |
| 1.0.1 | 2015-07-18 | documentation update |
| 1.0.0 | 2015-07-18 | bug-fixes, version bump, published as npm component |
| 0.0.3 | 2014-04-14 | bug-fix (cpu_speed) |
| 0.0.2 | 2014-03-14 | Optimization FS-Speed & CPU current speed |
| 0.0.1 | 2014-03-13 | initial release |
## Comments
If you have ideas or comments, please do not hesitate to contact me.
@ -495,6 +383,7 @@ All other trademarks are the property of their respective owners.
[license-url]: https://github.com/sebhildebrandt/systeminformation/blob/master/LICENSE
[license-img]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
[npmjs-license]: https://img.shields.io/npm/l/systeminformation.svg?style=flat-square
[changelog-url]: https://github.com/sebhildebrandt/systeminformation/blob/master/CHANGELOG.md
[nodejs-url]: https://nodejs.org/en/
[docker-url]: https://www.docker.com/

106
lib/dockerSocket.js Normal file
View File

@ -0,0 +1,106 @@
'use strict';
const net = require('net');
class DockerSocket {
listContainers(all, callback) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let alldata = '';
socket.on("connect", () => {
socket.write('GET http:/containers/json' + (all ? "?all=1" : "") + ' HTTP/1.0\r\n\r\n');
});
socket.on("data", data => {
alldata = alldata + data.toString();
});
socket.on("error", () => {
socket = false;
callback({});
});
socket.on('end', () => {
let startbody = alldata.indexOf("\r\n\r\n");
alldata = alldata.substring(startbody, 100000).replace(/[\n\r]/g, '');
socket = false;
callback(JSON.parse(alldata));
});
} catch (err) {
callback({});
}
}
getStats(id, callback) {
id = id || '';
if (id) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let alldata = '';
socket.on("connect", () => {
socket.write('GET http:/containers/' + id + '/stats?stream=0 HTTP/1.0\r\n\r\n');
});
socket.on("data", data => {
alldata = alldata + data.toString();
});
socket.on("error", () => {
socket = false;
callback({});
});
socket.on('end', () => {
let startbody = alldata.indexOf("\r\n\r\n");
alldata = alldata.substring(startbody, 100000).replace(/[\n\r]/g, '');
socket = false;
callback(JSON.parse(alldata));
});
} catch (err) {
callback({});
}
} else {
callback({});
}
}
getProcesses(id, callback) {
id = id || '';
if (id) {
try {
let socket = net.createConnection({path: '/var/run/docker.sock'});
let alldata = '';
socket.on("connect", () => {
socket.write('GET http:/containers/' + id + '/top?ps_args=-opid,ppid,pgid,vsz,time,etime,nice,ruser,user,rgroup,group,stat,rss,args HTTP/1.0\r\n\r\n');
});
socket.on("data", data => {
alldata = alldata + data.toString();
});
socket.on("error", () => {
socket = false;
callback({});
});
socket.on('end', () => {
let startbody = alldata.indexOf("\r\n\r\n");
alldata = alldata.substring(startbody, 100000).replace(/[\n\r]/g, '');
socket = false;
callback(JSON.parse(alldata));
});
} catch (err) {
callback({});
}
} else {
callback({});
}
}
}
module.exports = DockerSocket;

View File

@ -81,6 +81,7 @@
// --------------------------------
//
// version date comment
// 3.7.0 2016-11-02 extended docker stats, and no longer relying on curl (version conflicts)
// 3.6.0 2016-09-16 added versions (kernel, ssl, node, npm, pm2, ...)
// 3.5.1 2016-09-14 bugfix graphics info
// 3.5.0 2016-09-14 added graphics info (controller, display)
@ -124,7 +125,10 @@ const os = require('os')
, exec = require('child_process').exec
, execSync = require('child_process').execSync
, fs = require('fs')
, lib_version = require('../package.json').version;
, lib_version = require('../package.json').version
, DockerSocket = require('./dockerSocket');
var _cores = 0;
var _platform = os.type();
@ -137,6 +141,7 @@ var _fs_speed = {};
var _disk_io = {};
var _default_iface;
var _docker_container_stats = {};
var _docker_socket;
var _process_cpu = {
all: 0,
list: {},
@ -2694,12 +2699,20 @@ function dockerContainers(all, callback) {
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/json" + (all ? "?all=1" : "");
exec(cmd, function (error, stdout) {
if (!error) {
try {
let jsonString = stdout.toString();
var docker_containers = JSON.parse(jsonString);
if (!_docker_socket) {
_docker_socket = new DockerSocket();
}
_docker_socket.listContainers(all, data => {
var docker_containers = {};
// let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/json" + (all ? "?all=1" : "");
// exec(cmd, function (error, stdout) {
// if (!error) {
try {
// let jsonString = stdout.toString();
// var docker_containers = JSON.parse(jsonString);
docker_containers = data;
if (docker_containers && Object.prototype.toString.call(docker_containers) === '[object Array]' && docker_containers.length > 0) {
docker_containers.forEach(function (element) {
/**
@ -2736,7 +2749,7 @@ function dockerContainers(all, callback) {
}
} catch (err) {
}
}
// }
// GC in _docker_container_stats
for (var key in _docker_container_stats) {
@ -2862,12 +2875,19 @@ function dockerContainerStats(containerID, callback) {
reject(error);
}
if (containerID) {
let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/" + containerID + "/stats?stream=0";
exec(cmd, function (error, stdout) {
if (!error) {
let jsonString = stdout.toString();
if (!_docker_socket) {
_docker_socket = new DockerSocket();
}
_docker_socket.getStats(containerID, data => {
// let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/" + containerID + "/stats?stream=0";
// exec(cmd, function (error, stdout) {
// if (!error) {
// let jsonString = stdout.toString();
try {
let stats = JSON.parse(jsonString);
// let stats = JSON.parse(jsonString);
let stats = data;
/**
* @namespace
* @property {Object} memory_stats
@ -2886,12 +2906,16 @@ function dockerContainerStats(containerID, callback) {
result.mem_percent = (stats.memory_stats && stats.memory_stats.usage && stats.memory_stats.limit ? stats.memory_stats.usage / stats.memory_stats.limit * 100.0 : 0);
result.cpu_percent = (stats.cpu_stats ? docker_calcCPUPercent(stats.cpu_stats, containerID) : 0);
result.pids = (stats.pids_stats && stats.pids_stats.current ? stats.pids_stats.current : 0);
if (stats.networks) result.netIO = docker_calcNetworkIO(stats.networks);
if (stats.blkio_stats)result.blockIO = docker_calcBlockIO(stats.blkio_stats);
if (stats.networks) result.netIO = docker_calcNetworkIO(stats.networks);
if (stats.blkio_stats) result.blockIO = docker_calcBlockIO(stats.blkio_stats);
result.cpu_stats = (stats.cpu_stats ? stats.cpu_stats : {});
result.precpu_stats = (stats.precpu_stats ? stats.precpu_stats : {});
result.memory_stats = (stats.memory_stats ? stats.memory_stats : {});
result.networks = (stats.networks ? stats.networks : {});
}
} catch (err) {
}
}
// }
if (callback) { callback(result) }
resolve(result);
});
@ -2926,6 +2950,10 @@ function dockerAll(callback) {
element.pids = res.pids;
element.netIO = res.netIO;
element.blockIO = res.blockIO;
element.cpu_stats = res.cpu_stats;
element.precpu_stats = res.precpu_stats;
element.memory_stats = res.memory_stats;
element.networks = res.networks;
// all done??
l -= 1;