示例#1
0
func (s *RPCServer) fetch_last_metrics(host_name string, n_get uint32) (map[string][]metric.MetricValue, error) {
	last_metric_values := make(map[string][]metric.MetricValue)
	if n_get > 0 {
		metrics, err := s.metric_srv.GetMetricsByHost(host_name)
		if err != nil {
			return nil, err
		}
		for _, m := range metrics {
			var m_values []metric.MetricValue
			if m.GetType() == metric.METRIC_DERIVE {
				n_get++
			}
			m_values, err = s.metric_srv.GetLastMetricValues(m, 0, int(n_get+1))
			if err != nil {
				log.Printf("Cannot fetch metric values: %s. Continue to other metric...\n", err)
				continue
			}
			if m.GetType() == metric.METRIC_DERIVE {
				m_values, err = metric.DeriveMetricValues(m_values, true)
				if err != nil {
					continue
				}
			}
			last_metric_values[metric.GetMetricString(m)] = m_values
		}
	}
	return last_metric_values, nil
}
示例#2
0
func (s *RPCServer) GetAllMetrics(req *rpc.AllMetricsRequest, stream rpc.Backend_GetAllMetricsServer) error {
	var metrics []metric.Metric
	var err error
	if req.GetFilterHostName() {
		metrics, err = s.metric_srv.GetMetricsByHost(req.GetHostName())
	} else {
		metrics, err = s.metric_srv.GetAllMetrics()
	}
	if err != nil {
		log.Printf("Cannot get all available metrics: %v\n", err)
		return err
	}
	var n_skip_counter int64 = 0
	var n_take_counter int64 = 0
	for _, m := range metrics {
		if n_skip_counter < req.GetNSkip() {
			n_skip_counter++
			continue
		}
		if req.GetNTake() >= 0 && n_take_counter == req.GetNTake() {
			break
		}
		out := &rpc.Metric{}
		count, err := s.metric_srv.CountValues(m)
		if err != nil {
			log.Printf("Cannot get metrics count: %v\n", err)
			continue
		}
		last_value, err := s.metric_srv.GetLastMetricValue(m)
		if err != nil {
			log.Printf("Cannot get last metric value: %v\n", err)
			continue
		}
		out.Name = proto.String(metric.GetMetricString(m))
		out.Kind = rpc.Metric_Kind(m.GetType()).Enum()
		out.NRecords = proto.Int64(count)
		out.TimeLastRecord = proto.Int64(last_value.Time.Unix())
		stream.Send(out)
		n_take_counter++
	}
	return nil
}