4.Golang + Gin + VUE3/React + Tailwindcss + Tauri2.0 实现Linux管理系统-实现Linux系统基本信息获取
编辑文件文件,新增如下内容// HostInfo Linux系统基本信息HostName string `json:"hostname"` // 主机名Arch string `json:"arch"` // 系统架构Platform string `json:"platform"` // 系统版本KernelVersion string `json:"kernelVersion"` // 内核版
Golang + Gin + VUE3/React + Tailwindcss + Tauri2.0 实现Linux管理系统-实现Linux系统基本信息获取
一、实现获取Linux基本信息
1. 定义结构体
编辑文件internal/data/system.go
文件,新增如下内容
// HostInfo Linux系统基本信息
type HostInfo struct {
HostName string `json:"hostname"` // 主机名
Arch string `json:"arch"` // 系统架构
Platform string `json:"platform"` // 系统版本
KernelVersion string `json:"kernelVersion"` // 内核版本
UpTime uint64 `json:"upTime"` // 运行时间
BootTime uint64 `json:"bootTime"` // 启动时间
}
type ISystemAPI interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo(ctx *gin.Context)
}
type ISystemServer interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo() HostInfo
}
2. 实现获取系统基本信息方法
编辑文件internal/server/system.go
文件,新增如下内容
可以先在命令行中执行
go get github.com/shirou/gopsutil/v3/host
或者在运行前使用
go mod tidy
命令同步一下项目依赖
package server
import (
"fmt"
"linuxManagerAgent/internal/data"
"github.com/shirou/gopsutil/v3/host"
)
type SystemServer struct {
}
func (s *SystemServer) GetBaseSystemInfo() data.HostInfo {
systemInfo, _ := host.Info()
return data.HostInfo{
HostName: systemInfo.Hostname,
Arch: systemInfo.KernelArch,
Platform: fmt.Sprintf("%s %s", systemInfo.Platform, systemInfo.PlatformVersion),
KernelVersion: systemInfo.KernelVersion,
UpTime: systemInfo.Uptime,
BootTime: systemInfo.BootTime,
}
}
func MakeSystemServer() data.ISystemServer {
return &SystemServer{}
}
3. 实现路由调用
编辑文件internal/api/system.go
文件,添加如下内容
package api
import (
"linuxManagerAgent/internal/data"
"linuxManagerAgent/internal/pkg"
"linuxManagerAgent/internal/server"
"github.com/gin-gonic/gin"
)
type SystemAPI struct {
server *server.SystemServer
}
func InitSystemRouter(router *gin.RouterGroup) {
api := MakeSystemAPI()
router.GET("/info", api.GetBaseSystemInfo)
}
func (s *SystemAPI) GetBaseSystemInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetBaseSystemInfo())
}
func MakeSystemAPI() data.ISystemAPI {
return &SystemAPI{
server: server.MakeSystemServer(),
}
}
二、实现获取CPU信息
1. 定义结构体
编辑文件internal/data/system.go
文件,新增如下内容
// CPUInfo CPU基本信息
type CPUInfo struct {
Name string `json:"name"` // CPU名称
Cores int32 `json:"cores"` // CPU核心数
Used float64 `json:"used"` // CPU总使用率
}
type ISystemAPI interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo(ctx *gin.Context)
// GetCPUInfo 获取CPU信息
GetCPUInfo(ctx *gin.Context)
}
type ISystemServer interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo() HostInfo
// GetCPUInfo 获取CPU信息
GetCPUInfo() CPUInfo
}
2. 实现获取CPU信息方法
编辑文件internal/server/system.go
文件,新增如下内容
可以先在命令行中执行
go get github.com/shirou/gopsutil/v3/cpu
或者在运行前使用
go mod tidy
命令同步一下项目依赖
package server
import (
"fmt"
"linuxManagerAgent/internal/data"
"sync"
"github.com/shirou/gopsutil/v3/cpu"
)
type SystemServer struct {
}
...
func (s *SystemServer) GetCPUInfo() data.CPUInfo {
var cpuInfoData data.CPUInfo
wg := &sync.WaitGroup{}
wg.Add(1)
go func(cpuInfoData *data.CPUInfo) {
defer wg.Done()
cpuInfo, _ := cpu.Info()
info := cpuInfo[0]
cpuInfoData.Cores = info.Cores
cpuInfoData.Name = info.ModelName
}(&cpuInfoData)
percent, _ := cpu.Percent(3*time.Second, true)
cpuInfoData.Used = percent[0]
wg.Wait()
return cpuInfoData
}
func MakeSystemServer() data.ISystemServer {
return &SystemServer{}
}
3. 实现路由调用
编辑文件internal/api/system.go
文件,添加如下内容
package api
import (
"linuxManagerAgent/internal/data"
"linuxManagerAgent/internal/pkg"
"linuxManagerAgent/internal/server"
"github.com/gin-gonic/gin"
)
type SystemAPI struct {
server *server.SystemServer
}
func InitSystemRouter(router *gin.RouterGroup) {
api := MakeSystemAPI()
...
router.GET("/cpu", api.CPUInfo)
}
...
func (s *SystemAPI) GetCPUInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetCPUInfo())
}
func MakeSystemAPI() data.ISystemAPI {
return &SystemAPI{
server: server.MakeSystemServer(),
}
}
三、实现获取内存信息
1. 定义结构体
编辑文件internal/data/system.go
文件,新增如下内容
type MemInfo struct {
Total uint64 `json:"total"` // 总内存大小
Used uint64 `json:"used"` // 内存使用大小
Available uint64 `json:"available"` // 可用内存
Free uint64 `json:"free"` // 空闲内存
Cached uint64 `json:"cached"` // 缓存内存
Shared uint64 `json:"shared"` // 共享内存
SwapTotal uint64 `json:"swapTotal"` // 交换分区大小
SwapFree uint64 `json:"swapFree"` // 交换分区可使用大小
}
type ISystemAPI interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo(ctx *gin.Context)
// GetCPUInfo 获取CPU信息
GetCPUInfo(ctx *gin.Context)
// GetMemInfo 获取内存信息
GetMemInfo(ctx *gin.Context)
}
type ISystemServer interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo() HostInfo
// GetCPUInfo 获取CPU信息
GetCPUInfo() CPUInfo
// GetMemInfo 获取内存使用率
GetMemInfo() MemInfo
}
2. 实现获取内存信息方法
编辑文件internal/server/system.go
文件,新增如下内容
可以先在命令行中执行
go get github.com/shirou/gopsutil/v3/cpu
或者在运行前使用
go mod tidy
命令同步一下项目依赖
package server
import (
"fmt"
"linuxManagerAgent/internal/data"
"github.com/shirou/gopsutil/v3/cpu"
)
type SystemServer struct {
}
...
memInfo, _ := mem.VirtualMemory()
return data.MemInfo{
Total: memInfo.Total,
Used: memInfo.Used,
Available: memInfo.Available,
Free: memInfo.Free,
Cached: memInfo.Buffers,
Shared: memInfo.Shared,
SwapTotal: memInfo.SwapTotal,
SwapFree: memInfo.SwapFree,
}
}
func MakeSystemServer() data.ISystemServer {
return &SystemServer{}
}
3. 实现路由调用
编辑文件internal/api/system.go
文件,添加如下内容
package api
import (
"linuxManagerAgent/internal/data"
"linuxManagerAgent/internal/pkg"
"linuxManagerAgent/internal/server"
"github.com/gin-gonic/gin"
)
type SystemAPI struct {
server *server.SystemServer
}
func InitSystemRouter(router *gin.RouterGroup) {
api := MakeSystemAPI()
...
router.GET("/mem", api.GetMemInfo)
}
...
func (s *SystemAPI) GetCPUInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetCPUInfo())
}
func (s *SystemAPI) GetMemInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetMemInfo())
}
func MakeSystemAPI() data.ISystemAPI {
return &SystemAPI{
server: server.MakeSystemServer(),
}
}
四、实现获取硬盘信息
1. 定义结构体
编辑文件internal/data/system.go
文件,新增如下内容
type DiskInfo struct {
Path string `json:"path"` // 挂载点
FsType string `json:"fsType"` // 文件系统
Total uint64 `json:"total"` // 总大小
Free uint64 `json:"free"` // 未使用大小
Used uint64 `json:"used"` // 使用大小
Usage float64 `json:"usage"` // 使用率
InodesTotal uint64 `json:"inodesTotal"` // inodes总大小
InodesFree uint64 `json:"inodesFree"` // inodes剩余大小
InodesUsed uint64 `json:"inodesUsed"` // inodes使用大小
InodesUsage float64 `json:"inodesUsage"` // inodes使用率
}
type ISystemAPI interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo(ctx *gin.Context)
// GetCPUInfo 获取CPU信息
GetCPUInfo(ctx *gin.Context)
// GetMemInfo 获取内存信息
GetMemInfo(ctx *gin.Context)
// GetDiskInfo 获取磁盘信息
GetDiskInfo(ctx *gin.Context)
}
type ISystemServer interface {
// GetBaseSystemInfo 获取系统基本信息
GetBaseSystemInfo() HostInfo
// GetCPUInfo 获取CPU信息
GetCPUInfo() CPUInfo
// GetMemInfo 获取内存使用率
GetMemInfo() MemInfo
// GetDiskInfo 获取磁盘信息
GetDiskInfo(ctx *gin.Context)
}
2. 实现获取磁盘信息方法
编辑文件internal/server/system.go
文件,新增如下内容
可以先在命令行中执行
go get github.com/shirou/gopsutil/v3/disk
或者在运行前使用
go mod tidy
命令同步一下项目依赖
package server
import (
"fmt"
"linuxManagerAgent/internal/data"
"github.com/shirou/gopsutil/v3/disk"
)
type SystemServer struct {
}
...
func (s *SystemServer) GetDiskInfo() []data.DiskInfo {
var result []data.DiskInfo
diskInfo, _ := disk.Partitions(false)
for _, info := range diskInfo {
var dInfo data.DiskInfo
diskExtends, _ := disk.Usage(info.Mountpoint)
dInfo.Usage = diskExtends.UsedPercent
dInfo.Total = diskExtends.Total
dInfo.Free = diskExtends.Free
dInfo.Used = diskExtends.Used
dInfo.Path = diskExtends.Path
dInfo.InodesTotal = diskExtends.InodesTotal
dInfo.InodesFree = diskExtends.InodesFree
dInfo.InodesUsed = diskExtends.InodesUsed
dInfo.InodesUsage = diskExtends.InodesUsedPercent
result = append(result, dInfo)
}
return result
}
func MakeSystemServer() data.ISystemServer {
return &SystemServer{}
}
3. 实现路由调用
编辑文件internal/api/system.go
文件,添加如下内容
package api
import (
"linuxManagerAgent/internal/data"
"linuxManagerAgent/internal/pkg"
"linuxManagerAgent/internal/server"
"github.com/gin-gonic/gin"
)
type SystemAPI struct {
server *server.SystemServer
}
func InitSystemRouter(router *gin.RouterGroup) {
api := MakeSystemAPI()
...
router.GET("/disk", api.GetBaseSystemInfo)
}
...
func (s *SystemAPI) GetDiskInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetCPUInfo())
}
func (s *SystemAPI) GetMemInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetMemInfo())
}
func (s *SystemAPI) GetDiskInfo(ctx *gin.Context) {
pkg.SuccessWithData(ctx, s.server.GetDiskInfo())
}
func MakeSystemAPI() data.ISystemAPI {
return &SystemAPI{
server: server.MakeSystemServer(),
}
}
五、注册全局路由
编辑internal/webServer/router.go
,添加如下内容
func initRouter() *gin.Engine {
// 创建路由
app := gin.Default()
app.Use(Cors())
// 判断服务器服务是否正常
app.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"data": "pong", "code": 200, "msg": "成功"})
})
api.InitSystemRouter(app.Group("system"))
return app
}
更多推荐
所有评论(0)