예제 #1
0
func (pk *Peekachu) refreshMetricValuesForClient(
	client *Client,
	table string,
	names []string,
) {

	glog.V(3).Infof("Fetching metric values for host %s...", client.PcpClient.Host)
	query := pcp.NewMetricValueQuery(names, []string{})

	if resp, err := client.PcpClient.MetricValues(query); err != nil {
		msg := "Failed to retrieve metric values from host %s : %s : %#v\n"
		glog.Errorf(msg, client.PcpClient.Host, err)
	} else {
		for _, value := range resp.Values {
			metric := client.Metrics.FindMetricByName(value.MetricName)
			indom, err := client.PcpClient.GetIndomForMetric(metric)

			if err != nil {
				glog.Errorf(
					"Failed to get indom for metric '%s' with instance domain '%d': %s\n",
					metric.Name,
					metric.Indom,
					err,
				)
				glog.Errorln("Refusing to update value due to prior errors.")
			} else {
				value.UpdateInstanceNames(indom)
			}
		}

		client.MetricValueResponses[table] = append(
			client.MetricValueResponses[table],
			resp,
		)

	}
}
예제 #2
0
파일: main.go 프로젝트: doctorjnupe/pcp
func main() {
	logger.Infoln("Starting...")

	context := pcp.NewContext(hostname, "")
	client := pcp.NewClient(endpoint, context)
	client.SetLogLevel(pcp.LOG_DEBUG)
	// var query pcp.Query

	err := client.RefreshContext()
	logger.Debugln(client.Context)

	if err != nil {
		logger.Errorf("Received error refreshing context: %s", err)
		os.Exit(1)
	}

	metrics, err := client.Metrics(pcp.NewMetricQuery(""))

	if err != nil {
		logger.Errorf("Received error retrieving metrics: %s", err)
		os.Exit(1)
	}

	logger.Infof("Retrieved %d unique metrics from context", len(metrics))

	// Get values for first 5 metrics by name
	var names []string
	for _, metric := range metrics[:50] {
		names = append(names, metric.Name)
	}

	metric_values_query := pcp.NewMetricValueQuery(names, []string{})
	resp, err := client.MetricValues(metric_values_query)

	logger.Debugln(metrics.MetricValueType(resp.Values[0]))

	if err != nil {
		logger.Errorf("Received error retrieving metric values: %s\n", err)
	}

	// update the metric values with their metric names
	for _, value := range resp.Values {
		metric := metrics.FindMetricByName(value.MetricName)
		indom, err := client.GetIndomForMetric(metric)
		logger.Debugln(indom)
		if err != nil {
			logger.Errorf("Failed to find Instance Domain for metric: %s", err)
		}
		value.UpdateInstanceNames(indom)
		logger.Debugln(value)
	}

	// Get all stats for containers on a host.
	q3 := pcp.NewMetricValueQuery([]string{"containers.name"}, []string{})
	containers, err := client.MetricValues(q3)
	if err != nil {
		logger.Errorf("Container query failed: %s", err)
	}
	// /pmapi/_context?hostspec=local:?container=fooba
	// get a name
	cname := containers.Values[0].Instances[0].Value
	// create a new context
	spec := fmt.Sprintf("local:?container=%s", cname)
	c_context := pcp.NewContext("", spec)
	c_client := pcp.NewClient(endpoint, c_context)
	c_client.SetLogLevel(pcp.LOG_DEBUG)
	err = c_client.RefreshContext()
	if err != nil {
		logger.Errorf("Query failed with error: %s", err)
	}
	names = []string{
		"cgroup.cpuacct.stat.user",
		"cgroup.cpuacct.stat.system",
		"cgroup.memory.usage",
	}
	for _, name := range names {
		c_query := pcp.NewMetricValueQuery([]string{name}, []string{})
		resp, err = c_client.MetricValues(c_query)

		if err != nil {
			logger.Errorf("Query failed with error: %s", err)
		}
		logger.Debugln(resp.Values)
	}

}