Exemplo n.º 1
0
func (self *version2_0) HandleRequest(requestType string, request []string, m manager.Manager, w http.ResponseWriter, r *http.Request) error {
	opt, err := getRequestOptions(r)
	if err != nil {
		return err
	}
	switch requestType {
	case versionApi:
		glog.V(4).Infof("Api - Version")
		versionInfo, err := m.GetVersionInfo()
		if err != nil {
			return err
		}
		return writeResult(versionInfo.CadvisorVersion, w)
	case attributesApi:
		glog.V(4).Info("Api - Attributes")

		machineInfo, err := m.GetMachineInfo()
		if err != nil {
			return err
		}
		versionInfo, err := m.GetVersionInfo()
		if err != nil {
			return err
		}
		info := v2.GetAttributes(machineInfo, versionInfo)
		return writeResult(info, w)
	case machineApi:
		glog.V(4).Info("Api - Machine")

		// TODO(rjnagal): Move machineInfo from v1.
		machineInfo, err := m.GetMachineInfo()
		if err != nil {
			return err
		}
		return writeResult(machineInfo, w)
	case summaryApi:
		containerName := getContainerName(request)
		glog.V(4).Infof("Api - Summary for container %q, options %+v", containerName, opt)

		stats, err := m.GetDerivedStats(containerName, opt)
		if err != nil {
			return err
		}
		return writeResult(stats, w)
	case statsApi:
		name := getContainerName(request)
		glog.V(4).Infof("Api - Stats: Looking for stats for container %q, options %+v", name, opt)
		infos, err := m.GetRequestedContainersInfo(name, opt)
		if err != nil {
			if len(infos) == 0 {
				return err
			}
			glog.Errorf("Error calling GetRequestedContainersInfo: %v", err)
		}
		contStats := make(map[string][]v2.DeprecatedContainerStats, 0)
		for name, cinfo := range infos {
			contStats[name] = v2.DeprecatedStatsFromV1(cinfo)
		}
		return writeResult(contStats, w)
	case customMetricsApi:
		containerName := getContainerName(request)
		glog.V(4).Infof("Api - Custom Metrics: Looking for metrics for container %q, options %+v", containerName, opt)
		infos, err := m.GetContainerInfoV2(containerName, opt)
		if err != nil {
			return err
		}
		contMetrics := make(map[string]map[string]map[string][]info.MetricValBasic, 0)
		for _, cinfo := range infos {
			metrics := make(map[string]map[string][]info.MetricValBasic, 0)
			for _, contStat := range cinfo.Stats {
				if len(contStat.CustomMetrics) == 0 {
					continue
				}
				for name, allLabels := range contStat.CustomMetrics {
					metricLabels := make(map[string][]info.MetricValBasic, 0)
					for _, metric := range allLabels {
						if !metric.Timestamp.IsZero() {
							metVal := info.MetricValBasic{
								Timestamp:  metric.Timestamp,
								IntValue:   metric.IntValue,
								FloatValue: metric.FloatValue,
							}
							labels := metrics[name]
							if labels != nil {
								values := labels[metric.Label]
								values = append(values, metVal)
								labels[metric.Label] = values
								metrics[name] = labels
							} else {
								metricLabels[metric.Label] = []info.MetricValBasic{metVal}
								metrics[name] = metricLabels
							}
						}
					}
				}
			}
			contMetrics[containerName] = metrics
		}
		return writeResult(contMetrics, w)
	case specApi:
		containerName := getContainerName(request)
		glog.V(4).Infof("Api - Spec for container %q, options %+v", containerName, opt)
		specs, err := m.GetContainerSpec(containerName, opt)
		if err != nil {
			return err
		}
		return writeResult(specs, w)
	case storageApi:
		var err error
		fi := []v2.FsInfo{}
		label := r.URL.Query().Get("label")
		if len(label) == 0 {
			// Get all global filesystems info.
			fi, err = m.GetFsInfo("")
			if err != nil {
				return err
			}
		} else {
			// Get a specific label.
			fi, err = m.GetFsInfo(label)
			if err != nil {
				return err
			}
		}
		return writeResult(fi, w)
	case eventsApi:
		return handleEventRequest(request, m, w, r)
	case psApi:
		// reuse container type from request.
		// ignore recursive.
		// TODO(rjnagal): consider count to limit ps output.
		name := getContainerName(request)
		glog.V(4).Infof("Api - Spec for container %q, options %+v", name, opt)
		ps, err := m.GetProcessList(name, opt)
		if err != nil {
			return fmt.Errorf("process listing failed: %v", err)
		}
		return writeResult(ps, w)
	default:
		return fmt.Errorf("unknown request type %q", requestType)
	}
}