Skip to content

Commit 8831552

Browse files
committed
init
0 parents  commit 8831552

10 files changed

+605
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cadvisor
2+
run
3+
uploadCadvisorData
4+
.swp

Diff for: Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM progrium/busybox
2+
3+
4+
MAINTAINER Meng Zhuo <[email protected]>
5+
6+
ENV Interval 60
7+
8+
ADD cadvisor /home/work/uploadCadviosrData/cadvisor
9+
10+
ADD uploadCadvisorData /home/work/uploadCadviosrData/uploadCadvisorData
11+
12+
ADD run /home/work/uploadCadviosrData/run
13+
14+
RUN mkdir -p /home/work/uploadCadviosrData/log
15+
16+
EXPOSE 8080
17+
18+
19+
ENTRYPOINT ["/home/work/uploadCadviosrData/run"]
20+

Diff for: README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Docker container monitor plugin for Open-Falcon Micadvisor-Open
2+
------------------------------------
3+
------------------------------------
4+
描述
5+
------------------
6+
micadvisor-open是基于open-falcon的docker容器资源监控插件,监控容器的cpu、内存、diskio以及网络io等,数据采集后上报到open-falcon
7+
8+
biuld方法:
9+
-----------------
10+
./build
11+
12+
启动方法:
13+
-----------------
14+
docker run \
15+
--volume=/:/rootfs:ro \
16+
--volume=/var/run:/var/run:rw \
17+
--volume=/sys:/sys:ro \
18+
--volume=/home/work/log/cadvisor/:/home/work/uploadCadviosrData/log \
19+
--volume=/var/lib/docker/:/var/lib/docker:ro \
20+
--volume=/home/docker/containers:/home/docker/containers:ro \
21+
--publish=18080:18080 \
22+
--env Interval=60 \
23+
--detach=true \
24+
--name=micadvisor \
25+
--net=host \
26+
--restart=always \
27+
micadvisor:latest
28+
29+
注:
30+
--volume=/sys:/sys:ro 此volume中包含docker容器监控所需要的重要内容,如/sys/fs/cgroup下的相关内容
31+
--volume=/home/work/log/cadvisor/:/home/work/uploadCadviosrData/log \ 为日志内容路径
32+
--env Interval=60 表示提取数据的间隔时间
33+
34+
采集的指标
35+
--------------------------
36+
| Counters | Notes|
37+
|-----|------|
38+
|cpu.busy|cpu使用情况百分比|
39+
|cpu.user|用户态使用的CPU百分比|
40+
|cpu.system|内核态使用的CPU百分比|
41+
|cpu.core.busy|每个cpu的使用情况|
42+
|mem.memused.percent|内存使用百分比|
43+
|mem.memused|内存使用原值|
44+
|mem.memtotal|内存总量|
45+
|mem.memused.hot|内存热使用情况|
46+
|disk.io.read_bytes|磁盘io读字节数|
47+
|disk.io.write_bytes|磁盘io写字节数|
48+
|net.if.in.bytes|网络io流入字节数|
49+
|net.if.in.packets|网络io流入包数|
50+
|net.if.in.errors|网络io流入出错数|
51+
|net.if.in.dropped|网络io流入丢弃数|
52+
|net.if.out.bytes|网络io流出字节数|
53+
|net.if.out.packets|网络io流出包数|
54+
|net.if.out.errors|网络io流出出错数|
55+
|net.if.out.dropped|网络io流出丢弃数|
56+
57+
Contributors
58+
------------------------------------------
59+
- mengzhuo: QQ:296142139; MAIL:mengzhuo@xiaomi.com

Diff for: build.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
go build run.go
4+
[ $? -ne 0 ] && exit 1
5+
6+
go build uploadCadvisorData.go pushDatas.go mylog.go getDatas.go dataFunc.go
7+
[ $? -ne 0 ] && exit 1
8+
9+
docker build -t micadvisor ./
10+
11+

