123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package apis
- import (
- "fmt"
- "github.com/shirou/gopsutil/host"
- "runtime"
- "strconv"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/go-admin-team/go-admin-core/sdk/api"
- "github.com/go-admin-team/go-admin-core/sdk/pkg"
- _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
- "github.com/shirou/gopsutil/cpu"
- "github.com/shirou/gopsutil/disk"
- "github.com/shirou/gopsutil/mem"
- )
- const (
- B = 1
- KB = 1024 * B
- MB = 1024 * KB
- GB = 1024 * MB
- )
- type ServerMonitor struct {
- api.Api
- }
- //获取相差时间
- func GetHourDiffer(startTime, endTime string) int64 {
- var hour int64
- t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
- t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
- if err == nil && t1.Before(t2) {
- diff := t2.Unix() - t1.Unix() //
- hour = diff / 3600
- return hour
- } else {
- return hour
- }
- }
- // ServerInfo 获取系统信息
- // @Summary 系统信息
- // @Description 获取JSON
- // @Tags 系统信息
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/server-monitor [get]
- // @Security Bearer
- func (e ServerMonitor) ServerInfo(c *gin.Context) {
- e.Context = c
- sysInfo, err := host.Info()
- osDic := make(map[string]interface{}, 0)
- osDic["goOs"] = runtime.GOOS
- osDic["arch"] = runtime.GOARCH
- osDic["mem"] = runtime.MemProfileRate
- osDic["compiler"] = runtime.Compiler
- osDic["version"] = runtime.Version()
- osDic["numGoroutine"] = runtime.NumGoroutine()
- osDic["ip"] = pkg.GetLocaHonst()
- osDic["projectDir"] = pkg.GetCurrentPath()
- osDic["hostName"] = sysInfo.Hostname
- osDic["time"] = time.Now().Format("2006-01-02 15:04:05")
- dis, _ := disk.Usage("/")
- diskTotalGB := int(dis.Total) / GB
- diskFreeGB := int(dis.Free) / GB
- diskDic := make(map[string]interface{}, 0)
- diskDic["total"] = diskTotalGB
- diskDic["free"] = diskFreeGB
- mem, _ := mem.VirtualMemory()
- memUsedMB := int(mem.Used) / GB
- memTotalMB := int(mem.Total) / GB
- memFreeMB := int(mem.Free) / GB
- memUsedPercent := int(mem.UsedPercent)
- memDic := make(map[string]interface{}, 0)
- memDic["total"] = memTotalMB
- memDic["used"] = memUsedMB
- memDic["free"] = memFreeMB
- memDic["usage"] = memUsedPercent
- cpuDic := make(map[string]interface{}, 0)
- cpuDic["cpuInfo"], _ = cpu.Info()
- percent, _ := cpu.Percent(0, false)
- cpuDic["Percent"] = pkg.Round(percent[0], 2)
- cpuDic["cpuNum"], _ = cpu.Counts(false)
- //服务器磁盘信息
- disklist := make([]disk.UsageStat, 0)
- //所有分区
- diskInfo, err := disk.Partitions(true)
- if err == nil {
- for _, p := range diskInfo {
- diskDetail, err := disk.Usage(p.Mountpoint)
- if err == nil {
- diskDetail.UsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", diskDetail.UsedPercent), 64)
- diskDetail.Total = diskDetail.Total / 1024 / 1024
- diskDetail.Used = diskDetail.Used / 1024 / 1024
- diskDetail.Free = diskDetail.Free / 1024 / 1024
- disklist = append(disklist, *diskDetail)
- }
- }
- }
- e.Custom(gin.H{
- "code": 200,
- "os": osDic,
- "mem": memDic,
- "cpu": cpuDic,
- "disk": diskDic,
- "diskList": disklist,
- })
- }
|