zstack监控功能

monitor.go中GetMonitorData函数通过传入的参数拼装请求参数,然后调用zstack.go中的getMonitor(),通过GET请求,获取相应的指标监控数据
This commit is contained in:
zhaoxiangchun
2020-02-17 12:03:17 +08:00
parent 4b6d0107b0
commit d8fc57b217
2 changed files with 81 additions and 0 deletions
+38
View File
@@ -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
}
+43
View File
@@ -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)
}