Skip to content

Commit

Permalink
init github version
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Feb 8, 2015
0 parents commit 009919b
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.swp
*.swo
.DS_Store
/agent
/paas-agent
/dinp-agent
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Agent
==========

这个模块部署在资源池的所有节点机器上,主要用于收集数据做汇报

收集的数据分成两部分

- 机器剩余内存,调度模块拿到这个信息之后才能做调度
- container情况,这是读取的Docker Daemon的接口,list所有container

对于container这块,目前只是拿到了本机的container列表,得知这些container的PublicPort,汇报给server,server把路由信息写入redis中的路由表

#### Q1:如何得知某个container是哪个app的呢?

刚开始的做法是把app的名称写入image的url中,container本身是可以知道使用了哪个image创建的,这样就知道了app与container的对
应关系。但是这个规范比较强,太具侵入性

现在的做法是把app的名称写入ENV,在创建container的时候写入,之后再通过inspect拿到ENV["APP\_NAME"]

## 注意:

- agent会把本机的ip汇报给server,server用此构建路由表,那本机的ip是如何获取的呢?多个ip的情况怎么办?现在的做法是只拿内网ip,然后过滤掉回环ip,只要网卡名称使用eth打头的,过滤掉Docker创建的虚拟网卡,如果用户配置了localIp,就直接使用用户配置的localIp
- agent是个前台进程,真正线上部署的时候可以使用god或者supervisor之类的管理

## 配置项说明

- **debug**: true/false 只影响打印的log
- **localIp**: 本机Ip地址,server会用这个ip地址和docker daemon通信
- **servers**: server的地址
- **interval**: 心跳周期,单位是秒
- **timeout**: 连接server的超时时间,单位是毫秒
- **docker**: Docker Daemon的接口地址,推荐Docker Daemon监听一个127.0.0.1的tcp接口,unix socket也可以,不过要注意文件权限了

## install

```
mkdir -p $GOPATH/src/github.com/dinp
cd $GOPATH/src/github.com/dinp; git clone https://github.com/dinp/agent.git
cd agent
go get ./...
# check cfg.json, depend docker daemon and server
./control start
```

11 changes: 11 additions & 0 deletions cfg.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"debug": true,
"localIp": "",
"servers": [
"10.1.2.3:1970",
"10.1.2.4:1970"
],
"interval": 5,
"timeout": 1000,
"docker": "http://127.0.0.1:2375"
}
70 changes: 70 additions & 0 deletions cron/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cron

import (
"github.com/dinp/agent/g"
"github.com/dinp/common/dock"
"github.com/dinp/common/model"
"github.com/toolkits/nux"
"log"
"time"
)

func Heartbeat() {
duration := time.Duration(g.Config().Interval) * time.Second
for {
heartbeat(duration)
time.Sleep(duration)
}
}

func heartbeat(duration time.Duration) {
mem, err := nux.MemInfo()
if err != nil {
log.Println("[ERROR] get meminfo:", err)
return
}

// use MB
memFree := (mem.MemFree + mem.Buffers + mem.Cached) / 1024 / 1024

localIp := g.Config().LocalIp

containers, err := dock.Containers(g.Config().Docker)
if err != nil {
log.Println("[ERROR] list containers fail:", err)

// this node dead
var resp model.NodeResponse
err = g.RpcClient.Call("NodeState.NodeDown", localIp, &resp)
if err != nil || resp.Code != 0 {
log.Println("[ERROR] call rpc: NodeState.NodeDown fail:", err, "resp:", resp)
} else if g.Config().Debug {
log.Println("[INFO] call rpc: NodeState.NodeDown successfully. I am dead...")
}

for {
time.Sleep(duration)
containers, err = dock.Containers(g.Config().Docker)
if err == nil {
break
} else {
log.Println("[ERROR] list containers fail:", err)
}
}
}

req := model.NodeRequest{
Node: model.Node{
Ip: localIp,
MemFree: memFree,
},
Containers: containers,
}
var resp model.NodeResponse
err = g.RpcClient.Call("NodeState.Push", req, &resp)
if err != nil || resp.Code != 0 {
log.Println("[ERROR] call rpc: NodeState.Push fail:", err, "resp:", resp)
} else if g.Config().Debug {
log.Println("[DEBUG] =>", req)
}
}
76 changes: 76 additions & 0 deletions g/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package g

import (
"encoding/json"
"github.com/toolkits/file"
"github.com/toolkits/net"
"log"
"sync"
)

type GlobalConfig struct {
Debug bool `json:"debug"`
LocalIp string `json:"localIp"`
Servers []string `json:"servers"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
Docker string `json:"docker"`
}

var (
ConfigFile string
config *GlobalConfig
configLock = new(sync.RWMutex)
)

func Config() *GlobalConfig {
configLock.RLock()
defer configLock.RUnlock()
return config
}

func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
}

if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent")
}

ConfigFile = cfg

configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}

var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}

if c.LocalIp == "" {
// detect local ip
localIps, err := net.IntranetIP()
if err != nil {
log.Fatalln("get intranet ip fail:", err)
}

if len(localIps) == 0 {
log.Fatalln("no intranet ip found")
}

c.LocalIp = localIps[0]
}

configLock.Lock()
defer configLock.Unlock()

config = &c

if config.Debug {
log.Println("read config file:", cfg, "successfully")
}
}
15 changes: 15 additions & 0 deletions g/g.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g

import (
"log"
"runtime"
)

const (
VERSION = "1.0.0"
)

func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
17 changes: 17 additions & 0 deletions g/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g

import (
"github.com/dinp/common/rpc"
"time"
)

var (
RpcClient *rpc.SingleConnRpcClient
)

func InitRpcClient() {
RpcClient = &rpc.SingleConnRpcClient{
RpcServers: Config().Servers,
Timeout: time.Duration(Config().Timeout) * time.Millisecond,
}
}
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"flag"
"fmt"
"github.com/dinp/agent/cron"
"github.com/dinp/agent/g"
"os"
)

func main() {
cfg := flag.String("c", "cfg.json", "configuration file")
version := flag.Bool("v", false, "show version")
flag.Parse()

if *version {
fmt.Println(g.VERSION)
os.Exit(0)
}

g.ParseConfig(*cfg)
g.InitRpcClient()

cron.Heartbeat()
}

0 comments on commit 009919b

Please sign in to comment.