/
docker_stats_decoder.go
77 lines (67 loc) · 2.29 KB
/
docker_stats_decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package dockerstats
import (
"bytes"
"encoding/json"
"fmt"
"github.com/mozilla-services/heka/pipeline"
)
type StatsPayload struct {
Hostname string `json:"hostname"`
ContainerName string `json:"container_name"`
CPUPercent float64 `json:"cpu_percent"`
MemPercent float64 `json:"mem_percent"`
MemUsage int64 `json:"mem_usage"`
MemLimit int64 `json:"mem_limit"`
NetworkRx int64 `json:"network_rx"`
NetworkTx int64 `json:"network_tx"`
BlockRead int64 `json:"block_read"`
BlockWrite int64 `json:"block_write"`
TimeStamp int64 `json:"timestamp"`
}
type DockerStatsDecoder struct {
*DockerStatsInputConfig
}
func (input *DockerStatsDecoder) Init(config interface{}) error {
return nil
}
func (input *DockerStatsDecoder) Decode(pack *pipeline.PipelinePack) (packs []*pipeline.PipelinePack, err error) {
var buf bytes.Buffer
buf = input.decode(pack)
pack.Message.SetPayload(string(buf.Bytes()))
packs = []*pipeline.PipelinePack{pack}
return
}
func (*DockerStatsDecoder) decode(pack *pipeline.PipelinePack) bytes.Buffer {
containerName, _ := pack.Message.GetFieldValue("ContainerName")
cpuPercent, _ := pack.Message.GetFieldValue("CPUPercent")
memoryPercent, _ := pack.Message.GetFieldValue("MemoryPercent")
memoryUsage, _ := pack.Message.GetFieldValue("MemoryUsage")
memoryLimit, _ := pack.Message.GetFieldValue("MemoryLimit")
networkInput, _ := pack.Message.GetFieldValue("NetworkInput")
networkOutput, _ := pack.Message.GetFieldValue("NetworkOutput")
blockInput, _ := pack.Message.GetFieldValue("BlockInput")
blockOutput, _ := pack.Message.GetFieldValue("BlockOutput")
stats := StatsPayload{
Hostname: *pack.Message.Hostname,
ContainerName: containerName.(string),
CPUPercent: cpuPercent.(float64),
MemPercent: memoryPercent.(float64),
MemUsage: memoryUsage.(int64),
MemLimit: memoryLimit.(int64),
NetworkRx: networkInput.(int64),
NetworkTx: networkOutput.(int64),
BlockRead: blockInput.(int64),
BlockWrite: blockOutput.(int64),
TimeStamp: *pack.Message.Timestamp,
}
json, err := json.Marshal(stats)
if err != nil {
fmt.Println(err)
}
return *bytes.NewBuffer(json)
}
func init() {
pipeline.RegisterPlugin("DockerStatsDecoder", func() interface{} {
return new(DockerStatsDecoder)
})
}