28
"github.com/cubefs/cubefs/proto"
29
"github.com/cubefs/cubefs/util/log"
33
RegisterPeriod = time.Duration(10) * time.Minute
34
RegisterPath = "/v1/agent/service/register"
41
type ConsulRegisterInfo struct {
42
Name string `json:"Name"`
44
Address string `json:"Address"`
45
Port int64 `json:"Port"`
46
Tags []string `json:"Tags"`
47
Meta map[string]string `json:",omitempty"`
51
func GetConsulId(app string, role string, host string, port int64) string {
52
return fmt.Sprintf("%s_%s_%s_%d", app, role, host, port)
56
func DoConsulRegisterProc(addr, app, role, cluster, meta, host string, port int64) {
60
log.LogInfof("metrics consul register %v %v %v", addr, cluster, port)
61
ticker := time.NewTicker(RegisterPeriod)
63
if err := recover(); err != nil {
64
log.LogErrorf("RegisterConsul panic,err[%v]", err)
69
client := &http.Client{}
70
req := makeRegisterReq(host, addr, app, role, cluster, meta, port)
72
log.LogErrorf("make register req error")
76
if resp, _ := client.Do(req); resp != nil {
82
req := makeRegisterReq(host, addr, app, role, cluster, meta, port)
84
log.LogErrorf("make register req error")
87
if resp, _ := client.Do(req); resp != nil {
95
func GetLocalIpAddr(filter string) (ipaddr string, err error) {
96
addrs, err := net.InterfaceAddrs()
98
log.LogError("consul register get local ip failed, ", err)
101
for _, addr := range addrs {
102
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
103
if ipnet.IP.To4() != nil {
104
ip := ipnet.IP.String()
107
match, err := doFilter(filter, ip)
109
return "", fmt.Errorf("regex match err, err %s", err.Error())
121
return "", fmt.Errorf("cannot get local ip")
125
func doFilter(filter, ip string) (ok bool, err error) {
127
if strings.HasPrefix(filter, "!") {
129
ok, err := regexp.MatchString(filter, ip)
133
ok, err = regexp.MatchString(filter, ip)
138
func makeRegisterReq(host, addr, app, role, cluster, meta string, port int64) (req *http.Request) {
139
id := GetConsulId(app, role, host, port)
140
url := addr + RegisterPath
141
cInfo := &ConsulRegisterInfo{
149
"cluster=" + cluster,
153
ok, metas := parseMetaStr(meta)
156
cInfo.Meta["cluster"] = cluster
157
cInfo.Meta["commit"] = proto.CommitID
158
if len(cInfo.Meta["metric_path"]) == 0 {
159
cInfo.Meta["metric_path"] = "/metrics"
160
log.LogInfo("metric_path is empty, use default /metrics")
164
cInfoBytes, err := json.Marshal(cInfo)
166
log.LogErrorf("marshal error, %v", err.Error())
169
req, err = http.NewRequest(http.MethodPut, url, bytes.NewBuffer(cInfoBytes))
171
log.LogErrorf("new request error, %v", err.Error())
174
req.Header.Set("Content-Type", "application/json; charset=utf-8")
181
func parseMetaStr(meta string) (bool, map[string]string) {
183
log.LogInfo("meta is empty, use default")
184
meta = "dataset=custom;category=custom;app=cfs;role=fuseclient;metric_path=/metrics"
187
m := map[string]string{}
189
kvs := strings.Split(meta, ";")
190
for _, kv := range kvs {
191
arr := strings.Split(kv, "=")
193
log.LogInfof("meta is invalid, can't use %s", meta)