123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package engine
- import (
- "encoding/json"
- "sync"
- "time"
- "github.com/shirou/gopsutil/v3/cpu"
- "github.com/shirou/gopsutil/v3/disk"
- "github.com/shirou/gopsutil/v3/mem"
- "github.com/shirou/gopsutil/v3/net"
- "m7s.live/engine/v4/util"
- )
- var (
- summary SummaryUtil
- lastSummary Summary
- children util.Map[string, *Summary]
- collectLock sync.RWMutex
- )
- // ServerSummary 系统摘要定义
- type Summary struct {
- Address string
- Memory struct {
- Total uint64
- Free uint64
- Used uint64
- Usage float64
- }
- CPUUsage float64
- HardDisk struct {
- Total uint64
- Free uint64
- Used uint64
- Usage float64
- }
- NetWork []NetWorkInfo
- Streams []StreamSummay
- ts time.Time //上次更新时间
- }
- // NetWorkInfo 网速信息
- type NetWorkInfo struct {
- Name string
- Receive uint64
- Sent uint64
- ReceiveSpeed uint64
- SentSpeed uint64
- }
- type SummaryUtil Summary
- // Report 上报数据
- func (s *Summary) Report(slave *Summary) {
- children.Set(slave.Address, slave)
- }
- func (s *SummaryUtil) MarshalJSON() ([]byte, error) {
- return json.Marshal(s.collect())
- }
- func (s *SummaryUtil) MarshalYAML() (any, error) {
- return s.collect(), nil
- }
- func (s *SummaryUtil) collect() *Summary {
- if collectLock.TryLock() {
- defer collectLock.Unlock()
- } else {
- collectLock.RLock()
- defer collectLock.RUnlock()
- return &lastSummary
- }
- dur := time.Since(s.ts)
- if dur < time.Second {
- return &lastSummary
- }
- s.ts = time.Now()
- v, _ := mem.VirtualMemory()
- d, _ := disk.Usage("/")
- nv, _ := net.IOCounters(true)
- s.Memory.Total = v.Total >> 20
- s.Memory.Free = v.Available >> 20
- s.Memory.Used = v.Used >> 20
- s.Memory.Usage = v.UsedPercent
- if cc, _ := cpu.Percent(time.Second, false); len(cc) > 0 {
- s.CPUUsage = cc[0]
- }
- s.HardDisk.Free = d.Free >> 30
- s.HardDisk.Total = d.Total >> 30
- s.HardDisk.Used = d.Used >> 30
- s.HardDisk.Usage = d.UsedPercent
- netWorks := []NetWorkInfo{}
- for i, n := range nv {
- info := NetWorkInfo{
- Name: n.Name,
- Receive: n.BytesRecv,
- Sent: n.BytesSent,
- }
- if len(lastSummary.NetWork) > i {
- info.ReceiveSpeed = (n.BytesRecv - lastSummary.NetWork[i].Receive) / uint64(dur.Seconds())
- info.SentSpeed = (n.BytesSent - lastSummary.NetWork[i].Sent) / uint64(dur.Seconds())
- }
- netWorks = append(netWorks, info)
- }
- s.NetWork = netWorks
- s.Streams = util.MapList(&Streams, func(name string, ss *Stream) StreamSummay {
- return ss.Summary()
- })
- lastSummary = Summary(*s)
- return &lastSummary
- }
|