Commit f9fd9a12 authored by Your Name's avatar Your Name

update interface param

parent d9cbd940
...@@ -15,45 +15,58 @@ import ( ...@@ -15,45 +15,58 @@ import (
// "github.com/spf13/cobra" // "github.com/spf13/cobra"
) )
type Cpus struct { // cpu{
TotalUtil int64 `json:"total_util"` // seq integer($int64)
List []Cpu `json:"list"` // model string
} // addr
// thread integer($int64)
// core integer($int64)
// }
type Cpu struct { type Cpu struct {
Seq int64 `json:"seq"`
Model string `json:"model"` Model string `json:"model"`
Thread int64 `json:"thread"` Number int64 `json:"number"`
Core int64 `json:"core"` Cores int64 `json:"cores"`
Threads int64 `json:"threads"`
Usage int64 `json:"usage"`
} }
// type Cpu struct {
// Seq int64 `json:"seq"`
// Model string `json:"model"`
// Thread int64 `json:"thread"`
// Core int64 `json:"core"`
// }
// Gpu // Gpu
func (c *ProApi) Cpus() (Cpus, error) { func (c *ProApi) Cpus() (Cpu, error) {
totalUtil, err := c.CpuTotalUtil() totalUtil, err := c.CpuTotalUtil()
if err != nil { if err != nil {
return Cpus{}, err return Cpu{}, err
} }
// inCpus := make(map[string]Cpu) // inCpus := make(map[string]Cpu)
modelname, core, thread, err := c.CpuItems() modelname, core, thread, err := c.CpuItems()
if err != nil { if err != nil {
return Cpus{}, err return Cpu{}, err
} }
res := make([]Cpu, 0, 1) // res := make([]Cpu, 0, 1)
// res = append(res, Cpu{
// Model: modelname,
// Threads: int64(thread) + 1,
// Cores: int64(core) + 1,
// })
res = append(res, Cpu{ result := Cpu{
Usage: totalUtil,
Model: modelname, Model: modelname,
Thread: int64(thread) + 1, Threads: int64(thread) + 1,
Core: int64(core) + 1, Cores: int64(core) + 1,
})
result := Cpus{
TotalUtil: totalUtil,
List: res,
} }
return result, nil return result, nil
......
...@@ -74,11 +74,12 @@ paths: ...@@ -74,11 +74,12 @@ paths:
schema: schema:
type: object type: object
properties: properties:
data: hardware:
$ref: "#/components/schemas/hw" $ref: "#/components/schemas/hw"
code: code:
type: integer type: integer
format: int64 format: int64
enum: [200, 500]
msg: msg:
type: string type: string
components: components:
...@@ -104,37 +105,41 @@ components: ...@@ -104,37 +105,41 @@ components:
hw: hw:
type: object type: object
properties: properties:
gpus: CPU:
type: array
items:
$ref: "#/components/schemas/gpu"
cpus:
type: object type: object
properties: properties:
total_util: model:
type: string
number:
type: integer
format: int64
cores:
type: integer
format: int64
threads:
type: integer
format: int64
usage:
type: integer type: integer
format: int64 format: int64
list: GPU:
type: array type: array
items: items:
$ref: "#/components/schemas/cpu" $ref: "#/components/schemas/gpu"
mem: RAM:
type: object type: object
properties: properties:
mem_total: total:
type: integer
format: int64
mem_free:
type: integer type: integer
format: int64 format: int64
mem_util: free:
type: integer type: integer
format: int64 format: int64
disk: DISK:
type: array type: array
items: items:
$ref: "#/components/schemas/filesystem" $ref: "#/components/schemas/filesystem"
networks: NET:
type: array type: array
items: items:
$ref: "#/components/schemas/network" $ref: "#/components/schemas/network"
...@@ -174,12 +179,14 @@ components: ...@@ -174,12 +179,14 @@ components:
seq: seq:
type: integer type: integer
format: int64 format: int64
uuid:
type: string
model: model:
type: string type: string
mem_util: performance:
type: integer type: integer
format: int64 format: int64
gpu_util: power_rating:
type: integer type: integer
format: int64 format: int64
mem_total: mem_total:
...@@ -188,13 +195,15 @@ components: ...@@ -188,13 +195,15 @@ components:
mem_free: mem_free:
type: integer type: integer
format: int64 format: int64
power: usage:
type: integer type: integer
format: int64 format: int64
gpu_temp: temp:
type: integer
format: int64
power_rt:
type: integer type: integer
format: int64 format: int64
cpu: cpu:
type: object type: object
properties: properties:
......
...@@ -11,15 +11,48 @@ import ( ...@@ -11,15 +11,48 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
) )
// seq:
// type: integer
// format: int64
// uuid:
// type: string
// model:
// type: string
// performance:
// type: integer
// format: int64
// power_rating:
// type: integer
// format: int64
// mem_total:
// type: integer
// format: int64
// mem_free:
// type: integer
// format: int64
// usage:
// type: integer
// format: int64
// temp:
// type: integer
// format: int64
// power_rt:
// type: integer
// format: int64
type Gpu struct { type Gpu struct {
Seq int `json:"seq"` Seq int `json:"seq"`
UUIDStr string `json:"uuid"`
Model string `json:"model"` Model string `json:"model"`
MemUtil int64 `json:"mem_util"` Performance int64 `json:"performance"`
GpuUtil int64 `json:"gpu_util"` PowerRating int64 `json:"power_rating"`
MemTotal int64 `json:"mem_total"` MemTotal int64 `json:"mem_total"`
MemFree int64 `json:"mem_free"` MemFree int64 `json:"mem_free"`
Power int64 `json:"power"` //performance
GpuTemp int64 `json:"gpu_temp"` GpuUtil int64 `json:"usage"`
GpuTemp int64 `json:"temp"`
// MemUtil int64 `json:"mem_util"`
Power int64 `json:"power_rt"`
} }
// Gpu // Gpu
...@@ -33,13 +66,13 @@ func (c *ProApi) Gpus() ([]Gpu, error) { ...@@ -33,13 +66,13 @@ func (c *ProApi) Gpus() ([]Gpu, error) {
return nil, err return nil, err
} }
gpuMemUtil, err := c.GpuMemUtil(gpuUtil) // gpuMemUtil, err := c.GpuMemUtil(gpuUtil)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
powerUsage, err := c.GpuPowerUsage(gpuMemUtil) powerUsage, err := c.GpuPowerUsage(gpuUtil)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -63,7 +96,8 @@ func (c *ProApi) Gpus() ([]Gpu, error) { ...@@ -63,7 +96,8 @@ func (c *ProApi) Gpus() ([]Gpu, error) {
res := make([]Gpu, 0, 8) res := make([]Gpu, 0, 8)
for _, gpu := range gpuMemFree { for uuidStr, gpu := range gpuMemFree {
gpu.UUIDStr = uuidStr
res = append(res, gpu) res = append(res, gpu)
} }
...@@ -140,67 +174,67 @@ func (c *ProApi) GpuUtil(inGpus map[string]Gpu) (map[string]Gpu, error) { ...@@ -140,67 +174,67 @@ func (c *ProApi) GpuUtil(inGpus map[string]Gpu) (map[string]Gpu, error) {
return inGpus, nil return inGpus, nil
} }
func (c *ProApi) GpuMemUtil(inGpus map[string]Gpu) (map[string]Gpu, error) { // func (c *ProApi) GpuMemUtil(inGpus map[string]Gpu) (map[string]Gpu, error) {
// outGpus := make(map[string]Gpu, 8) // // outGpus := make(map[string]Gpu, 8)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) // ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() // defer cancel()
gpuResult, gpuWarnings, err := c.API.Query(ctx, "DCGM_FI_DEV_MEM_COPY_UTIL", time.Now(), v1.WithTimeout(5*time.Second)) // gpuResult, gpuWarnings, err := c.API.Query(ctx, "DCGM_FI_DEV_MEM_COPY_UTIL", time.Now(), v1.WithTimeout(5*time.Second))
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
if len(gpuWarnings) > 0 { // if len(gpuWarnings) > 0 {
fmt.Printf("Warnings: %v\n", gpuWarnings) // fmt.Printf("Warnings: %v\n", gpuWarnings)
} // }
switch { // switch {
case gpuResult.Type() == model.ValVector: // case gpuResult.Type() == model.ValVector:
vectorVal := gpuResult.(model.Vector) // vectorVal := gpuResult.(model.Vector)
for _, elem := range vectorVal { // for _, elem := range vectorVal {
uuidStr := "" // uuidStr := ""
if uuid, ok := elem.Metric["UUID"]; ok { // if uuid, ok := elem.Metric["UUID"]; ok {
uuidStr = string(uuid) // uuidStr = string(uuid)
} else { // } else {
continue // continue
} // }
gpu := Gpu{} // gpu := Gpu{}
if oldV, ok := inGpus[uuidStr]; ok { // if oldV, ok := inGpus[uuidStr]; ok {
gpu = oldV // gpu = oldV
} else { // } else {
if idxAsStr, ok := elem.Metric["gpu"]; ok { // if idxAsStr, ok := elem.Metric["gpu"]; ok {
seq, err := strconv.Atoi(string(idxAsStr)) // seq, err := strconv.Atoi(string(idxAsStr))
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
gpu.Seq = seq // gpu.Seq = seq
} else { // } else {
continue // continue
} // }
if modelName, ok := elem.Metric["modelName"]; ok { // if modelName, ok := elem.Metric["modelName"]; ok {
gpu.Model = string(modelName) // gpu.Model = string(modelName)
} else { // } else {
continue // continue
} // }
} // }
gpu.MemUtil = int64(elem.Value) // gpu.MemUtil = int64(elem.Value)
inGpus[uuidStr] = gpu // inGpus[uuidStr] = gpu
} // }
} // }
return inGpus, nil // return inGpus, nil
} // }
func (c *ProApi) GpuPowerUsage(inGpus map[string]Gpu) (map[string]Gpu, error) { func (c *ProApi) GpuPowerUsage(inGpus map[string]Gpu) (map[string]Gpu, error) {
......
...@@ -48,7 +48,7 @@ func main() { ...@@ -48,7 +48,7 @@ func main() {
if err != nil { if err != nil {
fmt.Println("gpu error", err.Error()) fmt.Println("gpu error", err.Error())
res.Msg = err.Error() res.Msg = err.Error()
res.Code = 2 res.Code = 500
} }
res.Data.Gpus = gpus res.Data.Gpus = gpus
...@@ -58,7 +58,7 @@ func main() { ...@@ -58,7 +58,7 @@ func main() {
if err != nil { if err != nil {
fmt.Println("cpu error", err.Error()) fmt.Println("cpu error", err.Error())
res.Msg = err.Error() res.Msg = err.Error()
res.Code = 2 res.Code = 500
} }
res.Data.Cpus = cpus res.Data.Cpus = cpus
...@@ -68,7 +68,7 @@ func main() { ...@@ -68,7 +68,7 @@ func main() {
if err != nil { if err != nil {
fmt.Println("network error", err.Error()) fmt.Println("network error", err.Error())
res.Msg = err.Error() res.Msg = err.Error()
res.Code = 2 res.Code = 500
} }
res.Data.Networks = networks res.Data.Networks = networks
...@@ -78,7 +78,7 @@ func main() { ...@@ -78,7 +78,7 @@ func main() {
if err != nil { if err != nil {
fmt.Println("disks error", err.Error()) fmt.Println("disks error", err.Error())
res.Msg = err.Error() res.Msg = err.Error()
res.Code = 2 res.Code = 500
} }
res.Data.Disk = filesystems res.Data.Disk = filesystems
...@@ -88,12 +88,12 @@ func main() { ...@@ -88,12 +88,12 @@ func main() {
if err != nil { if err != nil {
fmt.Println("mem error", err.Error()) fmt.Println("mem error", err.Error())
res.Msg = err.Error() res.Msg = err.Error()
res.Code = 2 res.Code = 500
} }
res.Data.Mem = mem res.Data.Mem = mem
res.Code = 1 res.Code = 200
return c.JSON(&res) return c.JSON(&res)
}) })
...@@ -295,9 +295,9 @@ type NewResponse struct { ...@@ -295,9 +295,9 @@ type NewResponse struct {
} }
type Data struct { type Data struct {
Gpus []Gpu `json:"gpus"` Cpus Cpu `json:"CPU"`
Cpus Cpus `json:"cpus"` Gpus []Gpu `json:"GPU"`
Disk []FileSystem `json:"disk"` Disk []FileSystem `json:"DISK"`
Networks []Network `json:"networks"` Networks []Network `json:"NET"`
Mem Mem `json:"mem"` Mem Mem `json:"RAM"`
} }
...@@ -14,9 +14,9 @@ import ( ...@@ -14,9 +14,9 @@ import (
) )
type Mem struct { type Mem struct {
Total int64 `json:"mem_total"` Total int64 `json:"total"`
Free int64 `json:"mem_free"` Free int64 `json:"free"`
Util int64 `json:"mem_util"` // Util int64 `json:"mem_util"`
} }
func (c *ProApi) Mem() (Mem, error) { func (c *ProApi) Mem() (Mem, error) {
...@@ -32,16 +32,16 @@ func (c *ProApi) Mem() (Mem, error) { ...@@ -32,16 +32,16 @@ func (c *ProApi) Mem() (Mem, error) {
if err != nil { if err != nil {
return Mem{}, err return Mem{}, err
} }
util, err := c.MemUtil() // util, err := c.MemUtil()
if err != nil { // if err != nil {
return Mem{}, err // return Mem{}, err
} // }
return Mem{ return Mem{
Total: total, Total: total,
Free: free, Free: free,
Util: util, // Util: util,
}, nil }, nil
} }
......
...@@ -73,18 +73,18 @@ func (c *ProApi) Networks() ([]Network, error) { ...@@ -73,18 +73,18 @@ func (c *ProApi) Networks() ([]Network, error) {
res = append(res, n) res = append(res, n)
} }
winRes, err := GetNics("ipconfig") // winRes, err := GetNics("ipconfig")
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
for _, v := range winRes { // for _, v := range winRes {
res = append(res, Network{ // res = append(res, Network{
Device: v.Device, // Device: v.Device,
Mac: v.MAC, // Mac: v.MAC,
}) // })
} // }
// 定义排序函数 // 定义排序函数
sortByDevice := func(i, j int) bool { sortByDevice := func(i, j int) bool {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment