Esempio n. 1
0
//Get a specific project
func getProject(request router.Request) (int, []byte) {

	projectDTO, err := GetProjectDTOFromGAE(request.GetPathParams()["project_id"], request.GetContext())

	if err != nil && strings.Contains(err.Error(), "no such entity") {
		log.Errorf(request.GetContext(), "Error retriving Project: %v", err)
		return http.StatusNotFound, []byte("Project Not Found")
	} else if err != nil {
		log.Errorf(request.GetContext(), "Error retriving project: %v", err)
		return http.StatusInternalServerError, []byte(err.Error())
	} else {
		project, err := projectDTO.GetProject()
		if err != nil {
			log.Infof(request.GetContext(), "Error %v", err)
			return http.StatusInternalServerError, []byte(err.Error())

		}

		projectJSON, err := json.MarshalIndent(project, "", "	")
		if err != nil {
			log.Infof(request.GetContext(), "Error %v", err)
			return http.StatusInternalServerError, []byte(err.Error())

		}

		return http.StatusOK, projectJSON

	}

}
Esempio n. 2
0
func TriggerSubscriptionsIfNeeded(check alert.Check, subscriptions []Subscription, context appengine.Context) {
	for _, subscription := range subscriptions {

		if check.Changed && check.CurrentState > check.PreviousState {
			log.Infof(context, "Firing Subscrption %v", subscription)

			subject := fmt.Sprintf("Alert %s changed state from %s to %s", check.Alert,
				alert.GetStateString(check.PreviousState), alert.GetStateString(check.CurrentState))

			message := fmt.Sprintf("Alert %s changed state from %s to %s with value %f\n Value measured at %s.\n", check.Alert,
				alert.GetStateString(check.PreviousState), alert.GetStateString(check.CurrentState),
				check.Value, time.Now().UTC().String())

			msg := &mail.Message{
				Sender:  "Klaxon <*****@*****.**>",
				To:      []string{subscription.Target},
				Subject: subject,
				Body:    message,
			}
			if err := mail.Send(context, msg); err != nil {
				log.Errorf(context, "Couldn't send email: %v", err)
			}
		}

	}
}
Esempio n. 3
0
//Create/Update an alert for the given project
func postAlert(request router.Request) (int, []byte) {

	var alert Alert
	err := json.Unmarshal(request.GetContent(), &alert)
	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusBadRequest, []byte(err.Error())
	}

	//TODO Check Project Exists
	alert.Project = request.GetPathParams()["project_id"]
	err = SaveAlertToGAE(alert, request.GetContext())
	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusInternalServerError, []byte(err.Error())
	}

	return http.StatusOK, nil

}
Esempio n. 4
0
//Create/Update a project
//TODO: Check Admin/User Permissions
//TODO: Check required config present
//TODO: If create add to user project list
func updateProject(request router.Request) (int, []byte) {

	var project ProjectStruct
	err := json.Unmarshal(request.GetContent(), &project)
	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusBadRequest, []byte(err.Error())
	}

	projectDTO, err := project.GetDTO()
	if err != nil {
		return http.StatusInternalServerError, []byte(err.Error())

	}
	err = SaveProjectDTOToGAE(projectDTO, request.GetContext())

	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusInternalServerError, []byte(err.Error())
	}

	return http.StatusOK, nil
}
Esempio n. 5
0
func processAlert(projectAlert alert.Alert, context appengine.Context,
	graphiteReader graphite.GraphiteReader, subscriptions []subscription.Subscription,
	alertChecks []alert.Check) alert.Check {

	log.Infof(context, "Sending Graphite request too %s", projectAlert.Target)
	value, err := graphiteReader.ReadValue(projectAlert.Target)
	if err != nil {
		log.Errorf(context, "Error processing Alert: %v", err)
		projectAlert.SaveChangeIfNeeded(projectAlert.PreviousState != alert.UNKNOWN, alert.UNKNOWN, context)
		return alert.Check{projectAlert.Project, projectAlert.Name, projectAlert.PreviousState,
			alert.UNKNOWN, projectAlert.PreviousState != alert.UNKNOWN, 0}

	} else {
		changed, previous, current := projectAlert.CheckAlertStatusChange(value)
		projectAlert.SaveChangeIfNeeded(changed, current, context)
		check := alert.Check{projectAlert.Project, projectAlert.Name, previous, current, changed, value}
		subscription.TriggerSubscriptionsIfNeeded(check, subscriptions, context)
		return check
	}
}