Init
This commit is contained in:
commit
e77f611d81
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
lin_temp_mon.csv
|
79
lin_temp_mon.go
Normal file
79
lin_temp_mon.go
Normal file
@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func readTemp() string {
|
||||
temperatureFilePath := "/sys/class/hwmon/hwmon6/temp1_input"
|
||||
f, err := os.ReadFile(temperatureFilePath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
//defer f.Close()
|
||||
tempFloat, err := strconv.ParseFloat(strings.Fields(string(f))[0], 32)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
tempFloat = tempFloat / 1000
|
||||
|
||||
return string(fmt.Sprintf("%f", tempFloat))
|
||||
}
|
||||
|
||||
func writeCsv(temp string, load string) {
|
||||
// Input: temp, load
|
||||
// Result: write data to CSV file
|
||||
t := time.Now()
|
||||
timestamp := t.Unix()
|
||||
targetFile := "lin_temp_mon.csv"
|
||||
fmt.Println(temp, load, timestamp)
|
||||
|
||||
targetArr := []string{strconv.FormatInt(timestamp, 10), load, temp}
|
||||
|
||||
f, err := os.OpenFile(targetFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatal("Error: ", err)
|
||||
return
|
||||
}
|
||||
w := csv.NewWriter(f)
|
||||
err = w.Write(targetArr)
|
||||
if err != nil {
|
||||
log.Fatal("Error: ", err)
|
||||
return
|
||||
}
|
||||
w.Flush()
|
||||
f.Close()
|
||||
}
|
||||
|
||||
func getPerCPU() string {
|
||||
loadavgFile := "/proc/loadavg"
|
||||
nproc := float64(runtime.NumCPU())
|
||||
|
||||
loadAvgRaw, err := os.ReadFile(loadavgFile)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
//defer loadAvgRaw.Close()
|
||||
|
||||
loadAvgCur, err := strconv.ParseFloat(strings.Fields(string(loadAvgRaw))[0], 32)
|
||||
|
||||
loadPerCpu := loadAvgCur / nproc
|
||||
|
||||
return string(fmt.Sprintf("%f", loadPerCpu))
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Timestamp, LA, Temp
|
||||
writeCsv(readTemp(), getPerCPU())
|
||||
}
|
Loading…
Reference in New Issue
Block a user