コード例 #1
0
ファイル: skipper.go プロジェクト: pharaujo/consul-alerts
// Campaign handles leader election. Basically this just acquires a lock on the LeadershipKey and whoever gets the lock
// is the leader. A re-election occurs when there are changes in the LeadershipKey.
func (c *Candidate) campaign() {
	c.retrieveNode()
	c.retrieveSession()
	consul := c.consulClient()

	logrus.Debugf("%s is running for election with session %s.", c.node, c.session)

	kvpair := &consulapi.KVPair{
		Key:     c.LeadershipKey,
		Value:   []byte(c.node),
		Session: c.session,
	}
	acquired, _, err := consul.KV().Acquire(kvpair, nil)
	if err != nil {
		logrus.Errorln("Failed to run Consul KV Acquire:", err)
	}

	if acquired {
		logrus.Infof("%s has become the leader.", c.node)
	}

	kv, _, _ := consul.KV().Get(c.LeadershipKey, nil)

	if kv != nil && kv.Session != "" {
		logrus.Debugf("%s is the current leader.", string(kv.Value))
		logrus.Debugf("%s is waiting for changes in '%s'.", c.node, c.LeadershipKey)
		latestIndex := kv.ModifyIndex
		options := &consulapi.QueryOptions{
			WaitIndex: latestIndex,
		}
		consul.KV().Get(c.LeadershipKey, options)
	}
	time.Sleep(15 * time.Second)
	c.campaign()
}
コード例 #2
0
func (beary *BearyNotifier) postToBeary() bool {

	data, err := json.Marshal(beary)
	if err != nil {
		log.Println("Unable to marshal beary payload:", err)
		return false
	}
	log.Debugf("struct = %+v, json = %s", beary, string(data))

	b := bytes.NewBuffer(data)
	if res, err := http.Post(beary.Url, "application/json", b); err != nil {
		log.Println("Unable to send data to beary:", err)
		return false
	} else {
		defer res.Body.Close()
		statusCode := res.StatusCode
		if statusCode != 200 {
			body, _ := ioutil.ReadAll(res.Body)
			log.Println("Unable to notify beary:", string(body))
			return false
		} else {
			log.Println("Beary notification sent.")
			return true
		}
	}

}
コード例 #3
0
func (influxdb *InfluxdbNotifier) toBatchPoints(messages Messages, bp client.BatchPoints) {

	seriesName := influxdb.SeriesName

	for index, message := range messages {
		tags := map[string]string{
			"node":      message.Node,
			"service":   message.Service,
			"status":    message.Status,
			"serviceId": message.ServiceId,
		}
		fields := map[string]interface{}{
			"checks": message.Check,
			"notes":  message.Notes,
			"output": message.Output,
		}

		p, err := client.NewPoint(seriesName, tags, fields, message.Timestamp)
		if err != nil {
			log.Println("Error: ", err.Error())
		}
		log.Debugf("%s", index)
		bp.AddPoint(p)
	}
}
コード例 #4
0
ファイル: skipper.go プロジェクト: pharaujo/consul-alerts
// Resign forces the current agent to step down as the leader forcing a re-election. Nothing happens if the agent is not
// the current leader.
func (c *Candidate) Resign() {
	if c.IsLeader() {
		consul := c.consulClient()
		kvpair := &consulapi.KVPair{
			Key:     c.LeadershipKey,
			Value:   []byte(c.node),
			Session: c.session,
		}
		success, _, err := consul.KV().Release(kvpair, nil)
		if !success || err != nil {
			logrus.Warnf("%s was unable to step down as a leader", c.node)
		} else {
			logrus.Debugf("%s is no longer the leader.", c.node)
		}
	}
}