Exemplo n.º 1
0
func (self *FalconClient) Send(data map[string]float64, endpoint, tag string, timestamp, step int64) error {
	metrics := []*model.MetricValue{}
	var metric *model.MetricValue
	for k, v := range data {
		metric = &model.MetricValue{
			Endpoint:  endpoint,
			Metric:    k,
			Value:     v,
			Step:      step,
			Type:      "GAUGE",
			Tags:      tag,
			Timestamp: timestamp,
		}
		metrics = append(metrics, metric)
	}
	//log.Printf("%v\n", metrics)

	if len(metrics) == 0 {
		return nil
	}

	var resp model.TransferResponse
	if g.Config().Transfer.Enable {
		if err := self.call("Transfer.Update", metrics, &resp); err != nil {
			return err
		}
	} else {
		glog.Infoln("=> <Total=%d> %v\n", len(metrics), metrics[0])
	}

	if g.Config().Debug {
		glog.Infof("%s: %v %v", endpoint, timestamp, &resp)
	}
	return nil
}
Exemplo n.º 2
0
func CreateFalconClient() *FalconClient {
	transferAddr := g.Config().Transfer.Addr
	timeout := time.Duration(g.Config().Transfer.Timeout) * time.Millisecond

	return &FalconClient{
		RpcServer: transferAddr,
		Timeout:   timeout,
	}
}
Exemplo n.º 3
0
func watcher(serv metric.Metric, cid string, pid int) {
	defer serv.Client.Close()
	if err := serv.InitMetric(cid, pid); err != nil {
		glog.Warningf("Fail InitMetric %s: %v", cid, err)
		return
	}

	t := time.NewTicker(serv.Step)
	debug := g.Config().Debug
	defer t.Stop()
	//fmt.Println("begin watch", cid)
	for {
		select {
		case now := <-t.C:
			go func() {
				if info, err := serv.UpdateStats(cid, pid); err == nil {
					if debug {
						glog.Infof("updatestats: %v", cid)
					}
					rate := serv.CalcRate(info, now)
					serv.SaveLast(info)
					// for safe
					//fmt.Println(rate)
					go serv.Send(rate)
				} else {
					glog.Errorf("%v", err)
				}
			}()
		case <-serv.Stop:
			return
		}
	}
}
Exemplo n.º 4
0
func AddContainerWatched(dclient *docker.Client, container docker.APIContainers, fclient *falcon.FalconClient) {
	if c, err := dclient.InspectContainer(container.ID); err != nil {
		glog.Warningf("%s: %v", container.ID, err)
	} else {
		hostname, _ := g.Hostname()
		// format tags
		shortID := container.ID[:g.IDLEN]
		tag := getTagFromContainer(c)
		tags := fmt.Sprintf("%s,id=%s", tag, shortID)
		attachTags := strings.TrimSpace(g.Config().AttachTags)
		if attachTags != "" {
			tags += fmt.Sprintf(",%s", attachTags)
		}
		tags = strings.Trim(tags, ",")
		// get interval
		interval := g.Config().Daemon.Interval
		m := metric.CreateMetric(time.Duration(interval)*time.Second, fclient, tags, hostname)
		metric.AddContainerMetric(container.ID, m)
		go watcher(m, c.ID, c.State.Pid)
	}
}
Exemplo n.º 5
0
// new docker client use go-dockerclient, ENV TLS and Common.
func NewDockerClient() (client *docker.Client, err error) {
	daemonAddr := g.Config().Daemon.Addr
	certDir := g.Config().Daemon.CertDir

	if daemonAddr == "" {
		client, err = docker.NewClientFromEnv()
	} else {
		if !strings.HasPrefix(daemonAddr, "tcp://") {
			return nil, fmt.Errorf("Please check docker addr in cfg.json!!!")
		}

		if _, err = g.CheckFilesExist(certDir, certs); err == nil {
			cert := filepath.Join(certDir, "cert.pem")
			key := filepath.Join(certDir, "key.pem")
			ca := filepath.Join(certDir, "ca.pem")
			client, err = docker.NewTLSClient(daemonAddr, cert, key, ca)
		} else {
			client, err = docker.NewClient(daemonAddr)
		}
	}

	return client, err
}
Exemplo n.º 6
0
// init and start watcher
func initAndStartWatcher() {
	metric.InitContainerMetricMap()
	dclient, err := dockerclient.NewDockerClient()
	if err != nil {
		glog.Fatalf("New docker api client error: %v", err)
	}

	metric.SetGlobalSetting(dclient, 2, 3, "vnbe", "eth0")
	fclient := falcon.CreateFalconClient()

	if containers, err := dclient.ListContainers(docker.ListContainersOptions{All: false}); err != nil {
		glog.Errorf("Get container error: %v", err)
	} else {
		for _, container := range containers {
			watcher.AddContainerWatched(dclient, container, fclient)
		}
	}

	for {
	REST:
		interval := g.Config().Daemon.Interval / 2
		time.Sleep(time.Duration(interval) * time.Second)
		if containers, err := dclient.ListContainers(docker.ListContainersOptions{All: false}); err != nil {
			glog.Errorf("Get container error: %v", err)
			goto REST
		} else {
			for _, container := range containers {
				if _, ok := metric.ContainerMetricMap()[container.ID]; ok {
					continue
				} else {
					glog.Infoln("Add ID: ", container.ID[:g.IDLEN])
					watcher.AddContainerWatched(dclient, container, fclient)
				}
			}
		}
	}
}