sophuwu.site > quickstat
net not work
sophuwu sophie@skisiel.com
Thu, 11 Apr 2024 13:31:36 +0200
commit

02e09c9907c37743d009fe472f701ff48350e43a

parent

30a16be75733f3315208ae3aed8d88a2c3174b6e

4 files changed, 152 insertions(+), 45 deletions(-)

jump to
M cpu.gocpu.go

@@ -8,9 +8,11 @@ "os"

"time" ) +type float float32 + type CPU struct { - Load float64 - MHz float64 + Load float + MHz float Temp int }

@@ -71,14 +73,14 @@ if Is(err) {

return } var ns numSeeker - var Ipart, Fpart, n float64 + var Ipart, Fpart, n float for _, v := range bytes.Split(b, []byte("\n")) { Ipart = 0 Fpart = 0 if bytes.HasPrefix(v, []byte("cpu MHz")) { ns.Init(v) - Ipart = float64(ns.GetNum()) - Fpart = float64(ns.GetNum()) + Ipart = float(ns.GetNum()) + Fpart = float(ns.GetNum()) for Fpart >= 1 { Fpart /= 10 }

@@ -89,7 +91,7 @@ }

c.MHz /= n } func (c *CPU) loadUsage() { - readStat := func(n *[4]float64) bool { + readStat := func(n *[4]float) bool { b := make([]byte, 100) f, err := os.Open("/proc/stat")

@@ -103,14 +105,14 @@ }

f.Close() for i, j := 6, 0; j < 4; i++ { if b[i] >= 48 && b[i] <= 57 { - n[j] = n[j]*10 + float64(b[i]) - 48 + n[j] = n[j]*10 + float(b[i]) - 48 } else if b[i] == ' ' { j++ } } return false } - var a, b [4]float64 + var a, b [4]float if readStat(&a) { return }

@@ -120,25 +122,27 @@ return

} c.Load = ((b[0] + b[1] + b[2]) - (a[0] + a[1] + a[2])) / ((b[0] + b[1] + b[2] + b[3]) - (a[0] + a[1] + a[2] + a[3])) } -func (c *CPU) update(done chan bool) { - go c.loadMHz() - go c.loadTemp() +func (c *CPU) update() { + c.loadMHz() + c.loadTemp() c.loadUsage() - done <- true } -func (c *CPU) GHz() float64 { - return c.MHz / 1000 +func (c *CPU) GHzStr() string { + return fmt.Sprintf("%.2f GHz", c.MHz/1000) +} +func (c *CPU) LoadStr() string { + return fmt.Sprintf("%.1f %c", c.Load*100, '%') } func (c *CPU) TempStr() string { if c.Temp == -100 { return "" } - return fmt.Sprintf("%d °C", c.Temp) + return fmt.Sprintf("%d C", c.Temp) } func (c *CPU) String() string { if CONFIG.Json { b, _ := json.Marshal(c) return string(b) } - return fmt.Sprintf("%.1f %c %.2f GHz %s\n", c.Load*100, '%', c.GHz(), c.TempStr()) + return fmt.Sprintf("%6.6s %8.8s %5.5s", c.LoadStr(), c.GHzStr(), c.TempStr()) }
M main.gomain.go

@@ -15,14 +15,14 @@ Json bool

Repeat bool Unit int Time time.Duration -}{false, false, 2, time.Second} +}{false, false, 0, time.Second} func Help() { fmt.Printf("Usage: %s [options]\n", os.Args[0]) fmt.Println("Options:") fmt.Println(" -j Output in JSON format") fmt.Println(" -r Repeat output") - fmt.Printf(" -[%s] Use unit\n", SI) + fmt.Printf(" -[%s] Print in unit\n", SI) fmt.Println(" -t<n> Set sampling time to n seconds") os.Exit(0) }

@@ -60,38 +60,58 @@ }

