[REV] Add disk usage statistics

This commit is contained in:
pujielan 2018-01-12 17:29:43 +08:00
parent 04321e1d49
commit c19740724f
4 changed files with 34 additions and 10 deletions

View File

@ -36,7 +36,7 @@ type TenantHandler interface {
TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)
//QueryTsdb(md *api_model.MontiorData) (*tsdbClient.QueryResponse, error)
HTTPTsdb(md *api_model.MontiorData) ([]byte, error)
GetTenantsResources(tr *api_model.TenantResources) ([]*map[string]interface{}, error)
GetTenantsResources(tr *api_model.TenantResources) ([]map[string]interface{}, error)
TenantsSum() (int, error)
GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError)
}

View File

@ -224,9 +224,24 @@ func (t *TenantAction) HTTPTsdb(md *api_model.MontiorData) ([]byte, error) {
}
//GetTenantsResources GetTenantsResources
func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) ([]*map[string]interface{}, error) {
func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) ([]map[string]interface{}, error) {
//返回全部资源
return db.GetManager().TenantServiceDao().GetCPUAndMEM(tr.Body.TenantName)
res, err := db.GetManager().TenantServiceDao().GetCPUAndMEM(tr.Body.TenantName)
if err != nil {
return nil, err
}
if len(tr.Body.TenantName) == 0 {
return res, nil
}
//TODO: 应用关闭,硬盘存储资源仍会占用
logrus.Infof("res is %v", res)
for key, per := range res {
id, ok := per["tenant_id"].(string)
if !ok {
return res, nil
}
}
}
//TenantsSum TenantsSum

View File

@ -64,7 +64,7 @@ type TenantServiceDao interface {
GetServicesByTenantID(tenantID string) ([]*model.TenantServices, error)
GetServicesAllInfoByTenantID(tenantID string) ([]*model.TenantServices, error)
DeleteServiceByServiceID(serviceID string) error
GetCPUAndMEM(tenantName []string) ([]*map[string]interface{}, error)
GetCPUAndMEM(tenantName []string) ([]map[string]interface{}, error)
GetPagedTenantService(offset, len int) ([]map[string]interface{}, error)
GetTenantServiceRes(uuid string) (map[string]interface{}, error)
}

View File

@ -23,6 +23,8 @@ import (
"os"
"strconv"
"time"
"os/exec"
"strings"
"github.com/goodrain/rainbond/pkg/db/model"
@ -135,7 +137,7 @@ func (t *TenantServicesDaoImpl) GetServiceByID(serviceID string) (*model.TenantS
//GetCPUAndMEM GetCPUAndMEM
func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]*map[string]interface{}, error) {
func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]map[string]interface{}, error) {
if len(tenantName) == 0 {
rows, err := t.DB.Raw("select sum(container_cpu) as cpu,sum(container_memory * replicas) as memory from tenant_services where service_id in (select service_id from tenant_service_status where status != 'closed' && status != 'undeploy')").Rows()
if err != nil {
@ -147,14 +149,14 @@ func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]*map[string
for rows.Next() {
rows.Scan(&cpu, &mem)
}
var rc []*map[string]interface{}
var rc []map[string]interface{}
res := make(map[string]interface{})
res["cpu"] = cpu
res["memory"] = mem
rc = append(rc, &res)
rc = append(rc, res)
return rc, nil
}
var rc []*map[string]interface{}
var rc []map[string]interface{}
for _, tenant := range tenantName {
rows, err := t.DB.Raw("select tenant_id, sum(container_cpu) as cpu, sum(container_memory * replicas) as memory from tenant_services where service_id in (select service_id from tenant_service_status where (status != 'closed' && status != 'undeploy') && service_id in (select service_id from tenant_services where domain = (?))) group by tenant_id", tenant).Rows()
if err != nil {
@ -170,8 +172,15 @@ func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]*map[string
res["cpu"] = cpu
res["memory"] = mem
res["tenant_id"] = id
logrus.Infof("res is $v", res)
rc = append(rc, &res)
dirPath := fmt.Sprintf("/grdata/tenant/%s", id)
cmd := []string{"-sh", dirPath}
f, err := exec.Command("tail", cmd...).Output()
if err != nil {
f = []byte("0 xxx")
}
st := strings.Split(string(f), " ")[0]
res["disk"] = st
rc = append(rc, res)
}
}
return rc, nil