Example #1
0
func V1GetMonitors(c *middleware.Context, query m.GetMonitorsQuery) {
	endpoint, err := sqlstore.GetEndpointById(c.OrgId, query.EndpointId)
	if err != nil {
		handleError(c, err)
		return
	}
	if endpoint == nil {
		c.JSON(200, []m.MonitorDTO{})
	}

	monitors := make([]m.MonitorDTO, len(endpoint.Checks))
	for i, check := range endpoint.Checks {
		monitors[i] = m.MonitorDTOFromCheck(check, endpoint.Slug)
		if check.Enabled {
			probeList, err := sqlstore.GetProbesForCheck(&check)
			if err != nil {
				handleError(c, err)
				return
			}
			monitors[i].Collectors = probeList
		}
	}
	c.JSON(200, monitors)
}
Example #2
0
func V1AddMonitor(c *middleware.Context, cmd m.AddMonitorCommand) {
	cmd.OrgId = c.OrgId
	if cmd.EndpointId == 0 {
		c.JSON(400, "EndpointId not set.")
		return
	}
	if cmd.MonitorTypeId == 0 {
		c.JSON(400, "MonitorTypeId not set.")
		return
	}
	if cmd.MonitorTypeId > 4 {
		c.JSON(400, "Invlaid MonitorTypeId.")
		return
	}
	if cmd.Frequency == 0 {
		c.JSON(400, "Frequency not set.")
		return
	}

	// get the endpoint that the check belongs too.
	endpoint, err := sqlstore.GetEndpointById(c.OrgId, cmd.EndpointId)
	if err != nil {
		handleError(c, err)
		return
	}
	if endpoint == nil {
		c.JSON(400, "endpoint does not exist.")
		return
	}

	for _, check := range endpoint.Checks {
		if checkTypeToId(check.Type) == cmd.MonitorTypeId {
			c.JSON(400, fmt.Sprintf("Endpoint already has a %s check.", check.Type))
			return
		}
	}

	route := &m.CheckRoute{}
	if len(cmd.CollectorTags) > 0 {
		route.Type = m.RouteByTags
		route.Config = map[string]interface{}{
			"tags": cmd.CollectorTags,
		}
	} else {
		route.Type = m.RouteByIds
		route.Config = map[string]interface{}{
			"ids": cmd.CollectorIds,
		}
	}

	check := m.Check{
		OrgId:          cmd.OrgId,
		EndpointId:     cmd.EndpointId,
		Type:           m.MonitorTypeToCheckTypeMap[cmd.MonitorTypeId-1],
		Frequency:      cmd.Frequency,
		Enabled:        cmd.Enabled,
		HealthSettings: cmd.HealthSettings,
		Created:        time.Now(),
		Updated:        time.Now(),
		Route:          route,
		Settings:       m.MonitorSettingsDTO(cmd.Settings).ToV2Setting(m.MonitorTypeToCheckTypeMap[cmd.MonitorTypeId-1]),
	}
	err = sqlstore.ValidateCheckRoute(&check)
	if err != nil {
		handleError(c, err)
		return
	}
	endpoint.Checks = append(endpoint.Checks, check)

	//Update endpoint
	err = sqlstore.UpdateEndpoint(endpoint)
	if err != nil {
		handleError(c, err)
		return
	}

	var monitor m.MonitorDTO
	for _, check := range endpoint.Checks {

		if check.Type == m.MonitorTypeToCheckTypeMap[cmd.MonitorTypeId-1] {
			monitor = m.MonitorDTOFromCheck(check, endpoint.Slug)
			if check.Enabled {
				probeList, err := sqlstore.GetProbesForCheck(&check)
				if err != nil {
					handleError(c, err)
					return
				}

				monitor.Collectors = probeList
			}
			break
		}
	}

	c.JSON(200, monitor)
	return
}