type Bytes uint64 +func (b Bytes) Unit() string { + n := float64(b) + if SI[CONFIG.Unit] == '%' { + n /= 100 + } + for i := 1; i < CONFIG.Unit; i++ { + n /= 1024 + } + f := "%.0f" + if n < 100 { + f = "%.2f" + } + return fmt.Sprintf(f+" %c", n, SI[CONFIG.Unit]) +} + func (b Bytes) String() string { - if CONFIG.Unit == 0 { - + if CONFIG.Json { + json.Marshal(b * 1024) } - return fmt.Sprint(b) + return b.Unit() } -type Net struct { - Tx Bytes - Rx Bytes -} type HWInfo struct { - CPU CPU - MEM MEM - NET Net + Time int64 `json:"UnixMilli"` + CPU CPU `json:"CPU"` + MEM MEM `json:"MEM"` } -func (this *HWInfo) update() { +func (this *HWInfo) Update() { done := make(chan bool) - go this.CPU.update(done) - go this.MEM.update(done) + go func() { + this.CPU.update() + done <- true + }() + go func() { + this.MEM.update() + done <- true + }() <-done <-done - // i.NET.update() + this.Time = time.Now().UnixMilli() } -func (i *HWInfo) String() string { +func (i HWInfo) String() string { if CONFIG.Json { b, _ := json.Marshal(i) return string(b) } - return fmt.Sprintf("%s %s %s\n", i.CPU, i.MEM, i.NET) - + s := i.MEM.String() + if SI[CONFIG.Unit] == '%' { + s = i.MEM.Percent().String() + } + return fmt.Sprintf("MEM: %s | CPU: %s", s, i.CPU.String()) } func Is(err error) bool {

@@ -99,11 +119,9 @@ return err != nil

} func main() { - var HW HWInfo - HW.update() - fmt.Println(HW) - // go getCPUTemp() - // go getMHz() - // getCPU() - // println(" Mem: ", printNWithErr(prctMem), "% USED ", printNWithErr(prctBuff), "% BUFF ", printNWithErr(prctFree), "% FREE CPU: ", printNWithErr(cpuLoad), "% ", printNWithErr(cpuTemp), "C ", printNWithErr(cpuMHz), "MHz") + var hw HWInfo + for do := true; do; do = CONFIG.Repeat { + hw.Update() + fmt.Println(hw) + } }
M mem.gomem.go

@@ -1,6 +1,8 @@

package main import ( + "encoding/json" + "fmt" "os" )

@@ -58,8 +60,7 @@ }

return nums } -func (m *MEM) update(done chan bool) { - +func (m *MEM) update() { b := make([]byte, 140) f, _ := os.Open("/proc/meminfo") _, err := f.Read(b)

@@ -73,5 +74,21 @@ m.Total = Bytes(seeker.GetNum())

m.Free = Bytes(seeker.GetNum()) m.Avail = Bytes(seeker.GetNum()) m.Buffer = Bytes(seeker.GetNum() + seeker.GetNum()) - done <- true +} +func (m *MEM) Percent() *MEM { + var m2 MEM + fac := Bytes(10000) + m2.Avail = m.Avail * fac / m.Total + m2.Free = m.Free * fac / m.Total + m2.Buffer = m.Buffer * fac / m.Total + m2.Total = m.Total * fac / m.Total + return &m2 +} + +func (m *MEM) String() string { + if CONFIG.Json { + b, _ := json.Marshal(m) + return string(b) + } + return fmt.Sprintf("%s USED %s BUFF %s FREE", Bytes(m.Total-m.Avail), m.Buffer, m.Free) }
A net.go

@@ -0,0 +1,68 @@

+package main + +import ( + "encoding/json" + "fmt" + "os" + "strings" + "time" +) + +var BitRateUnit = CONFIG.Unit | 1 + +type BitRate int64 + +func (b BitRate) String() string { + n := float(b) + for i := 0; i <= CONFIG.Unit; i++ { + n /= 1024 + } + n /= float(CONFIG.Time.Seconds()) + return fmt.Sprintf("%.2f %cB/s", n, SI[BitRateUnit]) +} + +type Net struct { + Tx BitRate + Rx BitRate +} + +func readDev() *Net { + f, err := os.OpenFile("/proc/net/dev", os.O_RDONLY, 0) + if Is(err) { + return &Net{} + } + b := make([]byte, 1024) + ln, err := f.Read(b) + f.Close() + if Is(err) { + return &Net{} + } + var ns numSeeker + var arr []uint64 + var rx, tx BitRate + for _, v := range strings.Split(string(b[0:ln]), "\n") { + arr = []uint64{} + ns.Init([]byte(v)) + arr = ns.GetNums() + if len(arr) < 16 { + continue + } + tx += BitRate(int64(arr[8])) + rx += BitRate(int64(arr[0])) + } + return &Net{tx, rx} +} +func (n *Net) update() { + n2 := readDev() + time.Sleep(CONFIG.Time) + n = readDev() + n.Tx = n.Tx - n2.Tx + n.Rx = n.Rx - n2.Rx +} +func (n *Net) String() string { + if CONFIG.Json { + b, _ := json.Marshal(n) + return string(b) + } + return fmt.Sprintf("TX: %s | RX: %s", n.Tx, n.Rx) +}