Пример #1
0
func (measurement *Measurement) Create(context *ctp.ApiContext) *ctp.HttpError {
	measurement.BuildLinks(context)
	measurement.Metric = ctp.ShortenLink(context.CtpBase, measurement.Metric)
	if !ctp.IsShortLink(measurement.Metric) {
		return ctp.NewBadRequestError("Invalid metric URL")
	}

	if !measurement.State.IsValid() {
		return ctp.NewBadRequestError("Invalid or missing state value")
	}

	if measurement.Result != nil {
		if err := measurementCheckResult(context, measurement); err != nil {
			return err
		}
	} else {
		if err := measurementCheckMetric(context, measurement); err != nil {
			return err
		}
	}

	if measurement.Objective != nil {
		if err := measurementObjectiveEvaluate(context, measurement); err != nil {
			return err
		}
	}

	if !ctp.CreateResource(context, "measurements", measurement) {
		return ctp.NewInternalServerError("Could not save measurement object")
	}
	return nil
}
Пример #2
0
func (trigger *Trigger) Create(context *ctp.ApiContext) *ctp.HttpError {
	trigger.BuildLinks(context)
	trigger.Measurement = ctp.ShortenLink(context.CtpBase, trigger.Measurement)
	if !ctp.IsShortLink(trigger.Measurement) {
		return ctp.NewBadRequestError("Invalid measurement URL")
	}

	ok, err := triggerCheckCondition(context, trigger, nil)
	if err != nil {
		return ctp.NewBadRequestErrorf("%s", err.Error())
	}

	if ok {
		triggerLogAndNotify(context, trigger, nil)
	}

	if !ctp.CreateResource(context, "triggers", trigger) {
		return ctp.NewHttpError(http.StatusInternalServerError, "Could not save object")
	}
	return nil
}
Пример #3
0
func measurementTriggersEvaluate(context *ctp.ApiContext, measurement *Measurement) {
	var trigger Trigger
	now := ctp.Now()

	mlink := ctp.ShortenLink(context.CtpBase, measurement.Self)
	query := context.Session.DB("ctp").C("triggers").Find(bson.M{"measurement": mlink})

	n, err := query.Count()
	if err == nil {
		ctp.Log(context, ctp.DEBUG, "Evaluating %d triggers related to measurement %s", n, measurement.Id)
	} else {
		ctp.Log(context, ctp.ERROR, "Failed to evaludate triggers related to measurement %s, %s", measurement.Id, err.Error())
		return
	}

	iter := query.Iter()
	for iter.Next(&trigger) {
		var err error
		var err_upd error
		var ok bool

		trigger.BuildLinks(context)

		ctp.Log(context, ctp.DEBUG, "Evaludating trigger %s, currently with status '%s'", trigger.Id, trigger.Status.String())

		switch trigger.Status {
		case ctp.Tfalse:
			ok, err = triggerCheckCondition(context, &trigger, measurement)
		case ctp.Ttrue:
			if uint(ctp.SecondsSince(trigger.StatusUpdateTime)) <= trigger.GuardTime {
				continue
			}
			ok, err = triggerCheckCondition(context, &trigger, measurement)
		case ctp.Terror:
			continue
		}

		switch {
		case err != nil:
			ctp.Log(context, ctp.ERROR, "Error in trigger %s for measurement %s", trigger.Id, measurement.Id)
			err_log := CreateErrorLogEntry(context, &trigger, err.Error())
			if err_log != nil {
				ctp.Log(context, ctp.ERROR, "Failed to create error log: %s", err_log.Error())
			}
			err_upd = context.Session.DB("ctp").C("triggers").Update(bson.M{"_id": trigger.Id}, bson.M{"$set": bson.M{"status": ctp.Terror, "statusUpdateTime": now.String()}})
		case ok:
			ctp.Log(context, ctp.DEBUG, "trigger %s is TRUE", trigger.Id)
			err_log := CreateNormalLogEntry(context, &trigger, measurement.Result, trigger.Tags)
			if err_log != nil {
				ctp.Log(context, ctp.ERROR, "Failed to create normal log: %s", err_log.Error())
			}
			err_upd = context.Session.DB("ctp").C("triggers").Update(bson.M{"_id": trigger.Id}, bson.M{"$set": bson.M{"status": ctp.Ttrue, "statusUpdateTime": now.String()}})
		default:
			ctp.Log(context, ctp.DEBUG, "Trigger %s is FALSE", trigger.Id)
			err_upd = context.Session.DB("ctp").C("triggers").Update(bson.M{"_id": trigger.Id}, bson.M{"$set": bson.M{"status": ctp.Tfalse, "statusUpdateTime": now.String()}})
		}

		if err_upd != nil {
			ctp.Log(context, ctp.ERROR, "Failed to update trigger 'status' and 'statusDateTime': %s", err_upd.Error())
		}
	}
}