Skip to content

Commit

Permalink
/proc/loadavg basics
Browse files Browse the repository at this point in the history
  • Loading branch information
bioe007 committed Feb 22, 2024
1 parent 83ab93e commit 0d80f79
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

78 changes: 78 additions & 0 deletions load/load.go
Original file line number Diff line number Diff line change
@@ -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

}
8 changes: 8 additions & 0 deletions synopsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/bioe007/synopsys/cpu"
"github.com/bioe007/synopsys/load"
"github.com/bioe007/synopsys/memory"
)

Expand All @@ -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)

}

0 comments on commit 0d80f79

Please sign in to comment.