Example #1
0
func collect(ts time.Time, statsUrl string) {
	// TODO: At some point we can look at refactoring this to use the
	// 'serviced metric' code

	// collect eth0 statistics
	netStats, err := readInt64Stats(eth0StatsDir)
	if err != nil {
		glog.Errorf("Could not collect eth0 stats: %s", err)
		return
	}

	// convert netStats to samples
	samples := make([]stats.Sample, len(netStats))
	now := ts.Unix()
	tags := map[string]string{"component": "eth0"}
	i := 0
	for name, value := range netStats {
		samples[i] = stats.Sample{
			Metric:    "net." + name,
			Value:     strconv.FormatInt(value, 10),
			Timestamp: now,
			Tags:      tags,
		}
		i++
	}

	// collect open connection statistics
	for proto, procFile := range procNetFiles {
		conns, err := getOpenConnections(procFile)
		if err != nil {
			glog.Errorf("Could not collect open connection information: %s", err)
			return
		}

		sample := stats.Sample{
			Metric:    "net.open_connections." + proto,
			Value:     strconv.FormatInt(int64(conns), 10),
			Timestamp: now,
			Tags:      map[string]string{"protocol": proto},
		}
		samples = append(samples, sample)
	}

	glog.V(4).Infof("posting samples: %+v", samples)
	if err := stats.Post(statsUrl, samples); err != nil {
		glog.Errorf("could not post stats: %s", err)
	}
}
Example #2
0
func (a *api) PostMetric(metricName string, metricValue string) (string, error) {
	url := fmt.Sprintf("http://%s/api/metrics/store", options.HostStats)
	timeStamp := time.Now().Unix()
	hostId, err := utils.HostID()
	if err != nil {
		glog.Errorf("Error getting host id, error: %s", err)
		return "", err
	}

	samples := make([]stats.Sample, 1)
	samples[0] = stats.Sample{
		Metric:    metricName,
		Value:     metricValue,
		Timestamp: timeStamp,
		Tags:      map[string]string{"controlplane_host_id": hostId},
	}

	if err := stats.Post(url, samples); err != nil {
		glog.Errorf("could not post stats: %s", err)
		return "", err
	}
	return "Posted metric", nil
}