示例#1
0
func GetCollectorByName(query *m.GetCollectorByNameQuery) error {
	sess := x.Table("collector")
	rawParams := make([]interface{}, 0)
	rawSql := `SELECT
		GROUP_CONCAT(DISTINCT(collector_tag.tag)) as tags,
		collector.*
	FROM collector
	LEFT JOIN collector_tag ON collector.id = collector_tag.collector_id AND collector_tag.org_id=?
	WHERE
		(collector.public=1 OR collector.org_id=?)
	AND
		collector.name=?
	GROUP BY collector.id
	`
	rawParams = append(rawParams, query.OrgId, query.OrgId, query.Name)
	results := make([]CollectorWithTag, 0)
	err := sess.Sql(rawSql, rawParams...).Find(&results)
	if err != nil {
		return err
	}
	if len(results) < 1 {
		return m.ErrCollectorNotFound
	}

	result := results[0]

	tags := make([]string, 0)
	if result.Tags != "" {
		tags = strings.Split(result.Tags, ",")
	}

	query.Result = &m.CollectorDTO{
		Id:            result.Id,
		OrgId:         result.OrgId,
		Name:          result.Name,
		Slug:          result.Slug,
		Tags:          tags,
		Latitude:      result.Latitude,
		Longitude:     result.Longitude,
		Public:        result.Public,
		Online:        result.Online,
		OnlineChange:  result.OnlineChange,
		Enabled:       result.Enabled,
		EnabledChange: result.EnabledChange,
	}

	return err
}
示例#2
0
func register(so socketio.Socket) (*CollectorContext, error) {
	req := so.Request()
	req.ParseForm()
	keyString := req.Form.Get("apiKey")
	name := req.Form.Get("name")
	if name == "" {
		return nil, errors.New("collector name not provided.")
	}

	lastSocketId := req.Form.Get("lastSocketId")

	versionStr := req.Form.Get("version")
	if versionStr == "" {
		return nil, errors.New("version number not provided.")
	}
	versionParts := strings.SplitN(versionStr, ".", 2)
	if len(versionParts) != 2 {
		return nil, errors.New("could not parse version number")
	}
	versionMajor, err := strconv.ParseInt(versionParts[0], 10, 64)
	if err != nil {
		return nil, errors.New("could not parse version number")
	}
	versionMinor, err := strconv.ParseFloat(versionParts[1], 64)
	if err != nil {
		return nil, errors.New("could not parse version number.")
	}

	//--------- set required version of collector.------------//
	//
	if versionMajor < 0 || versionMinor < 1.1 {
		return nil, errors.New("invalid collector version. Please upgrade.")
	}
	//
	//--------- set required version of collector.------------//
	log.Info("collector %s with version %d.%f connected", name, versionMajor, versionMinor)
	if keyString != "" {
		// base64 decode key
		decoded, err := apikeygen.Decode(keyString)
		if err != nil {
			return nil, m.ErrInvalidApiKey
		}
		// fetch key
		keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
		if err := bus.Dispatch(&keyQuery); err != nil {
			return nil, m.ErrInvalidApiKey
		}
		apikey := keyQuery.Result

		// validate api key
		if !apikeygen.IsValid(decoded, apikey.Key) {
			return nil, m.ErrInvalidApiKey
		}
		// lookup collector
		colQuery := m.GetCollectorByNameQuery{Name: name, OrgId: apikey.OrgId}
		if err := bus.Dispatch(&colQuery); err != nil {
			//collector not found, so lets create a new one.
			colCmd := m.AddCollectorCommand{
				OrgId:   apikey.OrgId,
				Name:    name,
				Enabled: true,
			}
			if err := bus.Dispatch(&colCmd); err != nil {
				return nil, err
			}
			colQuery.Result = colCmd.Result
		}

		sess := &CollectorContext{
			SignedInUser: &m.SignedInUser{
				IsGrafanaAdmin: apikey.IsAdmin,
				OrgRole:        apikey.Role,
				ApiKeyId:       apikey.Id,
				OrgId:          apikey.OrgId,
				Name:           apikey.Name,
			},
			Collector: colQuery.Result,
			Socket:    so,
			SocketId:  so.Id(),
		}
		log.Info("collector %s with id %d owned by %d authenticated successfully.", name, colQuery.Result.Id, apikey.OrgId)
		if lastSocketId != "" {
			log.Info("removing socket with Id %s", lastSocketId)
			cmd := &m.DeleteCollectorSessionCommand{
				SocketId:    lastSocketId,
				OrgId:       sess.OrgId,
				CollectorId: sess.Collector.Id,
			}
			if err := bus.Dispatch(cmd); err != nil {
				log.Error(0, "failed to remove collectors lastSocketId", err)
				return nil, err
			}
		}

		if err := sess.Save(); err != nil {
			return nil, err
		}
		log.Info("saving session to contextCache")
		contextCache.Set(sess.SocketId, sess)
		log.Info("session saved to contextCache")
		return sess, nil
	}
	return nil, m.ErrInvalidApiKey
}