Commit 2fb5a659 authored by Your Name's avatar Your Name

add interface

parent 2f0cc906
......@@ -9,14 +9,208 @@ package main
//https://github.com/ricochet2200/go-disk-usage.git
import (
"context"
"fmt"
"time"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/ricochet2200/go-disk-usage/du"
)
//100 - ((node_filesystem_free_bytes * 100) / node_filesystem_size_bytes)
func (c *ProApi) DiskUtil() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := ` (100 - ((node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes))`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
unique := make(map[string]bool)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
r := DeviceInfo{}
if modelName, ok := elem.Metric["device"]; ok {
if _, subOk := unique[string(modelName)]; subOk {
continue
} else {
unique[string(modelName)] = true
r.Model = string(modelName)
}
} else {
continue
}
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "DiskUtil"
res = append(res, r)
}
}
return res, nil
}
func (c *ProApi) DiskTotalSize() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `node_filesystem_size_bytes/1024/1024/1024`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
unique := make(map[string]bool)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
r := DeviceInfo{}
if modelName, ok := elem.Metric["device"]; ok {
if _, subOk := unique[string(modelName)]; subOk {
continue
} else {
unique[string(modelName)] = true
r.Model = string(modelName)
}
} else {
continue
}
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "DiskTotalSize"
res = append(res, r)
}
}
return res, nil
}
//node_filesystem_free_bytes
func (c *ProApi) DiskFreeSize() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `node_filesystem_free_bytes/1024/1024/1024`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
unique := make(map[string]bool)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
r := DeviceInfo{}
if modelName, ok := elem.Metric["device"]; ok {
if _, subOk := unique[string(modelName)]; subOk {
continue
} else {
unique[string(modelName)] = true
r.Model = string(modelName)
}
} else {
continue
}
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "DiskFreeSize"
res = append(res, r)
}
}
return res, nil
}
// showBlock show block storage information for the host system.
func getBlock() ([]DeviceInfo, error) {
......
package main
import "testing"
func TestDiskTotalSize(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.DiskTotalSize()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
func TestDiskFreeSize(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.DiskFreeSize()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
//DiskUtils()
func TestDiskUtil(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.DiskUtil()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
package main
import "testing"
func TestCli(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
_ = cli
}
package main
import (
"context"
"fmt"
"syscall"
"time"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
// "github.com/pkg/errors"
// "github.com/spf13/cobra"
)
func (c *ProApi) CpuUtil() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, warnings, err := c.API.Query(ctx, `(1 - sum(rate(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(rate(node_cpu_seconds_total[1m])) by (instance) ) * 100`, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
for k, v := range elem.Metric {
fmt.Println("k", k, "v", v)
}
r := DeviceInfo{}
// if modelName, ok := elem.Metric["modelName"]; ok {
// r.Model = string(modelName)
// } else {
// continue
// }
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = string(`(1 - sum(rate(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(rate(node_cpu_seconds_total[1m])) by (instance) ) * 100`)
//}
r.Power = uint64(elem.Value)
r.Type = "CpuUtil"
res = append(res, r)
}
}
return res, nil
}
// cpuCmd represents the install command
// var cpuCmd = &cobra.Command{
// Use: "cpu",
......@@ -44,7 +98,6 @@ func getCPU() ([]DeviceInfo, error) {
func getCPUUsage() ([]DeviceInfo, error) {
res := make([]DeviceInfo, 0)
var rusage syscall.Rusage
......@@ -53,7 +106,7 @@ func getCPUUsage() ([]DeviceInfo, error) {
if err := syscall.Getrusage(pid, &rusage); err != nil {
fmt.Println("Getrusage",err.Error())
fmt.Println("Getrusage", err.Error())
return nil, err
}
......
package main
import "testing"
func TestCpuUtil(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.CpuUtil()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
{
"swagger": "2.0",
"info": {
"title": "GPU+Model service",
"version": "1.0.0"
},
"paths": {
"/hw": {
"post": {
"summary": "get host hardware info and usage",
"responses": {
"200": {
"description": "success",
"schema": {
"$ref": "#/definitions/hw"
}
}
}
}
},
"/callback": {
"post": {}
}
},
"definitions": {
"hw": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int64"
},
"msg": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
}
}
}
},
"gpu": {
"type": "object",
"properties": {
"seq": {
"type": "integer",
"format": "int64"
},
"model": {
"type": "string"
},
"mem_util": {
"type": "integer",
"format": "int64"
},
"total_mem": {
"type": "integer",
"format": "int64"
},
"power": {
"type": "integer",
"format": "int64"
},
"gpu_tmp": {
"type": "integer",
"format": "int64"
},
}
}
"cpu": {
"type": "object",
"properties": {
"seq": {
"type": "integer",
"format": "int64"
},
"model": {
"type": "string"
},
"mem_util": {
"type": "integer",
"format": "int64"
},
"total_mem": {
"type": "integer",
"format": "int64"
},
"power": {
"type": "integer",
"format": "int64"
},
"gpu_tmp": {
"type": "integer",
"format": "int64"
},
}
}
"mem": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"format": "int64"
},
"used": {
"type": "string"
},
"free": {
"type": "integer",
"format": "int64"
},
}
}
"disk": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"format": "int64"
},
"mount": {
"type": "string"
},
"used": {
"type": "integer",
"format": "int64"
},
}
}
"network": {
"type": "object",
"properties": {
"seed": {
"type": "integer",
"format": "int64"
},
"send": {
"type": "integer",
"format": "int64"
},
"receive": {
"type": "integer",
"format": "int64"
},
}
}
"Error": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
openapi: 3.0.0
servers:
- url: http://124.193.167.71:5000/
description: Default server
info:
description: |
This is a sample sniper wallet server.
You can find out more http://124.193.167.71:8001/docs/polygon/Uniswap
## Introduction
This API is documented in **OpenAPI format** and is based on
version: 0.0.1
title: Swagger Uniswap Wallet YAML
termsOfService: "http://swagger.io/terms/"
contact:
name: API Support
email: apiteam@swagger.io
url: https://github.com/Redocly/redoc
x-logo:
url: "https://redocly.github.io/redoc/petstore-logo.png"
altText: Petstore logo
license:
name: Apache 2.0
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
externalDocs:
description: Find out how to create Github repo for your OpenAPI spec.
url: "https://github.com/Rebilly/generator-openapi-repo"
tags:
- name: MonitorHardware
description: Everything about host hardware
# - name: User
# description: Everything about user
# - name: store
# description: Access to Petstore orders
# x-displayName: Petstore Orders
# - name: user
# description: Operations about user
# x-displayName: Users
# - name: pet_model
# x-displayName: The Pet Model
# description: |
# <SchemaDefinition schemaRef="#/components/schemas/Pet" />
# - name: store_model
# x-displayName: The Order Model
# description: |
# <SchemaDefinition schemaRef="#/components/schemas/Order" exampleRef="#/components/examples/Order" showReadOnly={true} showWriteOnly={true} />
x-tagGroups:
- name: General
tags:
- pet
- store
- name: User Management
tags:
- user
- name: Models
tags:
- pet_model
- store_model
paths:
/hd:
get:
summary: get host hardware info and usage
tags:
- MonitorHardware
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: object
properties:
data:
$ref: "#/components/schemas/hw"
code:
type: integer
format: int64
msg:
type: string
components:
securitySchemes:
api_key:
description: |
For this sample, you can use the api key `special-key` to test the authorization filters.
in: header
name: api_key
type: apiKey
petstore_auth:
description: |
Get access to data while protecting your account credentials.
OAuth2 is also a safer and more secure way to give you access.
flows:
implicit:
authorizationUrl: http://petstore.swagger.io/api/oauth/dialog
scopes:
read:pets: read your pets
write:pets: modify pets in your account
type: oauth2
schemas:
hw:
type: object
properties:
gpus:
type: array
items:
$ref: "#/components/schemas/gpu"
cpus:
type: object
properties:
total_util:
type: integer
format: int64
list:
type: array
items:
$ref: "#/components/schemas/cpu"
mem:
type: object
properties:
mem_total:
type: integer
format: int64
mem_free:
type: integer
format: int64
mem_used:
type: integer
format: int64
mem_util:
type: integer
format: int64
disk:
type: array
items:
$ref: "#/components/schemas/filesystem"
networks:
type: array
items:
$ref: "#/components/schemas/network"
network:
type: object
properties:
speed:
type: integer
format: int64
send:
type: integer
format: int64
receive:
type: integer
format: int64
filesystem:
type: object
properties:
device:
type: string
mount_points:
type: array
items:
type: string
free_bytes:
type: integer
format: int64
size_bytes:
type: integer
format: int64
gpu:
type: object
properties:
seq:
type: integer
format: int64
model:
type: string
mem_util:
type: integer
format: int64
mem_total:
type: integer
format: int64
power:
type: integer
format: int64
gpu_tmp:
type: integer
format: int64
cpu:
type: object
properties:
seq:
type: integer
format: int64
model:
description: addr
type: string
thread:
type: integer
format: int64
physical:
type: integer
format: int64
......@@ -30,7 +30,7 @@ import (
func ExampleAPI_query() {
client, err := api.NewClient(api.Config{
Address: "http://demo.robustperception.io:9090",
Address: "http://192.168.1.21:9090",
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
......
......@@ -7,24 +7,39 @@ toolchain go1.22.1
require github.com/jaypipes/ghw v0.12.0
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/NVIDIA/go-nvml v0.12.0-3 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gofiber/fiber/v3 v3.0.0-20240313181542-df1f877cc0be // indirect
github.com/gofiber/utils/v2 v2.0.0-beta.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/gofiber/fiber v1.14.6 // indirect
github.com/gofiber/fiber/v2 v2.52.0 // indirect
github.com/gofiber/fiber/v3 v3.0.0-beta.2 // indirect
github.com/gofiber/swagger v1.0.0 // indirect
github.com/gofiber/utils v0.0.10 // indirect
github.com/gofiber/utils/v2 v2.0.0-beta.4 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/schema v1.1.0 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
......@@ -35,14 +50,18 @@ require (
github.com/prometheus/common v0.51.1 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
......
This diff is collapsed.
This diff is collapsed.
package main
import "testing"
func TestGpuUtil(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.GpuUtil()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
func TestGpuMemUtil(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.GpuMemUtil()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
//GpuInfo()
func TestGpuInfo(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.GpuInfo()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
This diff is collapsed.
package main
import (
"context"
"fmt"
"time"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/shopspring/decimal"
)
func (c *ProApi) MemUtil() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `(1- (node_memory_Buffers_bytes + node_memory_Cached_bytes + node_memory_MemFree_bytes) / node_memory_MemTotal_bytes) * 100`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
for k, v := range elem.Metric {
fmt.Println("k", k, "v", v)
}
r := DeviceInfo{}
// if modelName, ok := elem.Metric["modelName"]; ok {
// r.Model = string(modelName)
// } else {
// continue
// }
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "MemUtil"
res = append(res, r)
}
}
return res, nil
}
func (c *ProApi) MemInfo() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `node_memory_MemTotal_bytes/1024/1024/1024`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
for k, v := range elem.Metric {
fmt.Println("k", k, "v", v)
}
r := DeviceInfo{}
// if modelName, ok := elem.Metric["modelName"]; ok {
// r.Model = string(modelName)
// } else {
// continue
// }
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "MemInfo"
res = append(res, r)
}
}
return res, nil
}
// showMemory show memory information for the host system.
func getMemory() ([]DeviceInfo, error) {
mem, err := ghw.Memory()
......
package main
import "testing"
func TestMemUtil(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.MemUtil()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
//MemInfo
func TestMemInfo(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.MemInfo()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
package main
import (
"context"
"fmt"
"time"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
//node_network_speed_bytes/1024/1024/1024
func (c *ProApi) NetworkSpeed() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `rate(node_network_speed_bytes[10s])`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
r := DeviceInfo{}
if modelName, ok := elem.Metric["device"]; ok {
r.Model = string(modelName)
} else {
continue
}
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "NetworkSpeed"
res = append(res, r)
}
}
return res, nil
}
func (c *ProApi) NetworkReceive() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `sum by(instance) (irate(node_network_receive_bytes_total{device!~"bond.*?|lo"}[5m])) `
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
r := DeviceInfo{}
// if modelName, ok := elem.Metric["modelName"]; ok {
// r.Model = string(modelName)
// } else {
// continue
// }
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "NetworkReceive"
res = append(res, r)
}
}
return res, nil
}
//sum by(instance) (irate(node_network_receive_bytes_total{device!~"bond.*?|lo"}[5m]))
//sum by(instance) (irate(node_network_transmit_bytes{device!~"bond.*?|lo"}[5m]))
func (c *ProApi) NetworkTransmit() ([]DeviceInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `sum by(instance) (irate(node_network_transmit_bytes_total{device!~"bond.*?|lo"}[5m]))`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make([]DeviceInfo, 0, 8)
switch {
case result.Type() == model.ValScalar:
//scalarVal := result.(*model.Scalar)
// handle scalar stuff
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
for k, v := range elem.Metric {
fmt.Println("k", k, "v", v)
}
r := DeviceInfo{}
// if modelName, ok := elem.Metric["modelName"]; ok {
// r.Model = string(modelName)
// } else {
// continue
// }
//if name, ok := elem.Metric["__name__"]; ok {
r.Param = queryStr
//}
r.Power = uint64(elem.Value)
r.Type = "NetworkTransmit"
res = append(res, r)
}
}
return res, nil
}
package main
import "testing"
func TestNetworkSpeed(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.NetworkSpeed()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
func TestNetworkReceive(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.NetworkReceive()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
//NetworkTransmit
func TestNetworkTransmit(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090")
if err != nil {
t.Fatal(err)
}
gpus, err := cli.NetworkTransmit()
for k, v := range gpus {
t.Log("k", k, "v", v)
}
}
package main
import (
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
type ProApi struct {
v1.API
}
func NewProCli(addr string) (*ProApi, error) {
client, err := api.NewClient(api.Config{
//Address: "http://192.168.1.21:9090",
Address: addr,
})
if err != nil {
return nil, err
}
v1api := v1.NewAPI(client)
res := ProApi{
v1api,
}
return &res, nil
}
package main
type DeviceInfo struct {
Type string
Model string
Param string
Power uint64
Type string `json:"type"`
Model string `json:"model"`
Param string `json:"param"`
Power uint64 `json:"power"`
}
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