Diff for: dataFunc.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"strings"
7+
)
8+
9+
func pushIt(value, timestamp, metric, tags, containerId, counterType, endpoint string) error {
10+
11+
postThing := `[{"metric": "` + metric + `", "endpoint": "` + endpoint + `", "timestamp": ` + timestamp + `,"step": ` + "60" + `,"value": ` + value + `,"counterType": "` + counterType + `","tags": "` + tags + `"}]`
12+
LogRun(postThing)
13+
//push data to falcon-agent
14+
url := "http://127.0.0.1:1988/v1/push"
15+
resp, err := http.Post(url,
16+
"application/x-www-form-urlencoded",
17+
strings.NewReader(postThing))
18+
if err != nil {
19+
LogErr(err, "Post err in pushIt")
20+
return err
21+
}
22+
defer resp.Body.Close()
23+
_, err1 := ioutil.ReadAll(resp.Body)
24+
if err1 != nil {
25+
LogErr(err1, "ReadAll err in pushIt")
26+
return err1
27+
}
28+
return nil
29+
}

Diff for: getDatas.go

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"net"
9+
"net/http"
10+
"net/http/httputil"
11+
"net/url"
12+
"os"
13+
"regexp"
14+
"strconv"
15+
"strings"
16+
)
17+
18+
func getCpuNum(dockerdata string) {
19+
cpuNum = 1
20+
tmp := getBetween(dockerdata, `"CPU=`, `",`)
21+
if tmp != "" {
22+
cpuNum, _ = strconv.ParseInt(tmp, 10, 32)
23+
if cpuNum == 0 {
24+
cpuNum = 1
25+
}
26+
}
27+
}
28+
29+
func getTag() string {
30+
//FIXMI:some other message for container
31+
return ""
32+
}
33+
34+
func getMemLimit(str string) string {
35+
return getBetween(str, `"memory":{"limit":`, `,"`)
36+
}
37+
38+
func getBetween(str, start, end string) string {
39+
res := regexp.MustCompile(start + `(.+?)` + end).FindStringSubmatch(str)
40+
if len(res) <= 1 {
41+
LogErr(errors.New("regexp len < 1"), start+" "+end)
42+
return ""
43+
}
44+
return res[1]
45+
}
46+
47+
func getCadvisorData() (string, error) {
48+
var (
49+
resp *http.Response
50+
err error
51+
body []byte
52+
)
53+
url := "http://localhost:" + CadvisorPort + "/api/v1.2/docker"
54+
if resp, err = http.Get(url); err != nil {
55+
LogErr(err, "Get err in getCadvisorData")
56+
return "", err
57+
}
58+
defer resp.Body.Close()
59+
if body, err = ioutil.ReadAll(resp.Body); err != nil {
60+
LogErr(err, "ReadAll err in getCadvisorData")
61+
return "", err
62+
}
63+
64+
return string(body), nil
65+
}
66+
67+
func getUsageData(cadvisorData string) (ausge, busge string) {
68+
ausge = strings.Split(cadvisorData, `{"timestamp":`)[1]
69+
if len(strings.Split(cadvisorData, `{"timestamp":`)) < 11 {
70+
countNum = 1
71+
busge = strings.Split(cadvisorData, `{"timestamp":`)[2]
72+
} else {
73+
busge = strings.Split(cadvisorData, `{"timestamp":`)[11]
74+
countNum = 10
75+
}
76+
77+
return ausge, busge
78+
}
79+
80+
func getContainerId(cadvisorData string) string {
81+
82+
getContainerId1 := strings.Split(cadvisorData, `],"namespace"`)
83+
getContainerId2 := strings.Split(getContainerId1[0], `","`)
84+
getContainerId3 := strings.Split(getContainerId2[1], `"`)
85+
containerId := getContainerId3[0]
86+
87+
return containerId
88+
}
89+
90+
func getEndPoint(DockerData string) string {
91+
filepath := getBetween(DockerData, `"HostsPath":"`, `",`)
92+
buf := make(map[int]string, 6)
93+
inputFile, inputError := os.Open(filepath)
94+
if inputError != nil {
95+
LogErr(inputError, "getEndPoint open file err"+filepath)
96+
return ""
97+
}
98+
defer inputFile.Close()
99+
100+
inputReader := bufio.NewReader(inputFile)
101+
lineCounter := 0
102+
for i := 0; i < 2; i++ {
103+
inputString, readerError := inputReader.ReadString('\n')
104+
if readerError == io.EOF {
105+
break
106+
}
107+
lineCounter++
108+
buf[lineCounter] = inputString
109+
}
110+
hostname := strings.Split(buf[1], " ")[0]
111+
return hostname
112+
}
113+
114+
func getDockerData(containerId string) (string, error) {
115+
str, err := RequestUnixSocket("/containers/"+containerId+"/json", "GET")
116+
if err != nil {
117+
LogErr(err, "getDockerData err")
118+
}
119+
return str, nil
120+
}
121+
122+
func RequestUnixSocket(address, method string) (string, error) {
123+
DOCKER_UNIX_SOCKET := "unix:///var/run/docker.sock"
124+
// Example: unix:///var/run/docker.sock:/images/json?since=1374067924
125+
unix_socket_url := DOCKER_UNIX_SOCKET + ":" + address
126+
u, err := url.Parse(unix_socket_url)
127+
if err != nil || u.Scheme != "unix" {
128+
LogErr(err, "Error to parse unix socket url "+unix_socket_url)
129+
return "", err
130+
}
131+
132+
hostPath := strings.Split(u.Path, ":")
133+
u.Host = hostPath[0]
134+
u.Path = hostPath[1]
135+
136+
conn, err := net.Dial("unix", u.Host)
137+
if err != nil {
138+
LogErr(err, "Error to connect to"+u.Host)
139+
// fmt.Println("Error to connect to", u.Host, err)
140+
return "", err
141+
}
142+
143+
reader := strings.NewReader("")
144+
query := ""
145+
if len(u.RawQuery) > 0 {
146+
query = "?" + u.RawQuery
147+
}
148+
149+
request, err := http.NewRequest(method, u.Path+query, reader)
150+
if err != nil {
151+
LogErr(err, "Error to create http request")
152+
// fmt.Println("Error to create http request", err)
153+
return "", err
154+
}
155+
156+
client := httputil.NewClientConn(conn, nil)
157+
response, err := client.Do(request)
158+
if err != nil {
159+
LogErr(err, "Error to achieve http request over unix socket")
160+
// fmt.Println("Error to achieve http request over unix socket", err)
161+
return "", err
162+
}
163+
164+
body, err := ioutil.ReadAll(response.Body)
165+
if err != nil {
166+
LogErr(err, "Error, get invalid body in answer")
167+
// fmt.Println("Error, get invalid body in answer")
168+
return "", err
169+
}
170+
171+
defer response.Body.Close()
172+
173+
return string(body), err
174+
}

Diff for: mylog.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"time"
7+
)
8+
9+
var errLog = "/home/work/uploadCadviosrData/log/err_log_uploadCadvisorData.txt"
10+
var runLog = "/home/work/uploadCadviosrData/log/run_log_uploadCadvisorData.txt"
11+
12+
func LogErr(errInfo interface{}, str string) {
13+
lf, err := os.OpenFile(errLog, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
14+
if err != nil {
15+
os.Exit(1)
16+
}
17+
defer lf.Close()
18+
19+
l := log.New(lf, "", os.O_APPEND)
20+
21+
l.Println(time.Now().Format("2006-01-02 15:04:05"), errInfo, str)
22+
}
23+
24+
func LogRun(str string) {
25+
lf, err := os.OpenFile(runLog, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
26+
if err != nil {
27+
os.Exit(1)
28+
}
29+
defer lf.Close()
30+
31+
l := log.New(lf, "", os.O_APPEND)
32+
33+
l.Printf("%s", time.Now().Format("2006-01-02 15:04:05"), str)
34+
}

0 commit comments

Comments
 (0)