added load average calculations and moved the testing from main to their own cli commands

This commit is contained in:
lin committed 2026-10-06 00:10:52 +02:00
1 parent 8c931aa3d8
commit b1d4f3565d
7 files changed
+271 -26

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
//go:build linux
package loadaverage
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func getLinuxLoadAverage() (LoadAverageInformation, error) {
loadFile, err := os.Open("/proc/loadavg")
if err != nil {
return LoadAverageInformation{}, fmt.Errorf("Could not find /proc/loadavg. Are you sure you are on linux?")
}
defer loadFile.Close()
fileReader := bufio.NewScanner(loadFile)
var one_m float64
var five_m float64
var fifteen_m float64
fileReader.Scan()
if fileReader.Err() != nil {
return LoadAverageInformation{}, fmt.Errorf("Could not read /proc/uptime.")
}
data := fileReader.Text()
/*
* fields[0] One minute load average
* fields[1] Five minute load average
* fields[2] Fifteen minute load average
* fields[3] Currently executing/total scheduled
* fields[4] Most recent process PID
*/
fields := strings.Split(data, " ")
one_m, _ = strconv.ParseFloat(fields[0], 64)
five_m, _ = strconv.ParseFloat(fields[1], 64)
fifteen_m, _ = strconv.ParseFloat(fields[2], 64)
return LoadAverageInformation{
Load1: one_m,
Load5: five_m,
Load15: fifteen_m,
}, nil
}
func GetLoadAverage() (LoadAverageInformation, error) {
return getLinuxLoadAverage()
}