Skip to content

Commit

Permalink
fix: fix wrong load and cpu display
Browse files Browse the repository at this point in the history
fix #3
  • Loading branch information
cokemine committed Oct 15, 2021
1 parent c0023f0 commit 820621d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
35 changes: 18 additions & 17 deletions cmd/client/client.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/cokemine/ServerStatus-goclient/pkg/status"
"log"
"net"
Expand All @@ -23,21 +24,21 @@ var (
)

type serverStatus struct {
Uptime uint64 `json:"uptime"`
Load float64 `json:"load"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsed uint64 `json:"memory_used"`
SwapTotal uint64 `json:"swap_total"`
SwapUsed uint64 `json:"swap_used"`
HddTotal uint64 `json:"hdd_total"`
HddUsed uint64 `json:"hdd_used"`
CPU float64 `json:"cpu"`
NetworkTx uint64 `json:"network_tx"`
NetworkRx uint64 `json:"network_rx"`
NetworkIn uint64 `json:"network_in"`
NetworkOut uint64 `json:"network_out"`
Online4 bool `json:"online4"`
Online6 bool `json:"online6"`
Uptime uint64 `json:"uptime"`
Load json.Number `json:"load"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsed uint64 `json:"memory_used"`
SwapTotal uint64 `json:"swap_total"`
SwapUsed uint64 `json:"swap_used"`
HddTotal uint64 `json:"hdd_total"`
HddUsed uint64 `json:"hdd_used"`
CPU json.Number `json:"cpu"`
NetworkTx uint64 `json:"network_tx"`
NetworkRx uint64 `json:"network_rx"`
NetworkIn uint64 `json:"network_in"`
NetworkOut uint64 `json:"network_out"`
Online4 bool `json:"online4"`
Online6 bool `json:"online6"`
}

func connect() {
Expand Down Expand Up @@ -101,8 +102,8 @@ func connect() {
hddTotal, hddUsed := status.Disk(INTERVAL)
uptime := status.Uptime()
load := status.Load()
item.CPU = CPU
item.Load = load
item.CPU = json.Number(fmt.Sprintf("%.1f", CPU))
item.Load = json.Number(fmt.Sprintf("%.2f", load))
item.Uptime = uptime
item.MemoryTotal = memoryTotal
item.MemoryUsed = memoryUsed
Expand Down
3 changes: 2 additions & 1 deletion pkg/status/status.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
psutilNet "github.com/shirou/gopsutil/v3/net"
"math"
"net"
"os/exec"
"strconv"
Expand Down Expand Up @@ -69,7 +70,7 @@ func Disk(INTERVAL *float64) (uint64, uint64) {

func Cpu(INTERVAL *float64) float64 {
cpuInfo, _ := cpu.Percent(time.Duration(*INTERVAL)*time.Second, true)
return cpuInfo[0]
return math.Round(cpuInfo[0]*10) / 10
}

func Network(checkIP int) bool {
Expand Down

0 comments on commit 820621d

Please sign in to comment.