agent: add system-info endpoint

This commit is contained in:
yusing
2025-02-10 12:34:08 +08:00
parent 9120bbea34
commit 73e2660e59
8 changed files with 151 additions and 18 deletions

View File

@@ -6,7 +6,6 @@ import (
"net/http"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
)
func WriteBody(w http.ResponseWriter, body []byte) {
@@ -20,26 +19,17 @@ func RespondJSON(w http.ResponseWriter, r *http.Request, data any, code ...int)
w.WriteHeader(code[0])
}
w.Header().Set("Content-Type", "application/json")
var j []byte
var err error
switch data := data.(type) {
case string:
j = []byte(fmt.Sprintf("%q", data))
_, err = w.Write([]byte(fmt.Sprintf("%q", data)))
case []byte:
j = data
case error:
j, err = json.Marshal(ansi.StripANSI(data.Error()))
_, err = w.Write(data)
default:
j, err = json.MarshalIndent(data, "", " ")
err = json.NewEncoder(w).Encode(data)
}
if err != nil {
logging.Panic().Err(err).Msg("failed to marshal json")
return false
}
_, err = w.Write(j)
if err != nil {
HandleErr(w, r, err)
return false

View File

@@ -0,0 +1,62 @@
package metrics
import (
"context"
"encoding/json"
"time"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/mem"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type (
SystemInfo struct {
CPUAverage float64
Memory *mem.VirtualMemoryStat
Disk *disk.UsageStat
}
)
func GetSystemInfo(ctx context.Context) (*SystemInfo, error) {
memoryInfo, err := mem.VirtualMemory()
if err != nil {
return nil, err
}
cpuAverage, err := cpu.PercentWithContext(ctx, time.Second, false)
if err != nil {
return nil, err
}
diskInfo, err := disk.Usage("/")
if err != nil {
return nil, err
}
return &SystemInfo{
CPUAverage: cpuAverage[0],
Memory: memoryInfo,
Disk: diskInfo,
}, nil
}
func (info *SystemInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"cpu_average": info.CPUAverage,
"memory": map[string]interface{}{
"total": strutils.FormatByteSize(info.Memory.Total),
"available": strutils.FormatByteSize(info.Memory.Available),
"used": strutils.FormatByteSize(info.Memory.Used),
"used_percent": info.Memory.UsedPercent,
"free": strutils.FormatByteSize(info.Memory.Free),
},
"disk": map[string]interface{}{
"total": strutils.FormatByteSize(info.Disk.Total),
"used": strutils.FormatByteSize(info.Disk.Used),
"used_percent": info.Disk.UsedPercent,
"free": strutils.FormatByteSize(info.Disk.Free),
"fs_type": info.Disk.Fstype,
},
})
}

View File

@@ -2,6 +2,7 @@ package strutils
import (
"fmt"
"math"
"strconv"
"strings"
"time"
@@ -65,6 +66,39 @@ func ParseBool(s string) bool {
}
}
func formatFloat(f float64) string {
f = math.Round(f*100) / 100
if f == 0 {
return "0"
}
return strconv.FormatFloat(f, 'f', -1, 64)
}
func FormatByteSize[T ~uint64](size T) string {
const (
_ = (1 << (10 * iota))
kb
mb
gb
tb
pb
)
switch {
case size < kb:
return fmt.Sprintf("%d B", size)
case size < mb:
return formatFloat(float64(size)/kb) + "KiB"
case size < gb:
return formatFloat(float64(size)/mb) + "MiB"
case size < tb:
return formatFloat(float64(size)/gb) + "GiB"
case size < pb:
return formatFloat(float64(size/gb)/kb) + "TiB" // prevent overflow
default:
return formatFloat(float64(size/tb)/kb) + "PiB" // prevent overflow
}
}
func PortString(port uint16) string {
return strconv.FormatUint(uint64(port), 10)
}