This commit is contained in:
2026-02-27 21:20:08 -07:00
commit 2f35c9dd34
12 changed files with 203 additions and 0 deletions

View File

@@ -0,0 +1 @@
package collectors

View File

@@ -0,0 +1,33 @@
package collectors
type StatusInfo struct {
CPULoad1 float32
CPULoad5 float32
CPULoad15 float32
Year int
Month int
Day int
Hour int
Minute int
Second int
BatteryPercent int
BatteryStatus string
}
func Collect() (*StatusInfo, error) {
year, month, day, hour, minute, second := datetime()
cpuLoad1, cpuLoad5, cpuLoad15 := cpuLoad()
return &StatusInfo{
CPULoad1: cpuLoad1,
CPULoad5: cpuLoad5,
CPULoad15: cpuLoad15,
Year: year,
Month: int(month),
Day: day,
Hour: hour,
Minute: minute,
Second: second,
BatteryPercent: 100,
BatteryStatus: "Charging",
}, nil
}

View File

@@ -0,0 +1,45 @@
package collectors
import (
"bufio"
"log"
"os"
"strconv"
"strings"
)
func strToFloat32(input string) (float32) {
f64, err := strconv.ParseFloat(input, 32)
if err != nil {
log.Fatal(err)
}
f32 := float32(f64)
return f32
}
func cpuLoad() (load1, load5, load15 float32) {
const filePath string = "/proc/loadavg"
var firstLine string
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
firstLine = scanner.Text()
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
loadAverages := strings.Fields(firstLine)
load1 = strToFloat32(loadAverages[0])
load5 = strToFloat32(loadAverages[1])
load15 = strToFloat32(loadAverages[2])
return load1, load5, load15
}

View File

@@ -0,0 +1,12 @@
package collectors
import (
"time"
)
func datetime() (year int, month time.Month, day, hour, minute, second int){
currentTime := time.Now()
year, month, day = currentTime.Date()
hour, minute, second = currentTime.Clock()
return year, month, day, hour, minute, second
}