From 0d80f79469a7200fdf2e17bed3ab909f317b26c8 Mon Sep 17 00:00:00 2001 From: Perry Hargrave Date: Thu, 22 Feb 2024 00:31:15 -0800 Subject: [PATCH] /proc/loadavg basics --- README.md | 6 ++++ load/load.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ synopsys.go | 8 ++++++ 3 files changed, 92 insertions(+) create mode 100644 load/load.go diff --git a/README.md b/README.md index 39e03f8..498900e 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,9 @@ These are the parameters I'd like to show initially this will just output some rolling format. will have to think about something like a tui to properly place things for readability though + + +## References + +- [/proc/loadavg](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/4/html/reference_guide/s2-proc-loadavg) + diff --git a/load/load.go b/load/load.go new file mode 100644 index 0000000..6a2faae --- /dev/null +++ b/load/load.go @@ -0,0 +1,78 @@ +package load + +import ( + "log" + "os" + "strconv" + "strings" +) + +type Load struct { + one float64 + five float64 + fifteen float64 + proc_running int + proc_total int + lastpid int +} + +type LA_CONST int + +const ( + LA_ONEMIN LA_CONST = iota + LA_FIVEMIN + LA_FIFTEENMIN + LA_PROC_RUN // gotta remember LA_PROC_TOTAL is + // LA_PROC_TOTAL // This is actually not a sepaarte entry when parsing + LA_LASTPID +) + +func GetLoadAvg() (*Load, error) { + f, err := os.ReadFile("/proc/loadavg") + if err != nil { + log.Fatal(err) + } + loadinfo := new(Load) + sp := strings.Split(strings.TrimSuffix(string(f), "\n"), " ") + + // The running/total process entries are not space-delimited so parse those + // a little different + var i LA_CONST + for i = LA_ONEMIN; i <= LA_LASTPID+1; i++ { + switch i { + case LA_ONEMIN: + loadinfo.one, err = strconv.ParseFloat(sp[i], 64) + if err != nil { + return nil, err + } + case LA_FIVEMIN: + loadinfo.five, err = strconv.ParseFloat(sp[i], 64) + if err != nil { + return nil, err + } + case LA_FIFTEENMIN: + loadinfo.fifteen, err = strconv.ParseFloat(sp[i], 64) + if err != nil { + return nil, err + } + case LA_PROC_RUN: + // These are not space delimited but shown like X/Y in the loadavg file + procs := strings.Split(sp[i], "/") + loadinfo.proc_running, err = strconv.Atoi(procs[0]) + if err != nil { + return nil, err + } + loadinfo.proc_total, err = strconv.Atoi(procs[1]) + if err != nil { + return nil, err + } + case LA_LASTPID: + loadinfo.lastpid, err = strconv.Atoi(sp[i]) + if err != nil { + return nil, err + } + } + } + return loadinfo, nil + +} diff --git a/synopsys.go b/synopsys.go index a0be96c..73aa1a4 100644 --- a/synopsys.go +++ b/synopsys.go @@ -5,6 +5,7 @@ import ( "log" "github.com/bioe007/synopsys/cpu" + "github.com/bioe007/synopsys/load" "github.com/bioe007/synopsys/memory" ) @@ -17,4 +18,11 @@ func main() { cpuinf, err := cpu.GetCPUStats() fmt.Println("cpu struct ${##v}", cpuinf) + + load, err := load.GetLoadAvg() + if err != nil { + log.Fatal("Load average failure", err) + } + fmt.Println("Load average stuff", load) + }