Example #1
0
func GetRecordList(domainId int) []g.RecordResult {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")
	data.Add("domain_id", strconv.Itoa(domainId))

	strResponse, err := Post("https://dnsapi.cn/Record.List", data)
	if nil == err && "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
		var rlr g.RecordListResult
		err = json.Unmarshal(bytes.NewBufferString(strResponse).Bytes(), &rlr)
		if nil != err {
			logger.Error("decode RECORD_LIST response fail %v\n", err)
		} else {
			var records []g.RecordResult
			for _, record := range rlr.Records {
				if "A" != record.Type {
					continue
				}
				records = append(records, record)
				logger.Info("domain_id:%d,name:%s,record_id:%s,name:%s,value:%s,status:%s\n",
					domainId, rlr.Domain.Name, record.Id, record.Name, record.Value, record.Status)
			}

			return records
		}
	} else {
		logger.Error("GET_RECORD_LIST RESPONSE<<<====%s, error: %v", strResponse, err)
	}
	return nil
}
Example #2
0
// metrics: [["endpoint", "counter"], []]
func QueryMulti(start, end int64, cf string, metrics [][]string) []*model.GraphQueryResponse {
	result := make([]*model.GraphQueryResponse, 0, len(metrics))

	var wg sync.WaitGroup
	lock := new(sync.Mutex)

	for _, pair := range metrics {
		if len(pair) != 2 {
			continue
		}
		endpoint := pair[0]
		counter := pair[1]

		wg.Add(1)
		go func(endpoint, counter string) {
			defer wg.Done()
			r, err := QueryOne(start, end, cf, endpoint, counter)
			if err != nil {
				logger.Error("query one from graph fail: %v", err)
				return
			}

			lock.Lock()
			defer lock.Unlock()
			result = append(result, r)

		}(endpoint, counter)
	}

	wg.Wait()

	return result
}
Example #3
0
func Control(runUser, workdir, arg string) (string, error) {
	cmd := BuildCommand(runUser, path.Join(workdir, "control"), arg)
	cmd.Dir = file.SelfDir()
	out, err := ExecuteCommandWithOutput(cmd)
	if err != nil {
		logger.Error("cd %s; ./control %s fail %v. output: %s\n", workdir, arg, err, out)
	}
	return out, err
}
Example #4
0
func ReloadBackends() {
	cfg := g.Config().Graph
	for {
		time.Sleep(time.Duration(cfg.ReloadInterval) * time.Second)
		err := InitBackends()
		if err != nil {
			logger.Error("reload backends fail: %v", err)
		}
	}
}
Example #5
0
func (p *ConnPool) Get() (conn io.Closer, err error) {
	if slowLogEnabled {
		start_t := time.Now()
		defer func() {
			end_t := time.Now()
			diff := float64(end_t.UnixNano()-start_t.UnixNano()) / 1000000
			if diff >= float64(slowLogLimit) {
				logger.Debug("%s get conn from pool cost too much, duration: %f ms, pool: %+v", LOG_TAG, diff, p)
			}
		}()
	}
	p.Lock()
	if p.conns >= p.MaxConns && len(p.free) == 0 {
		p.Unlock()
		return nil, ErrMaxConn
	}

	new_conn := false
	if len(p.free) > 0 {
		// return the first free connection in the pool
		conn = p.free[0]
		p.free = p.free[1:]
	} else {
		conn, err = p.New()
		if err != nil {
			p.Unlock()
			return nil, err
		}
		new_conn = true
	}
	p.Unlock()

	err = p.Ping(conn)
	if err != nil {
		p.Lock()
		logger.Error("%s ping conn fail: %v, pool: %+v", LOG_TAG, err, p)
		if !new_conn && p.conns > 0 {
			p.conns -= 1
		}
		p.Unlock()
		conn.Close()
		return nil, err
	}
	if new_conn {
		p.Lock()
		p.conns += 1
		logger.Trace("%s open new conn: %v, pool: %+v", LOG_TAG, conn, p)
		p.Unlock()
	} else {
		logger.Trace("%s get existent conn: %v, pool: %+v", LOG_TAG, conn, p)
	}

	return conn, nil
}
Example #6
0
func getIp() string {
	strUrl := g.Config().GetIpApi
	logger.Debug("REQUEST_URL:%s\n", strUrl)
	httpRequest := httplib.Get(strUrl).SetTimeout(3*time.Second, 10*time.Second)
	httpResponse, err := httpRequest.Bytes()
	if nil != err {
		logger.Errorln("GET_IP error", err)
		return ""
	}

	strIp := ""
	var resp g.ServletResponse
	err = json.Unmarshal(httpResponse, &resp)
	if err != nil {
		logger.Error("decode GET_IP response fail %v\n", err)
	} else if false == resp.Success {
		logger.Error("GET_IP fail %s\n", resp.Message)
	} else {
		strIp = resp.Message
	}

	logger.Infoln("RESPONSE_IP:", strIp)
	return strIp
}
Example #7
0
func BuildHeartbeatRequest(hostname string, desiredAgents []*model.DesiredAgent) model.HeartbeatRequest {
	req := model.HeartbeatRequest{Hostname: hostname}
	req.Ip = utils.GetLocalIP()
	req.UpdaterVersion = g.VERSION
	req.RunUser = CurrentUser

	realAgents := []*model.RealAgent{}
	now := time.Now().Unix()

	for _, da := range desiredAgents {
		agentDir := path.Join(da.WorkDir, da.Name)
		// 如果目录下没有.version,我们认为这根本不是一个agent
		version := ReadVersion(da.RunUser, agentDir)
		if "" == version {
			logger.Error("read %s/.version fail\n", agentDir)
			continue
		}

		controlFile := path.Join(agentDir, version, "control")
		if !CheckFileExists(da.RunUser, controlFile) {
			logger.Errorln(controlFile, "is nonexistent, user:"******"control"), "status")
		cmd.Dir = f.SelfDir()
		status, err := ExecuteCommandWithOutput(cmd)
		if err != nil {
			status = err.Error()
		}

		realAgent := &model.RealAgent{
			Name:      da.Name,
			Version:   version,
			Status:    status,
			Timestamp: now,
		}
		realAgent.RunUser = da.RunUser
		realAgent.WorkDir = da.WorkDir

		realAgents = append(realAgents, realAgent)
	}

	req.RealAgents = realAgents
	return req
}
Example #8
0
func main() {
	cfg := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")
	versionGit := flag.Bool("vg", false, "show version")
	flag.Parse()

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

	// config
	g.ParseConfig(*cfg)

	var err error
	// graph section
	err = graph.InitBackends()
	if err != nil {
		logger.Error("load graph backends fail: %v", err)
	}

	go graph.ReloadBackends()

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sigs
		fmt.Println("END")
		graph.DestroyConnPools()
		os.Exit(0)
	}()

	http.Start()
}
Example #9
0
func GetDomainList() []g.DomainResult {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")

	strResponse, err := Post("https://dnsapi.cn/Domain.List", data)
	if nil == err && "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
		var dlr g.DomainListResult
		err = json.Unmarshal(bytes.NewBufferString(strResponse).Bytes(), &dlr)
		if nil != err {
			logger.Errorln("decode DOMAIN_LIST response fail %v\n", err)
		} else {
			for _, domain := range dlr.Domains {
				logger.Info("domain_id:%d,name:%s,created:%v,updated:%v\n", domain.Id, domain.Name, domain.Created, domain.Updated)
			}
			return dlr.Domains
		}
	} else {
		logger.Error("GET_DOMAIN_LIST RESPONSE<<<====%s, error: %v", strResponse, err)
	}
	return nil
}
Example #10
0
func configGraphRoutes() {

	// method:post
	http.HandleFunc("/graph/history", func(w http.ResponseWriter, r *http.Request) {
		var body GraphHistoryParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body.EndpointCounters) == 0 {
			StdRender(w, "", errors.New("empty_payload"))
			return
		}

		data := []*model.GraphQueryResponse{}
		for _, ec := range body.EndpointCounters {
			result, err := graph.QueryOne(int64(body.Start), int64(body.End), body.CF, ec.Endpoint, ec.Counter)
			if err != nil {
				logger.Error("query one fail: %v", err)
			}
			data = append(data, result)
		}

		StdRender(w, data, nil)
	})

	// method:get
	http.HandleFunc("/graph/history/one", func(w http.ResponseWriter, r *http.Request) {
		start := r.FormValue("start")
		end := r.FormValue("end")
		cf := r.FormValue("cf")
		endpoint := r.FormValue("endpoint")
		counter := r.FormValue("counter")

		if endpoint == "" || counter == "" {
			StdRender(w, "", errors.New("empty_endpoint_counter"))
			return
		}

		if cf != "AVERAGE" && cf != "MAX" && cf != "MIN" {
			StdRender(w, "", errors.New("invalid_cf"))
			return
		}

		now := time.Now()
		start_i64, err := strconv.ParseInt(start, 10, 64)
		if err != nil {
			start_i64 = now.Unix() - 3600
		}
		end_i64, err := strconv.ParseInt(end, 10, 64)
		if err != nil {
			end_i64 = now.Unix()
		}

		result, err := graph.QueryOne(start_i64, end_i64, cf, endpoint, counter)
		logger.Trace("query one result: %v, err: %v", result, err)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		StdRender(w, result, nil)
	})

	// get, info
	http.HandleFunc("/graph/info/one", func(w http.ResponseWriter, r *http.Request) {
		endpoint := r.FormValue("endpoint")
		counter := r.FormValue("counter")

		if endpoint == "" || counter == "" {
			StdRender(w, "", errors.New("empty_endpoint_counter"))
			return
		}

		result, err := graph.Info(endpoint, counter)
		logger.Trace("graph.info result: %v, err: %v", result, err)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		StdRender(w, result, nil)
	})

	// post, info
	http.HandleFunc("/graph/info", func(w http.ResponseWriter, r *http.Request) {
		var body []*model.GraphInfoParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body) == 0 {
			StdRender(w, "", errors.New("empty_payload"))
			return
		}

		data := []*model.GraphFullyInfo{}
		for _, param := range body {
			info, err := graph.Info(param.Endpoint, param.Counter)
			if err != nil {
				logger.Trace("graph.info fail, resp: %v, err: %v", info, err)
			} else {
				logger.Trace("graph.info result: %v, err: %v", info, err)
			}
			data = append(data, info)
		}

		StdRender(w, data, nil)
	})

	// post, last
	http.HandleFunc("/graph/last", func(w http.ResponseWriter, r *http.Request) {
		var body []*model.GraphLastParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body) == 0 {
			StdRender(w, "", errors.New("empty_payload"))
			return
		}

		data := []*model.GraphLastResp{}
		for _, param := range body {
			last, err := graph.Last(param.Endpoint, param.Counter)
			if err != nil {
				logger.Trace("graph.last fail, resp: %v, err: %v", last, err)
				continue
			}
			data = append(data, last)
		}
		StdRender(w, data, nil)
	})
}