|
|
@@ -1,12 +1,22 @@
|
|
|
package system
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
"github.com/shirou/gopsutil/disk"
|
|
|
"github.com/shirou/gopsutil/mem"
|
|
|
+ "io/ioutil"
|
|
|
+ "log/slog"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+var (
|
|
|
+ logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
|
+)
|
|
|
+
|
|
|
func GetMemory() (uint64, uint64) {
|
|
|
v, _ := mem.VirtualMemory()
|
|
|
return v.Used, v.Total
|
|
|
@@ -20,4 +30,36 @@ func GetCPU() int {
|
|
|
func GetDisk() (uint64, uint64) {
|
|
|
d, _ := disk.Usage("/")
|
|
|
return d.Used, d.Free
|
|
|
+}
|
|
|
+
|
|
|
+func GetTemp() float64 {
|
|
|
+ data, err := ioutil.ReadFile("/sys/class/thermal/thermal_zone0/temp")
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error reading temperature:", err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ tempStr := strings.TrimSpace(string(data))
|
|
|
+ temp, err := strconv.Atoi(tempStr)
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error converting temperature to integer:", err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return float64(temp) / 1000.0
|
|
|
+}
|
|
|
+
|
|
|
+func HasSwap() int {
|
|
|
+ data, err := ioutil.ReadFile("/proc/swaps")
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error reading swap status:", err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ lines := strings.Split(string(data), "\n")
|
|
|
+ if len(lines) > 1 {
|
|
|
+ return 1
|
|
|
+ } else {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
}
|