From d8fc57b217d74ebcb89fc0eda505d646e0c10358 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun Date: Mon, 17 Feb 2020 11:51:08 +0800 Subject: [PATCH] =?UTF-8?q?zstack=E7=9B=91=E6=8E=A7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit monitor.go中GetMonitorData函数通过传入的参数拼装请求参数,然后调用zstack.go中的getMonitor(),通过GET请求,获取相应的指标监控数据 --- pkg/multicloud/zstack/monitor.go | 38 ++++++++++++++++++++++++++++ pkg/multicloud/zstack/zstack.go | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 pkg/multicloud/zstack/monitor.go diff --git a/pkg/multicloud/zstack/monitor.go b/pkg/multicloud/zstack/monitor.go new file mode 100644 index 0000000000..52d8b620be --- /dev/null +++ b/pkg/multicloud/zstack/monitor.go @@ -0,0 +1,38 @@ +package zstack + +import ( + "time" + + "yunion.io/x/jsonutils" +) + +type SDataPoint struct { + DataPoints []DataPoint `json:"data"` +} + +type DataPoint struct { + Value float64 `json:"value"` + TimeStemp int64 `json:"time"` + Labels *Label `json:"labels"` +} + +type Label struct { + VMUuid string `json:"VMUuid"` +} + +func (region *SRegion) GetMonitorData(name string, namespace string, since time.Time, + until time.Time) (*SDataPoint, error) { + datas := SDataPoint{} + param := jsonutils.NewDict() + param.Add(jsonutils.NewString(namespace), "namespace") + param.Add(jsonutils.NewString(name), "metricName") + param.Add(jsonutils.NewString("60"), "period") + param.Add(jsonutils.NewInt(since.Unix()), "startTime") + param.Add(jsonutils.NewInt(until.Unix()), "endTime") + rep, err := region.client.getMonitor("zwatch/metrics", param) + if err != nil { + return nil, err + } + rep.Unmarshal(&datas) + return &datas, nil +} diff --git a/pkg/multicloud/zstack/zstack.go b/pkg/multicloud/zstack/zstack.go index 86f81a4d88..884a8a7455 100644 --- a/pkg/multicloud/zstack/zstack.go +++ b/pkg/multicloud/zstack/zstack.go @@ -310,6 +310,49 @@ func (cli *SZStackClient) getResource(resource, resourceId string, retval interf return cloudprovider.ErrDuplicateId } +func (cli *SZStackClient) getMonitor(resource string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { + + return cli._getMonitor(resource, params) +} + +func (cli *SZStackClient) _getMonitor(resource string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { + client := httputils.GetDefaultClient() + header := http.Header{} + requestURL := cli.getPostURL(resource) + paramDict := params.(*jsonutils.JSONDict) + if paramDict.Size() > 0 { + values := url.Values{} + for _, key := range paramDict.SortedKeys() { + value, _ := paramDict.GetString(key) + values.Add(key, value) + } + requestURL += fmt.Sprintf("?%s", values.Encode()) + } + var resp jsonutils.JSONObject + startTime := time.Now() + for time.Now().Sub(startTime) < time.Minute*5 { + err := cli.sign(requestURL, "GET", header) + if err != nil { + return nil, err + } + _, resp, err = httputils.JSONRequest(client, context.Background(), "GET", requestURL, header, nil, cli.debug) + if err != nil { + if strings.Contains(err.Error(), "exceeded while awaiting headers") { + time.Sleep(time.Second * 5) + continue + } + return nil, errors.Wrapf(err, fmt.Sprintf("GET %s %s", resource, params)) + } + break + } + + if resp.Contains("location") { + location, _ := resp.GetString("location") + return cli.wait(client, header, "get", requestURL, jsonutils.NewDict(), location) + } + return resp, nil +} + func (cli *SZStackClient) get(resource, resourceId string, spec string) (jsonutils.JSONObject, error) { return cli._get(resource, resourceId, spec) }