-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainerstat.go
123 lines (107 loc) · 3.06 KB
/
containerstat.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"strings"
)
type Metrics struct {
Name string
ImageId string
}
type StarStatus struct {
Name string
CPU string
Memory string
Mempct string
}
func (m Metrics) ServeHTTP(w http.ResponseWriter, req *http.Request) {
name := "StarStatus"
ContainerTypes := []string{}
ContainerTypes = append(ContainerTypes, "networkName", "Name", "CPU", "memory", "mempct")
stars := GetContainerMessage(m.ImageId)
for _, star := range stars {
data := []string{}
data = append(data, m.Name, star.Name, star.CPU, star.Memory, star.Mempct)
print(data)
print("\n")
if s, err := GeneratePromData(name, ContainerTypes, data); err == nil {
fmt.Fprint(w, s)
}
}
}
func GetContainerMessage(ImageId string) []StarStatus {
print("container\n")
// diskMessages := GetDiskMessage(ImageId)
cmd := exec.Command("bash", "-c", fmt.Sprintf("docker ps | grep %s", ImageId))
stdout, _ := cmd.CombinedOutput()
output := string(stdout)
// print(output)
containerMessages := strings.Split(output, "\n")
containerIds := make(map[string]bool)
for _, message := range containerMessages {
if len(message) != 0 {
words := strings.Split(message, " ")
containerIds[words[0]] = true
}
}
cmd = exec.Command("bash", "-c", fmt.Sprintf("docker stats --no-stream"))
stdout, _ = cmd.CombinedOutput()
output = string(stdout)
// print(output)
containerMessages = strings.Split(output, "\n")
starStatuses := make([]StarStatus, 0)
for _, line := range containerMessages {
datas := strings.Fields(line)
if len(datas) != 0 {
if _, ok := containerIds[datas[0]]; ok {
var starStatus StarStatus
words := strings.Split(datas[1], "_")
starStatus.Name = words[1]
starStatus.CPU = datas[2]
starStatus.Memory = datas[3]
starStatus.Mempct = datas[6]
// fmt.Println(starStatus)
starStatuses = append(starStatuses, starStatus)
}
}
}
return starStatuses
}
// get all the symbolized container's disk message
func GetDiskMessage(ImageId string) map[string][]string {
cmd := exec.Command("bash", "-c", fmt.Sprintf("docker system df -v | grep %s", ImageId))
stdout, _ := cmd.CombinedOutput()
outStr := string(stdout)
diskMessage := strings.Split(outStr, "\n")
diskMessages := make(map[string][]string, 0)
for _, line := range diskMessage {
words := strings.Fields(line)
if len(words) != 0 {
diskMessages[words[0]] = words
}
}
return diskMessages
}
func main() {
args := os.Args
metric := Metrics{args[1], args[2]}
http.Handle("/metrics", metric)
http.ListenAndServe(":2112", nil)
}
func GeneratePromData(name string, types []string, datas []string) (string, error) {
var result string
if len(types) != len(datas) {
return result, errors.New("lens of types is difference from lens of datas for prometheus type")
}
result = fmt.Sprintf("# HELP %s data structure\n# TYPE %s counter\n", name, name)
result += name + "{"
for index := range types {
result += fmt.Sprintf("%s=\"%s\",", types[index], datas[index])
}
result = result[:len(result)-1] + "} 1"
return result, nil
}
// c2be9bc309db