コード例 #1
0
func (m *Mesos) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error) {
	configItems, err := getConfig(cfg)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	metricTypes := []plugin.MetricType{}

	if configItems["master"] != "" {
		log.Info("Getting metric types for the Mesos master at ", configItems["master"])
		master_mts, err := master.GetMetricsSnapshot(configItems["master"])
		if err != nil {
			log.Error(err)
			return nil, err
		}

		for key, _ := range master_mts {
			namespace := core.NewNamespace(pluginVendor, pluginName, "master").
				AddStaticElements(strings.Split(key, "/")...)
			log.Debug("Adding metric to catalog: ", namespace.String())
			metricTypes = append(metricTypes, plugin.MetricType{Namespace_: namespace})
		}

		framework_mts, err := master.GetFrameworksMetricTypes()
		if err != nil {
			log.Error(err)
			return nil, err
		}

		for _, key := range framework_mts {
			namespace := core.NewNamespace(pluginVendor, pluginName, "master").
				AddDynamicElement("framework_id", "Framework ID").
				AddStaticElements(strings.Split(key, "/")...)
			log.Debug("Adding metric to catalog: ", namespace.String())
			metricTypes = append(metricTypes, plugin.MetricType{Namespace_: namespace})
		}
	}

	if configItems["agent"] != "" {
		log.Info("Getting metric types for the Mesos agent at ", configItems["agent"])
		agent_mts, err := agent.GetMetricsSnapshot(configItems["agent"])
		if err != nil {
			log.Error(err)
			return nil, err
		}

		for key, _ := range agent_mts {
			namespace := core.NewNamespace(pluginVendor, pluginName, "agent").
				AddStaticElements(strings.Split(key, "/")...)
			log.Debug("Adding metric to catalog: ", namespace.String())
			metricTypes = append(metricTypes, plugin.MetricType{Namespace_: namespace})
		}

		agent_stats, err := agent.GetMonitoringStatisticsMetricTypes(configItems["agent"])
		if err != nil {
			log.Error(err)
			return nil, err
		}

		for _, key := range agent_stats {
			namespace := core.NewNamespace(pluginVendor, pluginName, "agent").
				AddDynamicElement("framework_id", "Framework ID").
				AddDynamicElement("executor_id", "Executor ID").
				AddStaticElements(strings.Split(key, "/")...)
			log.Debug("Adding metric to catalog: ", namespace.String())
			metricTypes = append(metricTypes, plugin.MetricType{Namespace_: namespace})
		}
	}

	return metricTypes, nil
}
コード例 #2
0
func (m *Mesos) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, error) {
	configItems, err := getConfig(mts[0])
	if err != nil {
		return nil, err
	}

	requestedMaster := []core.Namespace{}
	requestedAgent := []core.Namespace{}

	for _, metricType := range mts {
		switch metricType.Namespace().Strings()[2] {
		case "master":
			requestedMaster = append(requestedMaster, metricType.Namespace())
		case "agent":
			requestedAgent = append(requestedAgent, metricType.Namespace())
		}
	}

	// Translate Mesos metrics into Snap PluginMetrics
	now := time.Now()
	metrics := []plugin.MetricType{}

	if configItems["master"] != "" && len(requestedMaster) > 0 {
		log.Info("Collecting ", len(requestedMaster), " metrics from the master")
		isLeader, err := master.IsLeader(configItems["master"])
		if err != nil {
			log.Error(err)
			return nil, err
		}
		if isLeader {
			snapshot, err := master.GetMetricsSnapshot(configItems["master"])
			if err != nil {
				log.Error(err)
				return nil, err
			}

			frameworks, err := master.GetFrameworks(configItems["master"])
			if err != nil {
				log.Error(err)
				return nil, err
			}

			tags := map[string]string{"source": configItems["master"]}

			for _, requested := range requestedMaster {
				isDynamic, _ := requested.IsDynamic()
				if isDynamic {
					n := requested.Strings()[4:]

					// Iterate through the array of frameworks returned by GetFrameworks()
					for _, framework := range frameworks {
						val := ns.GetValueByNamespace(framework, n)
						if val == nil {
							log.Warn("Attempted to collect metric ", requested.String(), " but it returned nil!")
							continue
						}
						// substituting "framework" wildcard with particular framework id
						requested[3].Value = framework.ID
						// TODO(roger): units
						metrics = append(metrics, *plugin.NewMetricType(requested, now, tags, "", val))

					}
				} else {
					n := requested.Strings()[3:]
					val, ok := snapshot[strings.Join(n, "/")]
					if !ok {
						e := fmt.Errorf("error: requested metric %s not found", requested.String())
						log.Error(e)
						return nil, e
					}
					//TODO(kromar): is it possible to provide unit NewMetricType(ns, time, tags, unit, value)?
					// I'm leaving empty string for now...
					metrics = append(metrics, *plugin.NewMetricType(requested, now, tags, "", val))
				}
			}
		} else {
			log.Info("Attempted CollectMetrics() on ", configItems["master"], "but it isn't the leader. Skipping...")
		}
	}

	if configItems["agent"] != "" && len(requestedAgent) > 0 {
		log.Info("Collecting ", len(requestedAgent), " metrics from the agent")
		snapshot, err := agent.GetMetricsSnapshot(configItems["agent"])
		if err != nil {
			log.Error(err)
			return nil, err
		}

		executors, err := agent.GetMonitoringStatistics(configItems["agent"])
		if err != nil {
			log.Error(err)
			return nil, err
		}

		tags := map[string]string{"source": configItems["agent"]}

		for _, requested := range requestedAgent {
			n := requested.Strings()[5:]
			isDynamic, _ := requested.IsDynamic()
			if isDynamic {
				// Iterate through the array of executors returned by GetMonitoringStatistics()
				for _, exec := range executors {
					val := ns.GetValueByNamespace(exec.Statistics, n)
					if val == nil {
						log.Warn("Attempted to collect metric ", requested.String(), " but it returned nil!")
						continue
					}
					// substituting "framework" wildcard with particular framework id
					requested[3].Value = exec.Framework
					// substituting "executor" wildcard with particular executor id
					requested[4].Value = exec.ID
					// TODO(roger): units
					metrics = append(metrics, *plugin.NewMetricType(requested, now, tags, "", val))

				}
			} else {
				// Get requested metrics from the snapshot map
				n := requested.Strings()[3:]
				val, ok := snapshot[strings.Join(n, "/")]
				if !ok {
					e := fmt.Errorf("error: requested metric %v not found", requested.String())
					log.Error(e)
					return nil, e
				}

				//TODO(kromar): units here also?
				metrics = append(metrics, *plugin.NewMetricType(requested, now, tags, "", val))
			}
		}
	}

	log.Debug("Collected a total of ", len(metrics), " metrics.")
	return metrics, nil
}