Skip to content

Commit 8031276

Browse files
committed
Added reading stats.
1 parent debd45b commit 8031276

2 files changed

Lines changed: 27 additions & 46 deletions

File tree

src/main/kotlin/docker/DockerContainer.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package docker
22

3+
import com.google.gson.annotations.SerializedName
34
import kotlinx.serialization.Serializable
45
import utils.CircularList
56

@@ -27,4 +28,26 @@ data class DockerContainer(
2728
) {
2829
val averageCpuUsage get(): Double = cpuUsage.elements().average().takeIf { !it.isNaN() } ?: 0.0
2930
val averageMemoryUsage get(): Double = memoryUsage.elements().average().takeIf { !it.isNaN() } ?: 0.0
30-
}
31+
}
32+
33+
@Serializable
34+
data class DockerStatsModel(
35+
@SerializedName("BlockIO")
36+
val blockIo: String,
37+
@SerializedName("CPUPerc")
38+
val cpuperc: String,
39+
@SerializedName("Container")
40+
val container: String,
41+
@SerializedName("ID")
42+
val id: String,
43+
@SerializedName("MemPerc")
44+
val memPerc: String,
45+
@SerializedName("MemUsage")
46+
val memUsage: String,
47+
@SerializedName("Name")
48+
val name: String,
49+
@SerializedName("NetIO")
50+
val netIo: String,
51+
@SerializedName("PIDs")
52+
val pids: String,
53+
)

src/main/kotlin/docker/DockerProxy.kt

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import logging.Logger
88
import network.data.Endpoint
99
import network.data.clusters.Cluster
1010
import network.data.messages.Message
11-
import utils.CircularList
1211
import utils.runAfter
13-
import java.nio.ByteBuffer
14-
import java.util.*
1512
import java.util.concurrent.ConcurrentHashMap
1613
import java.util.concurrent.locks.ReentrantLock
1714
import 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

Comments
 (0)