Пример #1
0
func AddMonitor(c *middleware.Context, cmd m.AddMonitorCommand) Response {
	cmd.OrgId = c.OrgId

	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to add monitor", err)
	}

	return Json(200, cmd.Result)
}
Пример #2
0
func AddMonitor(c *middleware.Context, cmd m.AddMonitorCommand) Response {
	cmd.OrgId = c.OrgId
	if cmd.EndpointId == 0 {
		return ApiError(400, "EndpointId not set.", nil)
	}
	if cmd.MonitorTypeId == 0 {
		return ApiError(400, "MonitorTypeId not set.", nil)
	}
	if cmd.Frequency == 0 {
		return ApiError(400, "Frequency not set.", nil)
	}

	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to add monitor", err)
	}

	return Json(200, cmd.Result)
}
Пример #3
0
func addMonitorTransaction(cmd *m.AddMonitorCommand, sess *session) error {
	//validate Endpoint.
	endpointQuery := m.GetEndpointByIdQuery{
		Id:    cmd.EndpointId,
		OrgId: cmd.OrgId,
	}
	err := GetEndpointByIdTransaction(&endpointQuery, sess)
	if err != nil {
		return err
	}

	filtered_collectors := make([]*collectorList, 0, len(cmd.CollectorIds))
	if len(cmd.CollectorIds) > 0 {
		sess.Table("collector")
		sess.In("id", cmd.CollectorIds).Where("org_id=? or public=1", cmd.OrgId)
		sess.Cols("id")
		err = sess.Find(&filtered_collectors)

		if err != nil {
			return err
		}
	}

	if len(filtered_collectors) < len(cmd.CollectorIds) {
		return m.ErrMonitorCollectorsInvalid
	}

	//get settings definition for thie monitorType.
	var typeSettings []*m.MonitorTypeSetting
	sess.Table("monitor_type_setting")
	sess.Where("monitor_type_id=?", cmd.MonitorTypeId)
	err = sess.Find(&typeSettings)
	if err != nil {
		return nil
	}

	// push the typeSettings into a Map with the variable name as key
	settingMap := make(map[string]*m.MonitorTypeSetting)
	for _, s := range typeSettings {
		settingMap[s.Variable] = s
	}

	//validate the settings.
	seenMetrics := make(map[string]bool)
	for _, v := range cmd.Settings {
		def, ok := settingMap[v.Variable]
		if ok != true {
			log.Info("Unkown variable %s passed.", v.Variable)
			return m.ErrMonitorSettingsInvalid
		}
		//TODO:(awoods) make sure the value meets the definition.
		seenMetrics[def.Variable] = true
		log.Info("%s present in settings", def.Variable)
	}

	//make sure all required variables were provided.
	//add defaults for missing optional variables.
	for k, s := range settingMap {
		if _, ok := seenMetrics[k]; ok != true {
			log.Info("%s not in settings", k)
			if s.Required {
				// required setting variable missing.
				return m.ErrMonitorSettingsInvalid
			}
			cmd.Settings = append(cmd.Settings, &m.MonitorSettingDTO{
				Variable: k,
				Value:    s.DefaultValue,
			})
		}
	}

	mon := &m.Monitor{
		EndpointId:     cmd.EndpointId,
		OrgId:          cmd.OrgId,
		MonitorTypeId:  cmd.MonitorTypeId,
		Offset:         cmd.EndpointId % cmd.Frequency,
		Settings:       cmd.Settings,
		HealthSettings: cmd.HealthSettings,
		Created:        time.Now(),
		Updated:        time.Now(),
		Frequency:      cmd.Frequency,
		Enabled:        cmd.Enabled,
		State:          -1,
		StateChange:    time.Now(),
		StateCheck:     time.Now(),
	}
	if _, err := sess.Insert(mon); err != nil {
		return err
	}

	if len(cmd.CollectorIds) > 0 {
		monitor_collectors := make([]*m.MonitorCollector, len(cmd.CollectorIds))
		for i, l := range cmd.CollectorIds {
			monitor_collectors[i] = &m.MonitorCollector{
				MonitorId:   mon.Id,
				CollectorId: l,
			}
		}
		sess.Table("monitor_collector")
		if _, err := sess.Insert(&monitor_collectors); err != nil {
			return err
		}
	}

	if len(cmd.CollectorTags) > 0 {
		monitor_collector_tags := make([]*m.MonitorCollectorTag, len(cmd.CollectorTags))
		for i, t := range cmd.CollectorTags {
			monitor_collector_tags[i] = &m.MonitorCollectorTag{
				MonitorId: mon.Id,
				Tag:       t,
			}
		}

		sess.Table("monitor_collector_tag")
		if _, err := sess.Insert(&monitor_collector_tags); err != nil {
			return err
		}
	}
	// get collectorIds from tags
	tagCollectors, err := getCollectorIdsFromTags(cmd.OrgId, cmd.CollectorTags, sess)
	if err != nil {
		return err
	}

	collectorIdMap := make(map[int64]bool)
	collectorList := make([]int64, 0)
	for _, id := range cmd.CollectorIds {
		collectorIdMap[id] = true
		collectorList = append(collectorList, id)
	}

	for _, id := range tagCollectors {
		if _, ok := collectorIdMap[id]; !ok {
			collectorList = append(collectorList, id)
		}
	}

	cmd.Result = &m.MonitorDTO{
		Id:             mon.Id,
		EndpointId:     mon.EndpointId,
		OrgId:          mon.OrgId,
		EndpointSlug:   endpointQuery.Result.Slug,
		MonitorTypeId:  mon.MonitorTypeId,
		CollectorIds:   cmd.CollectorIds,
		CollectorTags:  cmd.CollectorTags,
		Collectors:     collectorList,
		Settings:       mon.Settings,
		HealthSettings: mon.HealthSettings,
		Frequency:      mon.Frequency,
		Enabled:        mon.Enabled,
		State:          mon.State,
		StateChange:    mon.StateChange,
		StateCheck:     mon.StateCheck,
		Offset:         mon.Offset,
		Updated:        mon.Updated,
	}
	sess.publishAfterCommit(&events.MonitorCreated{
		Timestamp: mon.Updated,
		MonitorPayload: events.MonitorPayload{
			Id:            mon.Id,
			EndpointId:    mon.EndpointId,
			OrgId:         mon.OrgId,
			EndpointSlug:  endpointQuery.Result.Slug,
			MonitorTypeId: mon.MonitorTypeId,
			CollectorIds:  cmd.CollectorIds,
			CollectorTags: cmd.CollectorTags,
			Collectors:    collectorList,
			Settings:      mon.Settings,
			Frequency:     mon.Frequency,
			Enabled:       mon.Enabled,
			Offset:        mon.Offset,
			Updated:       mon.Updated,
		},
	})
	return nil
}