Commit 68789801 authored by Ubuntu's avatar Ubuntu

add usage

parent c995df4a
......@@ -6,11 +6,15 @@
package main
//https://github.com/ricochet2200/go-disk-usage.git
import (
"fmt"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/ricochet2200/go-disk-usage/du"
)
// showBlock show block storage information for the host system.
......@@ -41,3 +45,35 @@ func getBlock() ([]DeviceInfo, error) {
return res, nil
}
func getBlockUsage() ([]DeviceInfo, error) {
//block := ghw.Block()
block, err := ghw.Block()
if err != nil {
return nil, errors.Wrap(err, "error getting block device info")
}
res := make([]DeviceInfo, 0, len(block.Disks))
usage := du.NewDiskUsage("/")
fmt.Println("Usage:", usage.Usage()*100, "%")
// for i, disk := range block.Disks {
// if disk.Model == "unknown" {
// continue
// }
e := DeviceInfo{
Type: "volumePath /",
//Model: ,
Param: "disk usage",
Power: uint64(usage.Usage() * 100),
}
res = append(res, e)
// }
return res, nil
}
......@@ -2,6 +2,8 @@ package main
import (
"fmt"
"syscall"
"time"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
......@@ -39,3 +41,61 @@ func getCPU() ([]DeviceInfo, error) {
return res, nil
}
func getCPUUsage() ([]DeviceInfo, error) {
cpu, err := ghw.CPU()
if err != nil {
return nil, errors.Wrap(err, "error getting CPU info")
}
res := make([]DeviceInfo, 0, len(cpu.Processors))
// for i, proc := range cpu.Processors {
// fmt.Printf(" %v\n", proc)
// e := DeviceInfo{
// Type: fmt.Sprintf("proc-%d", i),
// Model: proc.Model,
// Param: "proc.NumThreads",
// Power: uint64(proc.NumThreads),
// }
// res = append(res, e)
// }
var rusage syscall.Rusage
pid := syscall.Getpid()
if err := syscall.Getrusage(pid, &rusage); err != nil {
return nil, err
}
utime := rusage.Utime.Nano()
stime := rusage.Stime.Nano()
total := utime + stime
fmt.Printf("CPU usage: %.2f%%\n", float64(total)/float64(time.Second)*100)
e := DeviceInfo{
Type: "cpu",
//Model: proc.Model,
Param: "proc.NumThreads",
Power: uint64(float64(total) / float64(time.Second) * 100),
}
res = append(res, e)
return res, nil
}
// var rusage syscall.Rusage
// pid := syscall.Getpid()
// for {
// err := syscall.Getrusage(pid, &rusage)
// if err != nil {
// panic(err)
// }
// utime := rusage.Utime.Nano()
// stime := rusage.Stime.Nano()
// total := utime + stime
// fmt.Printf("CPU usage: %.2f%%\n", float64(total)/float64(time.Second)*100)
// time.Sleep(time.Second)
// }
module gpuhw
go 1.20
go 1.21
toolchain go1.22.1
require github.com/jaypipes/ghw v0.12.0
......@@ -20,6 +22,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 // 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
......
......@@ -36,6 +36,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 h1:d54EL9l+XteliUfUCGsEwwuk65dmmxX85VXF+9T6+50=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285/go.mod h1:fxIDly1xtudczrZeOOlfaUvd2OPb2qZAPuWdU2BsBTk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
......
......@@ -50,7 +50,7 @@ func main() {
//return c.SendString("Hello, World 👋!")
})
app.Get("/hw/used", func(c fiber.Ctx) error {
app.Get("/hw/usage", func(c fiber.Ctx) error {
res := make([]DeviceInfo, 0)
......@@ -62,6 +62,31 @@ func main() {
res = append(res, gpu...)
cpu, err :=getCPUUsage()
if err != nil {
c.SendString("getCPUUsage err: " + err.Error())
}
res = append(res, cpu...)
mem ,err :=getMemoryUsage()
if err != nil {
c.SendString("getMemoryUsage err: " + err.Error())
}
res = append(res, mem...)
block, err := getBlockUsage()
if err != nil {
c.SendString("getBlockUsage err: " + err.Error())
}
res = append(res, block...)
return c.JSON(res)
//res := make([]DeviceInfo, 0)
......
......@@ -5,6 +5,8 @@ import (
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
// showMemory show memory information for the host system.
......@@ -30,3 +32,33 @@ func getMemory() ([]DeviceInfo, error) {
return res, nil
}
func getMemoryUsage() ([]DeviceInfo, error) {
mem, err := ghw.Memory()
if err != nil {
return nil, errors.Wrap(err, "error getting memory info")
}
fmt.Println(mem.Area.TotalPhysicalBytes, mem.Area.TotalUsableBytes)
usable := decimal.NewFromInt(mem.Area.TotalUsableBytes)
total := decimal.NewFromInt(mem.Area.TotalPhysicalBytes)
mem.TotalUsableBytes
e := DeviceInfo{
Type: fmt.Sprintf("mem"),
//Model: disk.Model,
Param: "mem usage",
Power: uint64((1 - usable.Div(total).int64()) * 100),
}
res := make([]DeviceInfo, 0, 1)
res = append(res, e)
return res, nil
}
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