Пример #1
0
func (event *Event) MustPublish(pub *zmqpubsub.Publisher) {
	data, err := json.Marshal(event)
	if err != nil {
		log.Fatal(err)
	}
	pub.MustPublish("event."+event.Type, string(data))
}
Пример #2
0
func LoadConfig() {
	var err error
	c, err = NewConfig("applog_endpoint", Config{})
	if err != nil {
		log.Fatal("Unable to load applog_endpoint config; %v", err)
	}
}
Пример #3
0
func Stream() chan *Event {
	ch := make(chan *Event)
	res := getDockerEvents(3)

	go func() {
		defer res.Body.Close()

		// Read the streaming json from the events endpoint
		// http://docs.docker.io/en/latest/api/docker_remote_api_v1.3/#monitor-docker-s-events
		d := json.NewDecoder(res.Body)
		for {
			var event Event
			if err := d.Decode(&event); err != nil {
				if err == io.EOF {
					break
				}
				log.Fatal(err)
			}
			ch <- &event
		}

		close(ch)
	}()

	return ch
}
Пример #4
0
func main() {
	config.LoadConfig()

	drain.RemoveOrphanedDrains()
	applog_endpoint.RouterMain()

	err := applog_endpoint.Serve()
	log.Fatal(err)
}
Пример #5
0
func (m *MultiRegexpMatcher) MustAdd(name string, substring string, re string) {
	if oldName, ok := m.substrings[substring]; ok {
		log.Fatalf(
			"substring %s already added under %s; being added again by %s",
			substring, oldName, name)
	}
	if _, ok := m.regexps[name]; ok {
		log.Fatal("already in regexps")
	}
	m.substrings[substring] = name
	m.regexps[name] = regexp.MustCompile(re)
}
Пример #6
0
func (t *tracker) Remove(key string) {
	if t.debug {
		log.Infof("Removing the following key %s from cached instances", key)
	}
	t.mux.Lock()
	delete(t.Cached.Instances, key)
	t.mux.Unlock()
	if err := t.Commit(); err != nil {
		log.Fatal(err)

	}
}
Пример #7
0
func GetLiveDockerContainers(retries int) map[string]bool {
	allDockerIds := make(map[string]bool)
	var httpResByte []byte
	c := http.Client{}
	for attempt := 0; attempt < retries; attempt++ {
		res, err := c.Get(containers_url)
		defer res.Body.Close()
		if err != nil {
			if (attempt + 1) == retries {
				log.Fatalf("Failed to read from docker daemon; giving up retrying: %v", err)
			}
			log.Warnf("Docker connection error (%v); retrying after 1 second.", err)
			time.Sleep(time.Second)
		} else {
			httpResByte, err = ioutil.ReadAll(res.Body)
			if err != nil {
				log.Fatal(err)

			}
			var jsonData []Docker
			err = json.Unmarshal([]byte(httpResByte), &jsonData)

			if err != nil {
				log.Fatal(err)

			}

			for _, element := range jsonData {

				shortenedKey := element.Id[:ID_LENGTH]

				allDockerIds[shortenedKey] = true
			}

		}
	}
	return allDockerIds
}
Пример #8
0
func (t *tracker) StartSubmissionTimer(persistInterval time.Duration) {
	ticker := time.NewTicker(persistInterval)
	go func() {
		for {
			select {
			case <-ticker.C:
				err := t.Commit()
				if err != nil {
					log.Fatal(err)

				}
			case <-t.timerStopChan:
				ticker.Stop()
				return
			}

		}
	}()
}
Пример #9
0
// GetState retrieves the cached state for the given process on all
// nodes.
func (s *StateCache) GetState(name string) (map[string]StateInfo, error) {
	allKey, _ := s.getKeys(name)
	states := map[string]StateInfo{}

	reply := s.Client.SMembers(allKey)
	if err := reply.Err(); err != nil {
		return nil, err
	}
	for _, nodeip := range reply.Val() {
		reply2 := s.Client.Get(s.getKeyFor(name, nodeip))
		if err := reply2.Err(); err != nil {
			return nil, err
		}
		stateInfoJson := reply2.Val()
		var stateInfo StateInfo
		if err := json.Unmarshal([]byte(stateInfoJson), &stateInfo); err != nil {
			log.Fatal(err)
		}
		states[nodeip] = stateInfo
	}
	return states, nil
}
Пример #10
0
// SetState caches the given state of a process in redis.
func (s *StateCache) SetState(
	name string, state state.State, rev int64) {
	info := StateInfo(state.Info())
	info["rev"] = fmt.Sprintf("%d", rev)
	data, err := json.Marshal(info)
	if err != nil {
		log.Fatal(err)
	}

	allKey, thisKey := s.getKeys(name)

	log.Infof("[statecache] Caching state of %s", name)
	reply := s.Client.SAdd(allKey, s.Host)
	if err := reply.Err(); err != nil {
		log.Errorf("Unable to cache state of %s in redis; %v",
			name, err)
		return
	}
	reply2 := s.Client.Set(thisKey, string(data))
	if err := reply2.Err(); err != nil {
		log.Errorf("Unable to cache state of %s in redis; %v",
			name, err)
	}
}