2024-08-30 20:33:04 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package wshremote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v4/mem"
|
2024-09-12 03:03:55 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshutil"
|
2024-08-30 20:33:04 +02:00
|
|
|
)
|
|
|
|
|
2024-10-18 00:19:13 +02:00
|
|
|
const BYTES_PER_GB = 1073741824
|
|
|
|
|
2024-08-30 20:33:04 +02:00
|
|
|
func getCpuData(values map[string]float64) {
|
|
|
|
percentArr, err := cpu.Percent(0, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(percentArr) > 0 {
|
|
|
|
values[wshrpc.TimeSeries_Cpu] = percentArr[0]
|
|
|
|
}
|
|
|
|
percentArr, err = cpu.Percent(0, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for idx, percent := range percentArr {
|
|
|
|
values[wshrpc.TimeSeries_Cpu+":"+strconv.Itoa(idx)] = percent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMemData(values map[string]float64) {
|
|
|
|
memData, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-10-18 00:19:13 +02:00
|
|
|
values["mem:total"] = float64(memData.Total) / BYTES_PER_GB
|
|
|
|
values["mem:available"] = float64(memData.Available) / BYTES_PER_GB
|
|
|
|
values["mem:used"] = float64(memData.Used) / BYTES_PER_GB
|
|
|
|
values["mem:free"] = float64(memData.Free) / BYTES_PER_GB
|
2024-08-30 20:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateSingleServerData(client *wshutil.WshRpc, connName string) {
|
|
|
|
now := time.Now()
|
|
|
|
values := make(map[string]float64)
|
|
|
|
getCpuData(values)
|
|
|
|
getMemData(values)
|
|
|
|
tsData := wshrpc.TimeSeriesData{Ts: now.UnixMilli(), Values: values}
|
2024-09-12 03:03:55 +02:00
|
|
|
event := wps.WaveEvent{
|
|
|
|
Event: wps.Event_SysInfo,
|
2024-08-30 20:33:04 +02:00
|
|
|
Scopes: []string{connName},
|
|
|
|
Data: tsData,
|
|
|
|
Persist: 1024,
|
|
|
|
}
|
|
|
|
wshclient.EventPublishCommand(client, event, &wshrpc.RpcOpts{NoResponse: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunSysInfoLoop(client *wshutil.WshRpc, connName string) {
|
|
|
|
defer func() {
|
|
|
|
log.Printf("sysinfo loop ended conn:%s\n", connName)
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
generateSingleServerData(client, connName)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|