forked from open-falcon/micadvisor_open
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetDatas.go
157 lines (134 loc) · 3.68 KB
/
getDatas.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"errors"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strconv"
"strings"
)
func getCpuNum(dockerdata string) {
cpuNum = 1
tmp := getBetween(dockerdata, `"CPU=`, `",`)
if tmp != "" {
cpuNum, _ = strconv.ParseInt(tmp, 10, 32)
if cpuNum == 0 {
cpuNum = 1
}
}
}
func getTag() string {
//FIXMI:some other message for container
return ""
}
func getMemLimit(str string) string {
return getBetween(str, `"memory":{"limit":`, `,"`)
}
func getBetween(str, start, end string) string {
res := regexp.MustCompile(start + `(.+?)` + end).FindStringSubmatch(str)
if len(res) <= 1 {
LogErr(errors.New("regexp len < 1"), start+" "+end)
return ""
}
return res[1]
}
func getCadvisorData() (string, error) {
var (
resp *http.Response
err error
body []byte
)
url := "http://localhost:" + CadvisorPort + "/api/v1.2/docker"
if resp, err = http.Get(url); err != nil {
LogErr(err, "Get err in getCadvisorData")
return "", err
}
defer resp.Body.Close()
if body, err = ioutil.ReadAll(resp.Body); err != nil {
LogErr(err, "ReadAll err in getCadvisorData")
return "", err
}
return string(body), nil
}
func getUsageData(cadvisorData string) (ausge, busge string) {
ausge = strings.Split(cadvisorData, `{"timestamp":`)[1]
if len(strings.Split(cadvisorData, `{"timestamp":`)) < 11 {
countNum = 1
busge = strings.Split(cadvisorData, `{"timestamp":`)[2]
} else {
busge = strings.Split(cadvisorData, `{"timestamp":`)[11]
countNum = 10
}
return ausge, busge
}
func getContainerId(cadvisorData string) string {
getContainerId1 := strings.Split(cadvisorData, `],"namespace"`)
getContainerId2 := strings.Split(getContainerId1[0], `","`)
getContainerId3 := strings.Split(getContainerId2[1], `"`)
containerId := getContainerId3[0]
return containerId
}
func getEndPoint(DockerData string) string {
//get endpoint from env first
endPoint := getBetween(DockerData, `"EndPoint=`, `",`)
if endPoint != "" {
return endPoint
}
hostname := getBetween(DockerData, `"Hostname":"`, `",`)
return hostname
}
func getDockerData(containerId string) (string, error) {
str, err := RequestUnixSocket("/containers/"+containerId+"/json", "GET")
if err != nil {
LogErr(err, "getDockerData err")
}
return str, nil
}
func RequestUnixSocket(address, method string) (string, error) {
DOCKER_UNIX_SOCKET := "unix:///var/run/docker.sock"
// Example: unix:///var/run/docker.sock:/images/json?since=1374067924
unix_socket_url := DOCKER_UNIX_SOCKET + ":" + address
u, err := url.Parse(unix_socket_url)
if err != nil || u.Scheme != "unix" {
LogErr(err, "Error to parse unix socket url "+unix_socket_url)
return "", err
}
hostPath := strings.Split(u.Path, ":")
u.Host = hostPath[0]
u.Path = hostPath[1]
conn, err := net.Dial("unix", u.Host)
if err != nil {
LogErr(err, "Error to connect to"+u.Host)
// fmt.Println("Error to connect to", u.Host, err)
return "", err
}
reader := strings.NewReader("")
query := ""
if len(u.RawQuery) > 0 {
query = "?" + u.RawQuery
}
request, err := http.NewRequest(method, u.Path+query, reader)
if err != nil {
LogErr(err, "Error to create http request")
// fmt.Println("Error to create http request", err)
return "", err
}
client := httputil.NewClientConn(conn, nil)
response, err := client.Do(request)
if err != nil {
LogErr(err, "Error to achieve http request over unix socket")
// fmt.Println("Error to achieve http request over unix socket", err)
return "", err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
LogErr(err, "Error, get invalid body in answer")
// fmt.Println("Error, get invalid body in answer")
return "", err
}
defer response.Body.Close()
return string(body), err
}