feat: 合并Gin_Vue
This commit is contained in:
38
src/modules/monitor/service/sys_job.go
Normal file
38
src/modules/monitor/service/sys_job.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJob 调度任务信息 服务层接口
|
||||
type ISysJob interface {
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
SelectJobPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
SelectJobList(sysJob model.SysJob) []model.SysJob
|
||||
|
||||
// SelectJobById 通过调度ID查询调度任务信息
|
||||
SelectJobById(jobId string) model.SysJob
|
||||
|
||||
// CheckUniqueJobName 校验调度任务名称和组是否唯一
|
||||
CheckUniqueJobName(jobName, jobGroup, jobId string) bool
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
InsertJob(sysJob model.SysJob) string
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
UpdateJob(sysJob model.SysJob) int64
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
DeleteJobByIds(jobIds []string) (int64, error)
|
||||
|
||||
// ChangeStatus 任务调度状态修改
|
||||
ChangeStatus(sysJob model.SysJob) bool
|
||||
|
||||
// RunQueueJob 立即运行一次调度任务
|
||||
RunQueueJob(sysJob model.SysJob) bool
|
||||
|
||||
// ResetQueueJob 重置初始调度任务
|
||||
ResetQueueJob()
|
||||
}
|
||||
190
src/modules/monitor/service/sys_job.impl.go
Normal file
190
src/modules/monitor/service/sys_job.impl.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"ems.agt/src/framework/constants/common"
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysJobImpl 结构体
|
||||
var NewSysJobImpl = &SysJobImpl{
|
||||
sysJobRepository: repository.NewSysJobImpl,
|
||||
}
|
||||
|
||||
// SysJobImpl 调度任务 服务层处理
|
||||
type SysJobImpl struct {
|
||||
// 调度任务数据信息
|
||||
sysJobRepository repository.ISysJob
|
||||
}
|
||||
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobPage(query map[string]any) map[string]any {
|
||||
return r.sysJobRepository.SelectJobPage(query)
|
||||
}
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobList(sysJob model.SysJob) []model.SysJob {
|
||||
return r.sysJobRepository.SelectJobList(sysJob)
|
||||
}
|
||||
|
||||
// SelectJobById 通过调度ID查询调度任务信息
|
||||
func (r *SysJobImpl) SelectJobById(jobId string) model.SysJob {
|
||||
if jobId == "" {
|
||||
return model.SysJob{}
|
||||
}
|
||||
jobs := r.sysJobRepository.SelectJobByIds([]string{jobId})
|
||||
if len(jobs) > 0 {
|
||||
return jobs[0]
|
||||
}
|
||||
return model.SysJob{}
|
||||
}
|
||||
|
||||
// CheckUniqueJobName 校验调度任务名称和组是否唯一
|
||||
func (r *SysJobImpl) CheckUniqueJobName(jobName, jobGroup, jobId string) bool {
|
||||
uniqueId := r.sysJobRepository.CheckUniqueJob(model.SysJob{
|
||||
JobName: jobName,
|
||||
JobGroup: jobGroup,
|
||||
})
|
||||
if uniqueId == jobId {
|
||||
return true
|
||||
}
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
func (r *SysJobImpl) InsertJob(sysJob model.SysJob) string {
|
||||
insertId := r.sysJobRepository.InsertJob(sysJob)
|
||||
if insertId == "" && sysJob.Status == common.STATUS_YES {
|
||||
sysJob.JobID = insertId
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
return insertId
|
||||
}
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
func (r *SysJobImpl) UpdateJob(sysJob model.SysJob) int64 {
|
||||
rows := r.sysJobRepository.UpdateJob(sysJob)
|
||||
if rows > 0 {
|
||||
//状态正常添加队列任务
|
||||
if sysJob.Status == common.STATUS_YES {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
// 状态禁用删除队列任务
|
||||
if sysJob.Status == common.STATUS_NO {
|
||||
r.deleteQueueJob(sysJob)
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
func (r *SysJobImpl) DeleteJobByIds(jobIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
jobs := r.sysJobRepository.SelectJobByIds(jobIds)
|
||||
if len(jobs) <= 0 {
|
||||
return 0, errors.New("没有权限访问调度任务数据!")
|
||||
}
|
||||
if len(jobs) == len(jobIds) {
|
||||
// 清除任务
|
||||
for _, job := range jobs {
|
||||
r.deleteQueueJob(job)
|
||||
}
|
||||
rows := r.sysJobRepository.DeleteJobByIds(jobIds)
|
||||
return rows, nil
|
||||
}
|
||||
return 0, errors.New("删除调度任务信息失败!")
|
||||
}
|
||||
|
||||
// ChangeStatus 任务调度状态修改
|
||||
func (r *SysJobImpl) ChangeStatus(sysJob model.SysJob) bool {
|
||||
// 更新状态
|
||||
newSysJob := model.SysJob{
|
||||
JobID: sysJob.JobID,
|
||||
Status: sysJob.Status,
|
||||
UpdateBy: sysJob.UpdateBy,
|
||||
}
|
||||
rows := r.sysJobRepository.UpdateJob(newSysJob)
|
||||
if rows > 0 {
|
||||
//状态正常添加队列任务
|
||||
if sysJob.Status == common.STATUS_YES {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
// 状态禁用删除队列任务
|
||||
if sysJob.Status == common.STATUS_NO {
|
||||
r.deleteQueueJob(sysJob)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ResetQueueJob 重置初始调度任务
|
||||
func (r *SysJobImpl) ResetQueueJob() {
|
||||
// 获取注册的队列名称
|
||||
queueNames := cron.QueueNames()
|
||||
if len(queueNames) == 0 {
|
||||
return
|
||||
}
|
||||
// 查询系统中定义状态为正常启用的任务
|
||||
sysJobs := r.sysJobRepository.SelectJobList(model.SysJob{
|
||||
Status: common.STATUS_YES,
|
||||
})
|
||||
for _, sysJob := range sysJobs {
|
||||
for _, name := range queueNames {
|
||||
if name == sysJob.InvokeTarget {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunQueueJob 立即运行一次调度任务
|
||||
func (r *SysJobImpl) RunQueueJob(sysJob model.SysJob) bool {
|
||||
return r.insertQueueJob(sysJob, false)
|
||||
}
|
||||
|
||||
// insertQueueJob 添加调度任务
|
||||
func (r *SysJobImpl) insertQueueJob(sysJob model.SysJob, repeat bool) bool {
|
||||
// 获取队列 Processor
|
||||
queue := cron.GetQueue(sysJob.InvokeTarget)
|
||||
if queue.Name != sysJob.InvokeTarget {
|
||||
return false
|
||||
}
|
||||
|
||||
// 给执行任务数据参数
|
||||
options := cron.JobData{
|
||||
Repeat: repeat,
|
||||
SysJob: sysJob,
|
||||
}
|
||||
|
||||
// 不是重复任务的情况,立即执行一次
|
||||
if !repeat {
|
||||
// 执行单次任务
|
||||
status := queue.RunJob(options, cron.JobOptions{
|
||||
JobId: sysJob.JobID,
|
||||
})
|
||||
// 执行中或等待中的都返回正常
|
||||
return status == cron.Active || status == cron.Waiting
|
||||
}
|
||||
|
||||
// 执行重复任务
|
||||
queue.RunJob(options, cron.JobOptions{
|
||||
JobId: sysJob.JobID,
|
||||
Cron: sysJob.CronExpression,
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// deleteQueueJob 删除调度任务
|
||||
func (r *SysJobImpl) deleteQueueJob(sysJob model.SysJob) bool {
|
||||
// 获取队列 Processor
|
||||
queue := cron.GetQueue(sysJob.InvokeTarget)
|
||||
if queue.Name != sysJob.InvokeTarget {
|
||||
return false
|
||||
}
|
||||
return queue.RemoveJob(sysJob.JobID)
|
||||
}
|
||||
23
src/modules/monitor/service/sys_job_log.go
Normal file
23
src/modules/monitor/service/sys_job_log.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJobLog 调度任务日志 服务层接口
|
||||
type ISysJobLog interface {
|
||||
// SelectJobLogPage 分页查询调度任务日志集合
|
||||
SelectJobLogPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobLogList 查询调度任务日志集合
|
||||
SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog
|
||||
|
||||
// SelectJobLogById 通过调度ID查询调度任务日志信息
|
||||
SelectJobLogById(jobLogId string) model.SysJobLog
|
||||
|
||||
// DeleteJobLogByIds 批量删除调度任务日志信息
|
||||
DeleteJobLogByIds(jobLogIds []string) int64
|
||||
|
||||
// CleanJobLog 清空调度任务日志
|
||||
CleanJobLog() error
|
||||
}
|
||||
42
src/modules/monitor/service/sys_job_log.impl.go
Normal file
42
src/modules/monitor/service/sys_job_log.impl.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysJobLogImpl 结构体
|
||||
var NewSysJobLogImpl = &SysJobLogImpl{
|
||||
sysJobLogRepository: repository.NewSysJobLogImpl,
|
||||
}
|
||||
|
||||
// SysJobLogImpl 调度任务日志 服务层处理
|
||||
type SysJobLogImpl struct {
|
||||
// 调度任务日志数据信息
|
||||
sysJobLogRepository repository.ISysJobLog
|
||||
}
|
||||
|
||||
// SelectJobLogPage 分页查询调度任务日志集合
|
||||
func (s *SysJobLogImpl) SelectJobLogPage(query map[string]any) map[string]any {
|
||||
return s.sysJobLogRepository.SelectJobLogPage(query)
|
||||
}
|
||||
|
||||
// SelectJobLogList 查询调度任务日志集合
|
||||
func (s *SysJobLogImpl) SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog {
|
||||
return s.sysJobLogRepository.SelectJobLogList(sysJobLog)
|
||||
}
|
||||
|
||||
// SelectJobLogById 通过调度ID查询调度任务日志信息
|
||||
func (s *SysJobLogImpl) SelectJobLogById(jobLogId string) model.SysJobLog {
|
||||
return s.sysJobLogRepository.SelectJobLogById(jobLogId)
|
||||
}
|
||||
|
||||
// DeleteJobLogByIds 批量删除调度任务日志信息
|
||||
func (s *SysJobLogImpl) DeleteJobLogByIds(jobLogIds []string) int64 {
|
||||
return s.sysJobLogRepository.DeleteJobLogByIds(jobLogIds)
|
||||
}
|
||||
|
||||
// CleanJobLog 清空调度任务日志
|
||||
func (s *SysJobLogImpl) CleanJobLog() error {
|
||||
return s.sysJobLogRepository.CleanJobLog()
|
||||
}
|
||||
12
src/modules/monitor/service/sys_user_online.go
Normal file
12
src/modules/monitor/service/sys_user_online.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/vo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysUserOnline 在线用户 服务层接口
|
||||
type ISysUserOnline interface {
|
||||
// LoginUserToUserOnline 设置在线用户信息
|
||||
LoginUserToUserOnline(loginUser vo.LoginUser) model.SysUserOnline
|
||||
}
|
||||
33
src/modules/monitor/service/sys_user_online.impl.go
Normal file
33
src/modules/monitor/service/sys_user_online.impl.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/vo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysUserOnlineImpl 结构体
|
||||
var NewSysUserOnlineImpl = &SysUserOnlineImpl{}
|
||||
|
||||
// SysUserOnlineImpl 在线用户 服务层处理
|
||||
type SysUserOnlineImpl struct{}
|
||||
|
||||
// LoginUserToUserOnline 设置在线用户信息
|
||||
func (r *SysUserOnlineImpl) LoginUserToUserOnline(loginUser vo.LoginUser) model.SysUserOnline {
|
||||
if loginUser.UserID == "" {
|
||||
return model.SysUserOnline{}
|
||||
}
|
||||
|
||||
sysUserOnline := model.SysUserOnline{
|
||||
TokenID: loginUser.UUID,
|
||||
UserName: loginUser.User.UserName,
|
||||
IPAddr: loginUser.IPAddr,
|
||||
LoginLocation: loginUser.LoginLocation,
|
||||
Browser: loginUser.Browser,
|
||||
OS: loginUser.OS,
|
||||
LoginTime: loginUser.LoginTime,
|
||||
}
|
||||
if loginUser.User.DeptID != "" {
|
||||
sysUserOnline.DeptName = loginUser.User.Dept.DeptName
|
||||
}
|
||||
return sysUserOnline
|
||||
}
|
||||
25
src/modules/monitor/service/system_info.go
Normal file
25
src/modules/monitor/service/system_info.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package service
|
||||
|
||||
// ISystemInfo 服务器系统相关信息 服务层接口
|
||||
type ISystemInfo interface {
|
||||
// ProjectInfo 程序项目信息
|
||||
ProjectInfo() map[string]any
|
||||
|
||||
// SystemInfo 系统信息
|
||||
SystemInfo() map[string]any
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
TimeInfo() map[string]string
|
||||
|
||||
// MemoryInfo 内存信息
|
||||
MemoryInfo() map[string]any
|
||||
|
||||
// CPUInfo CPU信息
|
||||
CPUInfo() map[string]any
|
||||
|
||||
// NetworkInfo 网络信息
|
||||
NetworkInfo() map[string]string
|
||||
|
||||
// DiskInfo 磁盘信息
|
||||
DiskInfo() []map[string]string
|
||||
}
|
||||
236
src/modules/monitor/service/system_info.impl.go
Normal file
236
src/modules/monitor/service/system_info.impl.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/config"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
// 实例化服务层 SystemInfoImpl 结构体
|
||||
var NewSystemInfoImpl = &SystemInfoImpl{}
|
||||
|
||||
// SystemInfoImpl 服务器系统相关信息 服务层处理
|
||||
type SystemInfoImpl struct{}
|
||||
|
||||
// ProjectInfo 程序项目信息
|
||||
func (s *SystemInfoImpl) ProjectInfo() map[string]any {
|
||||
// 获取工作目录
|
||||
appDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
appDir = ""
|
||||
}
|
||||
// 项目依赖
|
||||
dependencies := s.dependencies()
|
||||
return map[string]any{
|
||||
"appDir": appDir,
|
||||
"env": config.Env(),
|
||||
"name": config.Get("framework.name"),
|
||||
"version": config.Get("framework.version"),
|
||||
"dependencies": dependencies,
|
||||
}
|
||||
}
|
||||
|
||||
// dependencies 读取mod内项目包依赖
|
||||
func (s *SystemInfoImpl) dependencies() map[string]string {
|
||||
var pkgs = make(map[string]string)
|
||||
|
||||
// 打开 go.mod 文件
|
||||
file, err := os.Open("go.mod")
|
||||
if err != nil {
|
||||
return pkgs
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 使用 bufio.Scanner 逐行读取文件内容
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
// 行不为空,不以module\require开头,不带有 // indirect 注释,则解析包名和版本
|
||||
prefixLine := strings.HasPrefix(line, "module") || strings.HasPrefix(line, "require") || strings.HasPrefix(line, "go ")
|
||||
suffixLine := strings.HasSuffix(line, ")") || strings.HasSuffix(line, "// indirect")
|
||||
if line == "" || prefixLine || suffixLine {
|
||||
continue
|
||||
}
|
||||
|
||||
modInfo := strings.Split(line, " ")
|
||||
if len(modInfo) >= 2 {
|
||||
moduleName := strings.TrimSpace(modInfo[0])
|
||||
version := strings.TrimSpace(modInfo[1])
|
||||
pkgs[moduleName] = version
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
pkgs["scanner-err"] = err.Error()
|
||||
}
|
||||
return pkgs
|
||||
}
|
||||
|
||||
// SystemInfo 系统信息
|
||||
func (s *SystemInfoImpl) SystemInfo() map[string]any {
|
||||
info, err := host.Info()
|
||||
if err != nil {
|
||||
info.Platform = err.Error()
|
||||
}
|
||||
// 用户目录
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = ""
|
||||
}
|
||||
cmd, err := os.Executable()
|
||||
if err != nil {
|
||||
cmd = ""
|
||||
}
|
||||
return map[string]any{
|
||||
"platform": info.Platform,
|
||||
"go": runtime.Version(),
|
||||
"processId": os.Getpid(),
|
||||
"arch": info.KernelArch,
|
||||
"uname": runtime.GOARCH,
|
||||
"release": info.OS,
|
||||
"hostname": info.Hostname,
|
||||
"homeDir": homeDir,
|
||||
"cmd": cmd,
|
||||
"execCommand": strings.Join(os.Args, " "),
|
||||
}
|
||||
}
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
func (s *SystemInfoImpl) TimeInfo() map[string]string {
|
||||
// 获取当前时间
|
||||
current := time.Now().Format("2006-01-02 15:04:05")
|
||||
// 获取程序运行时间
|
||||
uptime := time.Since(config.RunTime()).String()
|
||||
// 获取时区
|
||||
timezone := time.Now().Format("-0700 MST")
|
||||
// 获取时区名称
|
||||
timezoneName := time.Now().Format("MST")
|
||||
|
||||
return map[string]string{
|
||||
"current": current,
|
||||
"uptime": uptime,
|
||||
"timezone": timezone,
|
||||
"timezoneName": timezoneName,
|
||||
}
|
||||
}
|
||||
|
||||
// MemoryInfo 内存信息
|
||||
func (s *SystemInfoImpl) MemoryInfo() map[string]any {
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
memInfo.UsedPercent = 0
|
||||
memInfo.Available = 0
|
||||
memInfo.Total = 0
|
||||
}
|
||||
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
|
||||
return map[string]any{
|
||||
"usage": fmt.Sprintf("%.2f", memInfo.UsedPercent), // 内存利用率
|
||||
"freemem": parse.Bit(float64(memInfo.Available)), // 可用内存大小(GB)
|
||||
"totalmem": parse.Bit(float64(memInfo.Total)), // 总内存大小(GB)
|
||||
"rss": parse.Bit(float64(memStats.Sys)), // 常驻内存大小(RSS)
|
||||
"heapTotal": parse.Bit(float64(memStats.HeapSys)), // 堆总大小
|
||||
"heapUsed": parse.Bit(float64(memStats.HeapAlloc)), // 堆已使用大小
|
||||
"external": parse.Bit(float64(memStats.Sys - memStats.HeapSys)), // 外部内存大小(非堆)
|
||||
}
|
||||
}
|
||||
|
||||
// CPUInfo CPU信息
|
||||
func (s *SystemInfoImpl) CPUInfo() map[string]any {
|
||||
var core int32 = 0
|
||||
var speed string = "未知"
|
||||
var model string = "未知"
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err == nil {
|
||||
core = cpuInfo[0].Cores
|
||||
speed = fmt.Sprintf("%.0fMHz", cpuInfo[0].Mhz)
|
||||
model = strings.TrimSpace(cpuInfo[0].ModelName)
|
||||
}
|
||||
|
||||
useds := []string{}
|
||||
cpuPercent, err := cpu.Percent(0, true)
|
||||
if err == nil {
|
||||
for _, v := range cpuPercent {
|
||||
useds = append(useds, fmt.Sprintf("%.2f", v))
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"model": model,
|
||||
"speed": speed,
|
||||
"core": core,
|
||||
"coreUsed": useds,
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkInfo 网络信息
|
||||
func (s *SystemInfoImpl) NetworkInfo() map[string]string {
|
||||
ipAddrs := make(map[string]string)
|
||||
interfaces, err := net.Interfaces()
|
||||
if err == nil {
|
||||
for _, iface := range interfaces {
|
||||
name := iface.Name
|
||||
if name[len(name)-1] == '0' {
|
||||
name = name[0 : len(name)-1]
|
||||
name = strings.Trim(name, "")
|
||||
}
|
||||
// ignore localhost
|
||||
if name == "lo" {
|
||||
continue
|
||||
}
|
||||
var addrs []string
|
||||
for _, v := range iface.Addrs {
|
||||
prefix := strings.Split(v.Addr, "/")[0]
|
||||
if strings.Contains(prefix, "::") {
|
||||
addrs = append(addrs, fmt.Sprintf("IPv6 %s", prefix))
|
||||
}
|
||||
if strings.Contains(prefix, ".") {
|
||||
addrs = append(addrs, fmt.Sprintf("IPv4 %s", prefix))
|
||||
}
|
||||
}
|
||||
ipAddrs[name] = strings.Join(addrs, " / ")
|
||||
}
|
||||
}
|
||||
return ipAddrs
|
||||
}
|
||||
|
||||
// DiskInfo 磁盘信息
|
||||
func (s *SystemInfoImpl) DiskInfo() []map[string]string {
|
||||
disks := make([]map[string]string, 0)
|
||||
|
||||
partitions, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
return disks
|
||||
}
|
||||
|
||||
for _, partition := range partitions {
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
disks = append(disks, map[string]string{
|
||||
"size": parse.Bit(float64(usage.Total)),
|
||||
"used": parse.Bit(float64(usage.Used)),
|
||||
"avail": parse.Bit(float64(usage.Free)),
|
||||
"pcent": fmt.Sprintf("%.1f%%", usage.UsedPercent),
|
||||
"target": partition.Device,
|
||||
})
|
||||
}
|
||||
return disks
|
||||
}
|
||||
Reference in New Issue
Block a user