50 lines
1 KiB
Go
50 lines
1 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/mackerelio/go-osstat/cpu"
|
|
"github.com/mackerelio/go-osstat/memory"
|
|
|
|
"git.lastassault.de/speatzle/morffix/rpc"
|
|
"git.lastassault.de/speatzle/morffix/types"
|
|
)
|
|
|
|
func init() {
|
|
rpcServer.RegisterMethod("status", status)
|
|
}
|
|
|
|
var cpuBefore *cpu.Stats
|
|
var cpuUsage uint64
|
|
var cpuCount uint64
|
|
|
|
func calcUsage() {
|
|
for {
|
|
cStats, _ := cpu.Get()
|
|
if cStats != nil {
|
|
cpuUsage = cStats.Total
|
|
cpuCount = uint64(cStats.CPUCount)
|
|
|
|
if cpuBefore != nil {
|
|
total := float64(cStats.Total - cpuBefore.Total)
|
|
cpuUsage = uint64(float64((cStats.User-cpuBefore.User)+(cStats.System-cpuBefore.System)) / total * 100)
|
|
}
|
|
cpuBefore = cStats
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|
|
|
|
func status(ctx context.Context, req rpc.Request) (any, error) {
|
|
s := types.WorkerStatus{}
|
|
s.CPUUsage = cpuUsage
|
|
s.CPUCount = cpuCount
|
|
mStats, _ := memory.Get()
|
|
if mStats != nil {
|
|
s.MemoryUsage = uint64(float64(mStats.Used) / float64(mStats.Total) * 100)
|
|
s.MemoryTotal = mStats.Total / 1000000000
|
|
}
|
|
|
|
return s, nil
|
|
}
|