修改项目结构
This commit is contained in:
65
cmii-uav-watchdog-agent/host_info/cpu_service.go
Normal file
65
cmii-uav-watchdog-agent/host_info/cpu_service.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewCPUInfo 创建一个默认的 CPUInfo
|
||||
func NewCPUInfo() models.CPUInfo {
|
||||
return models.CPUInfo{
|
||||
ModelName: "unknown",
|
||||
Cores: 0,
|
||||
Architecture: "unknown",
|
||||
}
|
||||
}
|
||||
|
||||
// parseLine 解析单行 CPU 信息
|
||||
func parseLine(line string, cpuInfo *models.CPUInfo) {
|
||||
if strings.HasPrefix(line, "model name") {
|
||||
cpuInfo.ModelName = strings.TrimSpace(strings.Split(line, ":")[1])
|
||||
} else if strings.HasPrefix(line, "cpu cores") {
|
||||
if _, err := fmt.Sscanf(line, "cpu cores\t: %d", &cpuInfo.Cores); err != nil {
|
||||
fmt.Println("Error parsing cpu cores:", err)
|
||||
}
|
||||
} else if strings.HasPrefix(line, "Architecture") {
|
||||
cpuInfo.Architecture = strings.TrimSpace(strings.Split(line, ":")[1])
|
||||
}
|
||||
}
|
||||
|
||||
// getCPUInfo 获取 CPU 信息
|
||||
func GetCPUInfo() models.CPUInfo {
|
||||
cpuInfo := NewCPUInfo()
|
||||
|
||||
file, err := os.Open("/proc/cpuinfo")
|
||||
if err != nil {
|
||||
fmt.Println("Error opening /proc/cpuinfo:", err)
|
||||
return cpuInfo // 返回默认值
|
||||
}
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
fmt.Println("Error closing /proc/cpuinfo:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
parseLine(scanner.Text(), &cpuInfo)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Println("Error reading /proc/cpuinfo:", err)
|
||||
return cpuInfo // 返回默认值
|
||||
}
|
||||
|
||||
return cpuInfo
|
||||
}
|
||||
|
||||
/*
|
||||
CPU模型名称: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
|
||||
核心数量: 4
|
||||
架构信息: x86_64
|
||||
|
||||
*/
|
||||
134
cmii-uav-watchdog-agent/host_info/disk_service.go
Normal file
134
cmii-uav-watchdog-agent/host_info/disk_service.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// GetDiskInfo retrieves disk information similar to df -Th.
|
||||
func GetDiskInfo() []models.DiskInfo {
|
||||
var diskInfos []models.DiskInfo
|
||||
|
||||
// Read /proc/mounts to get mounted filesystems
|
||||
mountsData, err := readFileWithWarning("/proc/mounts")
|
||||
if err != nil {
|
||||
return diskInfos
|
||||
}
|
||||
|
||||
mounts := strings.Split(string(mountsData), "\n")
|
||||
|
||||
// Read /proc/partitions to get physical disks
|
||||
partitionsData, err := readFileWithWarning("/proc/partitions")
|
||||
if err != nil {
|
||||
return diskInfos
|
||||
}
|
||||
|
||||
partitions := strings.Split(string(partitionsData), "\n")
|
||||
physicalDevices := make(map[string]uint64)
|
||||
|
||||
// Map physical devices to their sizes
|
||||
for _, line := range partitions {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 4 {
|
||||
continue
|
||||
}
|
||||
device := fields[3] // The device name is in the 4th column
|
||||
size, err := strconv.ParseUint(fields[2], 10, 64) // 读取大小
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
physicalDevices[device] = size * 1024 // 转换为字节
|
||||
}
|
||||
|
||||
for _, mount := range mounts {
|
||||
if mount == "" {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(mount)
|
||||
if len(fields) < 3 {
|
||||
continue
|
||||
}
|
||||
logicalDevice := fields[0] // 逻辑分区设备名
|
||||
mountPoint := fields[1] // 挂载点
|
||||
fstype := fields[2] // 文件系统类型
|
||||
|
||||
// Skip NFS mounts
|
||||
if fstype == "nfs" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip overlay mounts
|
||||
if fstype == "overlay" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get disk usage information using stat
|
||||
var stat syscall.Statfs_t
|
||||
if err := syscall.Statfs(mountPoint, &stat); err != nil {
|
||||
fmt.Printf("Warning: error getting statfs for %s: %v\n", mountPoint, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate total, used, and available space
|
||||
total := stat.Blocks * uint64(stat.Bsize)
|
||||
available := stat.Bavail * uint64(stat.Bsize)
|
||||
used := total - available
|
||||
|
||||
// Skip if size is 0
|
||||
if total == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate percentage used
|
||||
usePercent := "0%"
|
||||
if total > 0 {
|
||||
usePercent = fmt.Sprintf("%.1f%%", float64(used)/float64(total)*100)
|
||||
}
|
||||
|
||||
// Determine the physical device and its size
|
||||
physicalDevice := ""
|
||||
physicalSize := uint64(0)
|
||||
|
||||
// Check if the logical device is a partition (e.g., /dev/sda1)
|
||||
if strings.HasPrefix(logicalDevice, "/dev/") {
|
||||
// Get the corresponding physical device (e.g., /dev/sda)
|
||||
physicalDevice = strings.TrimSuffix(logicalDevice, "1") // 假设逻辑分区是 /dev/sda1
|
||||
if size, exists := physicalDevices[physicalDevice]; exists {
|
||||
physicalSize = size
|
||||
}
|
||||
}
|
||||
|
||||
// If not found, use the logical device as the physical device
|
||||
if physicalDevice == "" || physicalSize == 0 {
|
||||
physicalDevice = logicalDevice
|
||||
if size, exists := physicalDevices[physicalDevice]; exists {
|
||||
physicalSize = size
|
||||
}
|
||||
}
|
||||
|
||||
diskInfos = append(diskInfos, models.DiskInfo{
|
||||
Device: logicalDevice, // 设置逻辑分区的设备名称
|
||||
Filesystem: logicalDevice, // 逻辑分区
|
||||
Type: fstype, // 文件系统类型
|
||||
Size: total, // 总大小
|
||||
Used: used, // 已用空间
|
||||
Available: available, // 可用空间
|
||||
UsePercent: usePercent, // 使用百分比
|
||||
MountPoint: mountPoint, // 挂载点
|
||||
PhysicalDevice: physicalDevice, // 物理设备名称
|
||||
PhysicalSize: physicalSize, // 物理盘大小
|
||||
})
|
||||
}
|
||||
|
||||
return diskInfos
|
||||
}
|
||||
|
||||
/*
|
||||
Device: sda, Total: 500107862016 bytes, Used: 250000000000 bytes, Available: 200000000000 bytes
|
||||
Device: sdb, Total: 400107862016 bytes, Used: 150000000000 bytes, Available: 250000000000 bytes
|
||||
Device: sdc, Total: 100107862016 bytes, Used: 50000000000 bytes, Available: 50000000000 bytes
|
||||
Device: unknown, Total: 0 bytes, Used: 0 bytes, Available: 0 bytes
|
||||
*/
|
||||
27
cmii-uav-watchdog-agent/host_info/host_info.go
Normal file
27
cmii-uav-watchdog-agent/host_info/host_info.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package services
|
||||
|
||||
import "cmii-uav-watchdog-common/models"
|
||||
|
||||
type Data struct {
|
||||
CPUInfo models.CPUInfo `json:"cpu_info"`
|
||||
DiskInfo []models.DiskInfo `json:"disk_info"`
|
||||
MemoryInfo models.MemoryInfo `json:"memory_info"`
|
||||
NetInfo []models.NetworkInterfaceInfo `json:"net_info"`
|
||||
MotherboardInfo models.MotherboardInfo `json:"motherboard_info"`
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
func GetAllInfo() Data {
|
||||
|
||||
data := Data{
|
||||
CPUInfo: GetCPUInfo(),
|
||||
DiskInfo: GetDiskInfo(),
|
||||
MemoryInfo: GetMemoryInfo(),
|
||||
NetInfo: GetNetworkInterfaces(),
|
||||
MotherboardInfo: GetMotherboardInfo(),
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
58
cmii-uav-watchdog-agent/host_info/mboard_service.go
Normal file
58
cmii-uav-watchdog-agent/host_info/mboard_service.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DefaultMotherboardInfo provides a default value for MotherboardInfo.
|
||||
var DefaultMotherboardInfo = models.MotherboardInfo{
|
||||
Manufacturer: "unknown",
|
||||
Product: "unknown",
|
||||
Version: "unknown",
|
||||
Serial: "unknown",
|
||||
}
|
||||
|
||||
// readFileWithWarning attempts to read a file and logs a warning if it fails.
|
||||
func readFileWithWarning(path string) ([]byte, error) {
|
||||
value, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Printf("Warning: unable to read file %s: %v\n", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// GetMotherboardInfo retrieves motherboard information from the system files.
|
||||
func GetMotherboardInfo() models.MotherboardInfo {
|
||||
info := DefaultMotherboardInfo // 初始化为默认值
|
||||
|
||||
// 文件路径与对应字段的映射
|
||||
paths := map[string]*string{
|
||||
"/sys/class/dmi/id/board_vendor": &info.Manufacturer,
|
||||
"/sys/class/dmi/id/board_name": &info.Product,
|
||||
"/sys/class/dmi/id/board_version": &info.Version,
|
||||
"/sys/class/dmi/id/board_serial": &info.Serial,
|
||||
}
|
||||
|
||||
// 遍历路径并更新信息
|
||||
for path, field := range paths {
|
||||
if value, err := readFileWithWarning(path); err == nil {
|
||||
*field = strings.TrimSpace(string(value))
|
||||
}
|
||||
}
|
||||
|
||||
return info // 返回最终的主板信息
|
||||
}
|
||||
|
||||
/*
|
||||
Motherboard Information:
|
||||
Manufacturer: ASUSTeK COMPUTER INC.
|
||||
Product: ROG STRIX B450-F GAMING
|
||||
Version: Rev 1.xx
|
||||
Serial: 123456789012345678
|
||||
|
||||
|
||||
*/
|
||||
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
|
||||
|
||||
*/
|
||||
77
cmii-uav-watchdog-agent/host_info/net_service.go
Normal file
77
cmii-uav-watchdog-agent/host_info/net_service.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/models"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// getMACAddress 获取指定接口的 MAC 地址
|
||||
func getMACAddress(iface net.Interface) (string, error) {
|
||||
_, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 获取 MAC 地址
|
||||
return iface.HardwareAddr.String(), nil
|
||||
}
|
||||
|
||||
// getNetworkInterfaces 获取网卡信息
|
||||
func GetNetworkInterfaces() []models.NetworkInterfaceInfo {
|
||||
var interfaces []models.NetworkInterfaceInfo
|
||||
|
||||
// 获取所有网络接口
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
fmt.Println("Error getting network interfaces:", err)
|
||||
return []models.NetworkInterfaceInfo{{Name: "unknown", MACAddress: "00:00:00:00:00:00", IPAddresses: []string{}}}
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
// 过滤掉 Docker 和 Kubernetes 网络插件创建的网卡
|
||||
if strings.HasPrefix(iface.Name, "docker") || strings.HasPrefix(iface.Name, "cali") ||
|
||||
strings.HasPrefix(iface.Name, "flannel") || strings.HasPrefix(iface.Name, "br") ||
|
||||
strings.HasPrefix(iface.Name, "lo") || strings.HasPrefix(iface.Name, "tunl0") {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取 MAC 地址
|
||||
macAddress, err := getMACAddress(iface)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting MAC address for", iface.Name, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取有效的 IP 地址
|
||||
var ipAddresses []string
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
fmt.Println("Error getting addresses for", iface.Name, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.To4() != nil {
|
||||
ipAddresses = append(ipAddresses, ipNet.IP.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 仅在有 IP 地址时才添加到接口列表
|
||||
if len(ipAddresses) > 0 {
|
||||
interfaces = append(interfaces, models.NetworkInterfaceInfo{
|
||||
Name: iface.Name,
|
||||
MACAddress: macAddress,
|
||||
IPAddresses: ipAddresses,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到任何有效的接口,返回默认值
|
||||
if len(interfaces) == 0 {
|
||||
return []models.NetworkInterfaceInfo{{Name: "unknown", MACAddress: "00:00:00:00:00:00", IPAddresses: []string{}}}
|
||||
}
|
||||
|
||||
return interfaces
|
||||
}
|
||||
Reference in New Issue
Block a user