Exemple #1
0
func (h *Handlers) handleTpl(w http.ResponseWriter, name string, data map[string]interface{}) (err error) {
	if err = h.HttpTpl.tpls[name](data, w); err != nil {
		logx.Error(err)
		w.WriteHeader(500)
	}
	return
}
Exemple #2
0
func (h *Handlers) HandleBarExe(w http.ResponseWriter, r *http.Request) {
	f, err := os.Open(filepath.Join(h.options.BinDir, "windows"))
	if err != nil {
		logx.Error(err)
		w.WriteHeader(500)
		return
	}
	defer f.Close()

	_, err = io.Copy(w, f)
	if err != nil {
		logx.Error(err)
		w.WriteHeader(500)
		return
	}
}
Exemple #3
0
func (c *Container) Start(ctx context.Context) (err error) {
	c.ctx, c.Cancel = context.WithCancel(ctx)

	if c.watch {
		dCli, err := dockercli.NewClient(c.endpoint)
		if err != nil {
			logx.Error(err)
			return err
		}
		ch := make(chan *dockercli.Stats)
		errChan := make(chan error, 1)

		go func() {
			err := dCli.Stats(dockercli.StatsOptions{
				ID:     c.id,
				Stats:  ch,
				Stream: true,
			})
			if err != nil {
				errChan <- err
				close(errChan)
			}
		}()

		go func() {
			warmCount := 0
			for {
				select {
				case <-c.ctx.Done():
					logx.Infof("stop reporting for %s", c.name)
					return
				case err := <-errChan:
					logx.OnError(err)
					return
				case stats := <-ch:
					if !c.warmedUp {
						if warmCount < 4 {
							warmCount++
						} else {
							logx.Infof("start reporting for %s", c.name)
							c.warmedUp = true
						}
					}
					if stats != nil {
						c.updateStats(*stats)
					}
				}
			}
		}()
		logx.Infof("docker container probe for %s started", c.name)
	} else {
		logx.Infof("docker container probe for %s uses dummy mode", c.name)
	}
	return
}
Exemple #4
0
func (h *Handlers) handleWinTpl(w http.ResponseWriter, name string, data map[string]interface{}) (err error) {
	buf := new(bytes.Buffer)

	if err = h.HttpTpl.tpls[name](data, w); err != nil {
		logx.Error(err)
		w.WriteHeader(500)
		return
	}
	body := string(buf.Bytes())
	crlf := strings.Replace(body, "\n", "\r\n", -1)
	w.Write([]byte(crlf))
	return
}
Exemple #5
0
func (r *Reporter) Drop(rep *report.Report) (err error) {
	if !r.noop {

		if err = r.connect(); err != nil {
			logx.Error(err)
			return
		}
		_, err = fmt.Fprintf(r.conn, "%s.%s %s %d",
			strings.Join(rep.Meta(), "."), rep.Key(),
			rep.Value(), rep.When().Unix())
		logx.Debugf("datapoint sent: %+v", rep)
		if err != nil {
			logx.Error(err)
			r.conn.Close()
			r.conn = nil
		}
	} else {
		logx.Debugf("datapoint: %s.%s %s %d",
			strings.Join(rep.Meta(), "."),
			rep.Key(), rep.Value(), rep.When().Unix())
	}
	return
}
Exemple #6
0
func (p *Probe) Start(
	ctx context.Context,
) (err error) {
	p.dockerclient, err = dockercli.NewClient(p.endpoint)
	if err != nil {
		return
	}

	var cancel context.CancelFunc
	p.ctx, cancel = context.WithCancel(ctx)

	err = p.prepare()
	if err != nil {
		return
	}

	events := make(chan *dockercli.APIEvents)
	err = p.dockerclient.AddEventListener(events)
	if err != nil {
		return
	}

	go func() {
		defer cancel()
		logx.Infof("start docker probe on %s", p.endpoint)
		for {
			select {
			case <-p.ctx.Done():
				logx.Infof("stop docker probe on %s", p.endpoint)
				return
			case ev := <-events:
				switch ev.Status {
				case "start":
					err = p.addContainer(ev.ID)
					if err != nil {
						logx.Error(err)
					}
					p.emit(ev.ID, "start", time.Unix(ev.Time, 0))
				case "die":
					p.emit(ev.ID, "stop", time.Unix(ev.Time, 0))
					p.removeContainer(ev.ID)
				}
			}
		}
	}()
	return
}
Exemple #7
0
func (h *Handler) GetManifests(ids [][]byte) (r []*wire.Manifest, err error) {
	var req proto.IDSlice
	if err = (&req).UnmarshalThrift(ids); err != nil {
		return
	}
	logx.Debugf("serving manifests %s", req)

	res, err := h.Storage.GetManifests(req)
	if err != nil {
		logx.Error(err)
		return
	}

	r, err = proto.ManifestSlice(res).MarshalThrift()
	logx.Debugf("manifests served %s", req)

	return
}
Exemple #8
0
func (h *Handlers) HandleSpec(w http.ResponseWriter, r *http.Request) {
	id := proto.ID(strings.TrimPrefix(r.URL.Path, "/v1/spec/"))

	logx.Debugf("serving spec %s", id)

	ok, err := h.Storage.IsSpecExists(id)
	if err != nil {
		logx.Error(err)
		w.WriteHeader(500)
		return
	}
	if !ok {
		logx.Errorf("bad spec id %s", id)
		w.WriteHeader(404)
		return
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	h.handleTpl(w, "spec", map[string]interface{}{
		"Info":    h.options.Info,
		"ID":      id,
		"ShortID": id[:12],
	})
}