mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
init
This commit is contained in:
53
monitoring/cpu.go
Normal file
53
monitoring/cpu.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
)
|
||||
|
||||
type CpuInfo struct {
|
||||
CPUName string `json:"cpu_name"`
|
||||
CPUArchitecture string `json:"cpu_architecture"`
|
||||
CPUCores int `json:"cpu_cores"`
|
||||
CPUUsage float64 `json:"cpu_usage"`
|
||||
}
|
||||
|
||||
func Cpu() CpuInfo {
|
||||
cpuinfo := CpuInfo{}
|
||||
info, err := cpu.Info()
|
||||
if err != nil {
|
||||
cpuinfo.CPUName = "Unknown"
|
||||
}
|
||||
// multiple CPU
|
||||
// 多个 CPU
|
||||
if len(info) > 1 {
|
||||
for _, cpu := range info {
|
||||
cpuinfo.CPUName += cpu.ModelName + ", "
|
||||
}
|
||||
cpuinfo.CPUName = cpuinfo.CPUName[:len(cpuinfo.CPUName)-2] // Remove trailing comma and space
|
||||
} else if len(info) == 1 {
|
||||
cpuinfo.CPUName = info[0].ModelName
|
||||
}
|
||||
|
||||
cpuinfo.CPUName = strings.TrimSpace(cpuinfo.CPUName)
|
||||
|
||||
cpuinfo.CPUArchitecture = runtime.GOARCH
|
||||
|
||||
cores, err := cpu.Counts(true)
|
||||
if err != nil {
|
||||
cpuinfo.CPUCores = 1 // Error case
|
||||
}
|
||||
cpuinfo.CPUCores = cores
|
||||
|
||||
// Get CPU Usage
|
||||
percentages, err := cpu.Percent(1*time.Second, false)
|
||||
if err != nil {
|
||||
cpuinfo.CPUUsage = 0.0 // Error case
|
||||
} else {
|
||||
cpuinfo.CPUUsage = percentages[0]
|
||||
}
|
||||
return cpuinfo
|
||||
}
|
35
monitoring/disk.go
Normal file
35
monitoring/disk.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
)
|
||||
|
||||
type DiskInfo struct {
|
||||
Total uint64 `json:"total"`
|
||||
Used uint64 `json:"used"`
|
||||
}
|
||||
|
||||
func Disk() DiskInfo {
|
||||
diskinfo := DiskInfo{}
|
||||
usage, err := disk.Partitions(true)
|
||||
if err != nil {
|
||||
diskinfo.Total = 0
|
||||
diskinfo.Used = 0
|
||||
} else {
|
||||
for _, part := range usage {
|
||||
if part.Mountpoint != "/tmp" && part.Mountpoint != "/var/tmp" && part.Mountpoint != "/dev/shm" {
|
||||
// Skip /tmp, /var/tmp, and /dev/shm
|
||||
// 获取磁盘使用情况
|
||||
u, err := disk.Usage(part.Mountpoint)
|
||||
if err != nil {
|
||||
diskinfo.Total = 0
|
||||
diskinfo.Used = 0
|
||||
} else {
|
||||
diskinfo.Total += u.Total
|
||||
diskinfo.Used += u.Used
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return diskinfo
|
||||
}
|
1
monitoring/gpu.go
Normal file
1
monitoring/gpu.go
Normal file
@@ -0,0 +1 @@
|
||||
package monitoring
|
25
monitoring/load.go
Normal file
25
monitoring/load.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/load"
|
||||
)
|
||||
|
||||
type LoadInfo struct {
|
||||
Load1 float64 `json:"load_1"`
|
||||
Load5 float64 `json:"load_5"`
|
||||
Load15 float64 `json:"load_15"`
|
||||
}
|
||||
|
||||
func Load() LoadInfo {
|
||||
|
||||
avg, err := load.Avg()
|
||||
if err != nil {
|
||||
return LoadInfo{Load1: 0, Load5: 0, Load15: 0}
|
||||
}
|
||||
return LoadInfo{
|
||||
Load1: avg.Load1,
|
||||
Load5: avg.Load5,
|
||||
Load15: avg.Load15,
|
||||
}
|
||||
|
||||
}
|
35
monitoring/mem.go
Normal file
35
monitoring/mem.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
|
||||
type RamInfo struct {
|
||||
Total uint64 `json:"total"`
|
||||
Used uint64 `json:"used"`
|
||||
}
|
||||
|
||||
func Ram() RamInfo {
|
||||
raminfo := RamInfo{}
|
||||
v, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
raminfo.Total = 0
|
||||
raminfo.Used = 0
|
||||
} else {
|
||||
raminfo.Total = v.Total
|
||||
raminfo.Used = v.Used
|
||||
}
|
||||
return raminfo
|
||||
}
|
||||
func Swap() RamInfo {
|
||||
swapinfo := RamInfo{}
|
||||
s, err := mem.SwapMemory()
|
||||
if err != nil {
|
||||
swapinfo.Total = 0
|
||||
swapinfo.Used = 0
|
||||
} else {
|
||||
swapinfo.Total = s.Total
|
||||
swapinfo.Used = s.Used
|
||||
}
|
||||
return swapinfo
|
||||
}
|
51
monitoring/net.go
Normal file
51
monitoring/net.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
func ConnectionsCount() (tcpCount, udpCount int, err error) {
|
||||
tcps, err := net.Connections("tcp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get TCP connections: %w", err)
|
||||
}
|
||||
udps, err := net.Connections("udp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get UDP connections: %w", err)
|
||||
}
|
||||
|
||||
return len(tcps), len(udps), nil
|
||||
}
|
||||
|
||||
func NetworkSpeed() (upSpeed, downSpeed float64, err error) {
|
||||
// Get the network IO counters
|
||||
ioCounters, err := net.IOCounters(false)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
|
||||
}
|
||||
|
||||
if len(ioCounters) == 0 {
|
||||
return 0, 0, fmt.Errorf("no network interfaces found")
|
||||
}
|
||||
|
||||
for _, interfaceStats := range ioCounters {
|
||||
loopbackNames := []string{"lo", "lo0", "localhost", "brd0", "docker0", "docker1", "veth0", "veth1", "veth2", "veth3", "veth4", "veth5", "veth6", "veth7"}
|
||||
isLoopback := false
|
||||
for _, name := range loopbackNames {
|
||||
if interfaceStats.Name == name {
|
||||
isLoopback = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isLoopback {
|
||||
continue // Skip loopback interface
|
||||
}
|
||||
upSpeed += float64(interfaceStats.BytesSent) / float64(interfaceStats.PacketsSent)
|
||||
downSpeed += float64(interfaceStats.BytesRecv) / float64(interfaceStats.PacketsRecv)
|
||||
|
||||
}
|
||||
|
||||
return upSpeed, downSpeed, nil
|
||||
}
|
49
monitoring/os.go
Normal file
49
monitoring/os.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
func OSName() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
productName, _, err := key.GetStringValue("ProductName")
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
|
||||
return productName
|
||||
} else if runtime.GOOS == "linux" {
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "PRETTY_NAME=") {
|
||||
return strings.Trim(line[len("PRETTY_NAME="):], `"`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
return "Unknown"
|
||||
}
|
11
monitoring/uptime.go
Normal file
11
monitoring/uptime.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/host"
|
||||
)
|
||||
|
||||
func Uptime() (uint64, error) {
|
||||
|
||||
return host.Uptime()
|
||||
|
||||
}
|
Reference in New Issue
Block a user