修改项目结构
This commit is contained in:
60
cmii-uav-watchdog-agent/host_info/mem_service.go
Normal file
60
cmii-uav-watchdog-agent/host_info/mem_service.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewMemoryInfo creates a new MemoryInfo instance with default values.
|
||||
func NewMemoryInfo() models.MemoryInfo {
|
||||
return models.MemoryInfo{
|
||||
Total: 0,
|
||||
Free: 0,
|
||||
Available: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// GetMemoryInfo reads memory information from /proc/meminfo.
|
||||
func GetMemoryInfo() models.MemoryInfo {
|
||||
memInfo := NewMemoryInfo()
|
||||
data, err := os.ReadFile("/proc/meminfo")
|
||||
if err != nil {
|
||||
fmt.Println("Error reading /proc/meminfo:", err)
|
||||
return memInfo
|
||||
}
|
||||
|
||||
lines := strings.Split(string(data), "\n")
|
||||
for _, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
key := fields[0]
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing value for %s: %v\n", key, err)
|
||||
continue
|
||||
}
|
||||
|
||||
switch key {
|
||||
case "MemTotal:":
|
||||
memInfo.Total = value
|
||||
case "MemFree:":
|
||||
memInfo.Free = value
|
||||
case "MemAvailable:":
|
||||
memInfo.Available = value
|
||||
}
|
||||
}
|
||||
|
||||
return memInfo
|
||||
}
|
||||
|
||||
/*
|
||||
Total Memory: 16384228 kB
|
||||
Free Memory: 1234567 kB
|
||||
Available Memory: 2345678 kB
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user