func requestHeartbeat(heartbeatURL string, hbtRequest *model.HeartbeatRequest) (hbtResponse *model.HeartbeatResponse, err error) { b, err := json.Marshal(hbtRequest) if err != nil { log.Errorln(err) return } resp, err := http.Post(heartbeatURL, "application/json;charset=utf-8", bytes.NewBuffer(b)) if err != nil { log.Errorln(err) return } if resp.StatusCode != http.StatusOK { log.Warnf("StatusCode: %d", resp.StatusCode) return } defer resp.Body.Close() result, err := ioutil.ReadAll(resp.Body) if err != nil { log.Errorln(err) return } hbtResponse = &model.HeartbeatResponse{} err = json.Unmarshal(result, hbtResponse) if err != nil { log.Errorln(err) return } return }
// Remove removes the given dependency from the list and stops the // associated View. If a View for the given dependency does not exist, this // function will return false. If the View does exist, this function will return // true upon successful deletion. func (w *Watch) Remove(d Dependency) bool { w.Lock() defer w.Unlock() log.Debugf("watcher removing %s", d.Name()) if view, ok := w.depViewMap[d.Name()]; ok { log.Debugf("watcher actually removing %s", d.Name()) view.stop() delete(w.depViewMap, d.Name()) return true } log.Warnf("watcher %s did not exist, skipping", d.Name()) return false }
func rewriteConfigForCollectd(comp *model.Component, containers []*Container) error { log.Infoln("rewrite configuration file ", comp.Name) // 1. Load configuration file. c := &cfg_collected.Config{} _, err := toml.DecodeFile(comp.ConfigFilepath, c) if err != nil { return err } // 2. Change the configuration content. inputConfig := &cfg_collected.InputConfig{ Enabled: true, Interval: DefaultInterval, SubInput: make([]*cfg_collected.InputConfig, len(containers)), } for i, ctn := range containers { serviceType := strings.Split(path.Base(ctn.Image), ":")[0] var interval int itv, ok := config.AppConfig().Intervals[serviceType] if !ok { log.Warnf("service %s hasn't config interval, using default %d", serviceType, DefaultInterval) interval = DefaultInterval } else { interval = itv.Value } subConfig := &cfg_collected.InputConfig{ Enabled: true, Interval: interval, Tags: fmt.Sprintf("cid:%s", ctn.ID), } inputConfig.SubInput[i] = subConfig } c.Input["container"] = inputConfig // 3. Rewrite the configuration file. buf := bytes.NewBuffer(nil) encoder := toml.NewEncoder(buf) err = encoder.Encode(c) if err != nil { return err } encoder.Indent = " " log.Debugln("configuration file path is ", comp.ConfigFilepath) err = utils.WriteFile(comp.ConfigFilepath, buf.Bytes(), 0644) return err }
func (w *Watch) Add(d Dependency) (bool, error) { w.Lock() defer w.Unlock() log.Debugf("%s is adding.", d.Name()) if _, ok := w.depViewMap[d.Name()]; ok { log.Warnf("watcher %s already exists, skipping", d.Name()) return false, nil } v, err := NewView(d) if err != nil { return false, err } v.Clients = w.clientSet log.Debugf("watcher %s starting", d.Name()) w.depViewMap[d.Name()] = v go v.poll(w.DataCh, w.ErrCh) return true, nil }