You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

80 lines
1.3 KiB

package systemServer
import (
"time"
"ycmediakit/internal/pkg/util"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
type PayloadServer struct {
Cfg *PayloadConfig
ref *util.Channel
p Payload
}
type Payload struct {
Memory struct {
Total uint64
Free uint64
Used uint64
Usage float32
}
Cpu struct {
Usage float32
}
}
func NewPayloadServer(plCfg *PayloadConfig) *PayloadServer {
s := &PayloadServer{
Cfg: plCfg,
ref: util.NewChannel(),
}
go s.start()
return s
}
func (ps *PayloadServer) start() {
for range time.Tick(time.Second) {
if ps.running() {
ps.collect()
ps.done()
}
}
}
// running 是否正在采集数据
func (ps *PayloadServer) running() bool {
return !ps.ref.IsClosed()
}
// Wait 等待
func (ps *PayloadServer) Wait() chan interface{} {
ps.ref.Open()
return ps.ref.C
}
// done 结束
func (ps *PayloadServer) done() {
ps.ref.Close()
}
// collect 采集数据
func (ps *PayloadServer) collect() {
var p Payload
v, _ := mem.VirtualMemory()
p.Memory.Total = v.Total >> 20
p.Memory.Free = v.Available >> 20
p.Memory.Used = v.Used >> 20
p.Memory.Usage = float32(v.UsedPercent)
if cc, _ := cpu.Percent(time.Second, false); len(cc) > 0 {
p.Cpu.Usage = float32(cc[0])
}
ps.p = p
}
// GetPayload 获取采集数据
func (ps *PayloadServer) GetPayload() Payload {
return ps.p
}