add agent
This commit is contained in:
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
@@ -1,8 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
6
.idea/misc.xml
generated
6
.idea/misc.xml
generated
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="graalvm-jdk-23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
1
.idea/modules.xml
generated
1
.idea/modules.xml
generated
@@ -2,7 +2,6 @@
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/cmii-uav-watchdog-common.iml" filepath="$PROJECT_DIR$/cmii-uav-watchdog-common.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/cmii-uav-watchdog-project.iml" filepath="$PROJECT_DIR$/.idea/cmii-uav-watchdog-project.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
|
||||
@@ -1,3 +1,39 @@
|
||||
module cmii-uav-watchdog-agent
|
||||
|
||||
go 1.23
|
||||
go 1.24
|
||||
|
||||
require (
|
||||
cmii-uav-watchdog-common v0.0.0
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/text v0.15.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace cmii-uav-watchdog-common => ../cmii-uav-watchdog-common
|
||||
|
||||
89
cmii-uav-watchdog-agent/go.sum
Normal file
89
cmii-uav-watchdog-agent/go.sum
Normal file
@@ -0,0 +1,89 @@
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
@@ -1 +1,69 @@
|
||||
package cmii_uav_watchdog_agent
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-agent/services"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个默认的 Gin 路由
|
||||
var r = gin.Default() // 定义一个 GET 路由
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
// 定义一个 POST 路由
|
||||
r.POST("/echo", func(c *gin.Context) {
|
||||
var json map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&json); err == nil {
|
||||
c.JSON(http.StatusOK, json)
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
})
|
||||
|
||||
r.GET("/cpu", func(c *gin.Context) {
|
||||
cpuInfo := services.GetCPUInfo() // 直接返回 CPU 信息
|
||||
c.JSON(http.StatusOK, cpuInfo)
|
||||
})
|
||||
|
||||
r.GET("/memory", func(c *gin.Context) {
|
||||
memInfo := services.GetMemoryInfo() // 直接返回内存信息
|
||||
c.JSON(http.StatusOK, memInfo)
|
||||
})
|
||||
|
||||
r.GET("/disk", func(c *gin.Context) {
|
||||
diskInfo := services.GetDiskInfo() // 直接返回磁盘信息
|
||||
c.JSON(http.StatusOK, diskInfo)
|
||||
})
|
||||
|
||||
r.GET("/motherboard", func(c *gin.Context) {
|
||||
mbInfo := services.GetMotherboardInfo() // 直接返回主板信息
|
||||
c.JSON(http.StatusOK, mbInfo)
|
||||
})
|
||||
|
||||
r.GET("/network", func(c *gin.Context) {
|
||||
networkInterfaces := services.GetNetworkInterfaces()
|
||||
c.JSON(http.StatusOK, networkInterfaces)
|
||||
})
|
||||
r.GET("/all", func(c *gin.Context) {
|
||||
allInfo := services.GetAllInfo()
|
||||
c.JSON(http.StatusOK, allInfo)
|
||||
})
|
||||
//r.GET("/phy", func(c *gin.Context) {
|
||||
// allInfo, _ := services.GetPVForLV()
|
||||
// c.JSON(http.StatusOK, allInfo)
|
||||
//})
|
||||
// 启动服务,监听在 8080 端口
|
||||
r.Run(":8098")
|
||||
// 等待终止信号
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigs
|
||||
fmt.Println("Shutting down service...")
|
||||
}
|
||||
|
||||
65
cmii-uav-watchdog-agent/services/cpu_service.go
Normal file
65
cmii-uav-watchdog-agent/services/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/services/disk_service.go
Normal file
134
cmii-uav-watchdog-agent/services/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
|
||||
*/
|
||||
@@ -1 +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/services/mboard_service.go
Normal file
58
cmii-uav-watchdog-agent/services/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/services/mem_service.go
Normal file
60
cmii-uav-watchdog-agent/services/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/services/net_service.go
Normal file
77
cmii-uav-watchdog-agent/services/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
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/cmii-uav-watchdog-common" />
|
||||
|
||||
@@ -1,14 +1,66 @@
|
||||
package models
|
||||
|
||||
// CPUInfo 结构体用于存储 CPU 信息
|
||||
type CPUInfo struct {
|
||||
ModelName string `json:"model_name"`
|
||||
Cores int `json:"cores"`
|
||||
Architecture string `json:"architecture"`
|
||||
}
|
||||
|
||||
// MemoryInfo holds the memory information.
|
||||
type MemoryInfo struct {
|
||||
Total uint64 `json:"total"`
|
||||
Free uint64 `json:"free"`
|
||||
Available uint64 `json:"available"`
|
||||
}
|
||||
|
||||
// DiskInfo holds the disk information similar to df -Th output.
|
||||
type DiskInfo struct {
|
||||
Device string `json:"device"` // 逻辑分区设备名称
|
||||
Filesystem string `json:"filesystem"` // 逻辑分区
|
||||
Type string `json:"type"` // 文件系统类型
|
||||
Size uint64 `json:"size"` // 总大小
|
||||
Used uint64 `json:"used"` // 已用空间
|
||||
Available uint64 `json:"available"` // 可用空间
|
||||
UsePercent string `json:"use_percent"` // 使用百分比
|
||||
MountPoint string `json:"mountpoint"` // 挂载点
|
||||
PhysicalDevice string `json:"physical_device"` // 物理设备名称
|
||||
PhysicalSize uint64 `json:"physical_size"` // 物理盘大小
|
||||
}
|
||||
|
||||
// NetworkInterfaceInfo 结构体用于存储网卡信息
|
||||
type NetworkInterfaceInfo struct {
|
||||
Name string `json:"name"`
|
||||
MACAddress string `json:"mac_address"`
|
||||
IPAddresses []string `json:"ip_addresses"`
|
||||
}
|
||||
|
||||
// MotherboardInfo holds the motherboard information.
|
||||
type MotherboardInfo struct {
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
Product string `json:"product"`
|
||||
Version string `json:"version"`
|
||||
Serial string `json:"serial"`
|
||||
}
|
||||
|
||||
// HostInfo 主机信息模型
|
||||
type HostInfo struct {
|
||||
UUID string `json:"uuid"` // 主机UUID
|
||||
CPU string `json:"cpu"` // CPU信息
|
||||
Motherboard string `json:"motherboard"` // 主板信息
|
||||
MAC string `json:"mac"` // MAC地址
|
||||
Disk string `json:"disk"` // 硬盘信息
|
||||
CPUInfo CPUInfo `json:"cpu_info"`
|
||||
DiskInfo []DiskInfo `json:"disk_info"`
|
||||
MemoryInfo MemoryInfo `json:"memory_info"`
|
||||
NetInfo []NetworkInterfaceInfo `json:"net_info"`
|
||||
MotherboardInfo MotherboardInfo `json:"motherboard_info"`
|
||||
}
|
||||
|
||||
//// HostInfo 主机信息模型
|
||||
//type HostInfo struct {
|
||||
// UUID string `json:"uuid"` // 主机UUID
|
||||
// CPU string `json:"cpu"` // CPU信息
|
||||
// Motherboard string `json:"motherboard"` // 主板信息
|
||||
// MAC string `json:"mac"` // MAC地址
|
||||
// Disk string `json:"disk"` // 硬盘信息
|
||||
//}
|
||||
|
||||
// HeartbeatRequest 心跳请求
|
||||
type HeartbeatRequest struct {
|
||||
HostInfo HostInfo `json:"host_info"` // 主机信息
|
||||
|
||||
Reference in New Issue
Block a user