init
This commit is contained in:
1
internal/collectors/battery.go
Normal file
1
internal/collectors/battery.go
Normal file
@@ -0,0 +1 @@
|
||||
package collectors
|
||||
33
internal/collectors/collector.go
Normal file
33
internal/collectors/collector.go
Normal 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
|
||||
}
|
||||
45
internal/collectors/cpu.go
Normal file
45
internal/collectors/cpu.go
Normal 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
|
||||
}
|
||||
12
internal/collectors/datetime.go
Normal file
12
internal/collectors/datetime.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user