sebhildebrandt / systeminformation

System Information Library for Node.JS
MIT License
2.69k stars 305 forks source link

dockerStats throws exception on occasion #157

Closed hosein closed 5 years ago

hosein commented 6 years ago

Issue Type

Expected behavior

dockerStats to return JSON object with docker stats

Actual behavior

Usually works great, on occasion throws an 'Unexpected end of JSON input

Steps to reproduce the problem

Hard to say, happens after a few days of running (every second) or after a few minutes.

-

Specifications

Unexpected end of JSON input date=Mon Sep 17 2018 13:42:00 GMT+0000 (UTC), pid=1, uid=0, gid=0, cwd=/, execPath=/usr/bin/node, version=v8.12.0, argv=[/usr/bin/node, /var/monitor/manager.js], rss=53899264, heapTotal=33980416, heapUsed=22321152, external=866100, loadavg=[1.2919921875, 1.29541015625, 0.91455078125], uptime=7766189, trace=[column=null, file=null, function=JSON.parse, line=null, method=parse, native=false, column=25, file=/var/monitor/node_modules/systeminformation/lib/dockerSocket.js, function=Socket.socket.on, line=75, method=on, native=false, column=20, file=events.js, function=emitNone, line=111, method=null, native=false, column=7, file=events.js, function=Socket.emit, line=208, method=emit, native=false, column=12, file=_stream_readable.js, function=endReadableNT, line=1064, method=null, native=false, column=11, file=internal/process/next_tick.js, function=_combinedTickCallback, line=139, method=null, native=false, column=9, file=internal/process/next_tick.js, function=process._tickCallback, line=181, method=_tickCallback, native=false], stack=[SyntaxError: Unexpected end of JSON input, at JSON.parse (<anonymous>), at Socket.socket.on (/var/monitor/node_modules/systeminformation/lib/dockerSocket.js:75:25), at emitNone (events.js:111:20), at Socket.emit (events.js:208:7), at endReadableNT (_stream_readable.js:1064:12), at _combinedTickCallback (internal/process/next_tick.js:139:11), at process._tickCallback (internal/process/next_tick.js:181:9)]

Problem and possible solution

If you have a solution in mind (or any idea what could cause this issue), we would be happy, if you can describe it here ... It appears the conversion from the docker stats output into JSON is creating some incorrect data, perhaps a try->catch for the stringification?

hosein commented 6 years ago

I'll add my usage as well, since it's pretty simple:

setInterval(function(){
    si.dockerContainers(function(containers) {
        for (let c in containers){
            let container = containers[c];
            si.dockerContainerStats(container.id)
            .then(function(containerStats){
                //Do some things with the stats data for container [c]
            })
            .catch(function(e){
                //Report error
            })
        }
}),1000)
sebhildebrandt commented 5 years ago

Thank you for informing me about this. Just published a fix for this. Version 3.48.1