mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #12318 from zhaoxiangchun/feature/zxc-usage-domain-3.8
feat(cloudmon): usage add domain.servers and project.servers statistic
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
package collectors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -79,6 +81,12 @@ func reportUsage(session *mcclient.ClientSession, args *common.ReportOptions) er
|
||||
}
|
||||
//查询host-type==""的情况,对应onecloud-控制面板-全部 要展示的内容
|
||||
dataList, err = packMetricList(session, dataList, imageUsageFieldsDict, "host-type", "", nowTime)
|
||||
//获取域/项目 虚拟机usage
|
||||
data, err := getDomainAndProjectServerUsage(session, nowTime)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getDomainAndProjectServerUsage err")
|
||||
}
|
||||
dataList = append(dataList, data...)
|
||||
//写入influDb
|
||||
return sendMetrics(session, dataList, args.Debug)
|
||||
}
|
||||
@@ -162,3 +170,49 @@ func jsonTometricData(obj *jsonutils.JSONDict, metric *influxdb.SMetricData,
|
||||
metric.Metrics = append(metric.Metrics, metricPairs...)
|
||||
return metric, nil
|
||||
}
|
||||
|
||||
type resourceUsage struct {
|
||||
Count string
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
|
||||
func getDomainAndProjectServerUsage(session *mcclient.ClientSession, nowTime time.Time) ([]influxdb.SMetricData, error) {
|
||||
metrics := make([]influxdb.SMetricData, 0)
|
||||
for urlKey, tag := range map[string]string{
|
||||
"domain-statistics": "domain_id.project_domain",
|
||||
"project-statistics": "tenant_id.tenant",
|
||||
} {
|
||||
jsonObject, err := modules.Servers.GetById(session, urlKey, jsonutils.NewDict())
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "get server-%s err", urlKey)
|
||||
}
|
||||
usageArr := jsonObject.(*jsonutils.JSONArray)
|
||||
for i := 0; i < usageArr.Size(); i++ {
|
||||
metric := influxdb.SMetricData{Name: measureMent, Timestamp: nowTime}
|
||||
usageObj, _ := usageArr.GetAt(i)
|
||||
usage := new(resourceUsage)
|
||||
usageObj.Unmarshal(usage)
|
||||
key := ""
|
||||
if strings.Contains(urlKey, "domain") {
|
||||
key = "domain"
|
||||
} else {
|
||||
key = "project"
|
||||
}
|
||||
metric.Metrics = append(metric.Metrics, influxdb.SKeyValue{
|
||||
Key: fmt.Sprintf("%s.servers", key),
|
||||
Value: usage.Count,
|
||||
})
|
||||
tagArr := strings.Split(tag, ".")
|
||||
metric.Tags = append(metric.Tags, influxdb.SKeyValue{
|
||||
Key: tagArr[0],
|
||||
Value: usage.Id,
|
||||
}, influxdb.SKeyValue{
|
||||
Key: tagArr[1],
|
||||
Value: usage.Name,
|
||||
})
|
||||
metrics = append(metrics, metric)
|
||||
}
|
||||
}
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user