@@ -8,10 +8,7 @@ import logging.Logger
88import network.data.Endpoint
99import network.data.clusters.Cluster
1010import network.data.messages.Message
11- import utils.CircularList
1211import utils.runAfter
13- import java.nio.ByteBuffer
14- import java.util.*
1512import java.util.concurrent.ConcurrentHashMap
1613import java.util.concurrent.locks.ReentrantLock
1714import kotlin.concurrent.withLock
@@ -94,50 +91,11 @@ abstract class DockerProxy(configuration: Configuration) : MigrationStrategy(con
9491 val numberOfElements = (configuration.slotDuration / 1000 ).toInt()
9592
9693 val process = ProcessBuilder ()
97- .command(" docker" , " stats" , " --no-trunc" , " --format" , " {{.ID}} {{.CPUPerc}} {{.MemPerc}} {{.PIDs }}" )
94+ .command(" docker" , " stats" , " --no-stream " , " --no- trunc" , " --format" , " {{ json . }}" )
9895 .redirectErrorStream(true )
9996 .start()
100-
101- val buffer = ByteBuffer .allocate(100_000 )
102- val escapeSequence = byteArrayOf(0x1B , 0x5B , 0x32 , 0x4A , 0x1B , 0x5B , 0x48 ) // Escape sequence of CLI output.
103- var escapeIndex = 0
104- process.inputStream.use { inputStream ->
105- while (true ) {
106- try {
107- val byte = inputStream.read().toByte()
108- if (byte < 0 ) break
109- buffer.put(byte)
110- if (byte == escapeSequence[escapeIndex]) escapeIndex++ else escapeIndex = 0
111- if (escapeIndex != escapeSequence.size) continue // If escape sequence was not detected, continue reading data.
112- val length = buffer.position() - escapeSequence.size
113- if (length > 0 ) String (buffer.array(), 0 , length).split(" \n " ).map { line ->
114- if (line.isNotEmpty()) {
115- val fields = line.split(" " )
116- val containerId = fields[0 ]
117- if (fields.none { it.contains(" -" ) || it.isEmpty() }) {
118- val cpuPercentage = fields[1 ].trim(' %' ).toDouble()
119- val memoryPercentage = fields[2 ].trim(' %' ).toDouble()
120- val activeProcesses = fields[3 ].toInt()
121- val container = localContainers.computeIfAbsent(containerId) {
122- DockerContainer (containerId, activeProcesses, CircularList (numberOfElements), CircularList (numberOfElements))
123- }
124- container.apply {
125- cpuUsage.add(cpuPercentage)
126- memoryUsage.add(memoryPercentage)
127- updated = System .currentTimeMillis()
128- processes = activeProcesses
129- }
130- } else localContainers[containerId]?.updated = System .currentTimeMillis()
131- }
132- }
133- buffer.clear()
134- escapeIndex = 0
135- } catch (e: Exception ) {
136- buffer.clear()
137- escapeIndex = 0
138- Dashboard .reportException(e)
139- }
140- }
97+ process.inputReader().readLines().forEach { line ->
98+ println (" Read line from docker stats: $line " )
14199 }
142100
143101 }
0 commit comments