This commit is contained in:
parent
f818a3525b
commit
9d2519c085
2 changed files with 27 additions and 5 deletions
|
@ -2,6 +2,7 @@ package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mackerelio/go-osstat/cpu"
|
"github.com/mackerelio/go-osstat/cpu"
|
||||||
"github.com/mackerelio/go-osstat/memory"
|
"github.com/mackerelio/go-osstat/memory"
|
||||||
|
@ -14,13 +15,31 @@ func init() {
|
||||||
rpcServer.RegisterMethod("status", status)
|
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) {
|
func status(ctx context.Context, req rpc.Request) (any, error) {
|
||||||
s := types.WorkerStatus{}
|
s := types.WorkerStatus{}
|
||||||
cStats, _ := cpu.Get()
|
s.CPUUsage = cpuUsage
|
||||||
if cStats != nil {
|
s.CPUCount = cpuCount
|
||||||
s.CPUUsage = cStats.Total
|
|
||||||
s.CPUCount = uint64(cStats.CPUCount)
|
|
||||||
}
|
|
||||||
mStats, _ := memory.Get()
|
mStats, _ := memory.Get()
|
||||||
if mStats != nil {
|
if mStats != nil {
|
||||||
s.MemoryUsage = uint64(float64(mStats.Used) / float64(mStats.Total) * 100)
|
s.MemoryUsage = uint64(float64(mStats.Used) / float64(mStats.Total) * 100)
|
||||||
|
|
|
@ -60,6 +60,9 @@ func Start(_conf config.Config) {
|
||||||
exit = true
|
exit = true
|
||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go calcUsage()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if exit {
|
if exit {
|
||||||
slog.InfoContext(ctx, "Done")
|
slog.InfoContext(ctx, "Done")
|
||||||
|
|
Loading…
Add table
Reference in a new issue