Skip to content

Commit

Permalink
Merge pull request #598 from go-admin-team/dev
Browse files Browse the repository at this point in the history
 fix🐛: fix monitor macos env error (#605)
  • Loading branch information
wenjianzhang authored Feb 12, 2022
2 parents e2c5075 + 39e26d7 commit 72391f0
Show file tree
Hide file tree
Showing 14 changed files with 525 additions and 87 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PROJECT:=go-admin
.PHONY: build
build:
CGO_ENABLED=0 go build -o go-admin main.go
build-linux:
env GOOS=linux GOARCH=amd64 go build
build-sqlite:
go build -tags sqlite3 -o go-admin main.go
#.PHONY: test
Expand Down
100 changes: 58 additions & 42 deletions app/admin/service/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"errors"
"github.com/go-admin-team/go-admin-core/sdk/config"
"gorm.io/gorm/clause"

"github.com/casbin/casbin/v2"
Expand Down Expand Up @@ -71,14 +72,17 @@ func (e *SysRole) Insert(c *dto.SysRoleInsertReq, cb *casbin.SyncedEnforcer) err
}
c.SysMenu = dataMenu
c.Generate(&data)
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" {
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
}

err = tx.Create(&data).Error
if err != nil {
Expand Down Expand Up @@ -108,14 +112,17 @@ func (e *SysRole) Insert(c *dto.SysRoleInsertReq, cb *casbin.SyncedEnforcer) err
// Update 修改SysRole对象
func (e *SysRole) Update(c *dto.SysRoleUpdateReq, cb *casbin.SyncedEnforcer) error {
var err error
tx := e.Orm.Debug().Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" {
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
}
var model = models.SysRole{}
var mlist = make([]models.SysMenu, 0)
tx.Preload("SysMenu").First(&model, c.GetId())
Expand All @@ -142,7 +149,7 @@ func (e *SysRole) Update(c *dto.SysRoleUpdateReq, cb *casbin.SyncedEnforcer) err
e.Log.Errorf("delete policy error:%s", err)
return err
}
mp:=make(map [string] interface{} ,0)
mp := make(map[string]interface{}, 0)
polices := make([][]string, 0)
for _, menu := range mlist {
for _, api := range menu.SysApi {
Expand All @@ -164,14 +171,17 @@ func (e *SysRole) Update(c *dto.SysRoleUpdateReq, cb *casbin.SyncedEnforcer) err
// Remove 删除SysRole
func (e *SysRole) Remove(c *dto.SysRoleDeleteReq) error {
var err error
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" {
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
}
var model = models.SysRole{}
tx.Preload("SysMenu").Preload("SysDept").First(&model, c.GetId())
db := tx.Select(clause.Associations).Delete(&model)
Expand Down Expand Up @@ -203,14 +213,17 @@ func (e *SysRole) GetRoleMenuId(roleId int) ([]int, error) {

func (e *SysRole) UpdateDataScope(c *dto.RoleDataScopeReq) *SysRole {
var err error
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" {
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
}
var dlist = make([]models.SysDept, 0)
var model = models.SysRole{}
tx.Preload("SysDept").First(&model, c.RoleId)
Expand Down Expand Up @@ -239,14 +252,17 @@ func (e *SysRole) UpdateDataScope(c *dto.RoleDataScopeReq) *SysRole {
// UpdateStatus 修改SysRole对象status
func (e *SysRole) UpdateStatus(c *dto.UpdateStatusReq) error {
var err error
tx := e.Orm.Debug().Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" {
tx := e.Orm.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
}
var model = models.SysRole{}
tx.First(&model, c.GetId())
c.Generate(&model)
Expand Down Expand Up @@ -299,4 +315,4 @@ func (e *SysRole) GetById(roleId int) ([]string, error) {
permissions = append(permissions, l[i].Permission)
}
return permissions, nil
}
}
125 changes: 98 additions & 27 deletions app/other/apis/sys_server_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package apis

import (
"fmt"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/v3/net"
"regexp"
"runtime"
"strconv"
"strings"
"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"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
)

const (
Expand All @@ -23,11 +26,28 @@ const (
GB = 1024 * MB
)

var (
Version string
expectDiskFsTypes = []string{
"apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs",
"fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs", "fuse.rclone",
}
excludeNetInterfaces = []string{
"lo", "tun", "docker", "veth", "br-", "vmbr", "vnet", "kube",
}
getMacDiskNo = regexp.MustCompile(`\/dev\/disk(\d)s.*`)
)

var (
netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdateNetStats uint64
cachedBootTime time.Time
)

type ServerMonitor struct {
api.Api
}

//获取相差时间
// GetHourDiffer 获取相差时间
func GetHourDiffer(startTime, endTime string) int64 {
var hour int64
t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
Expand Down Expand Up @@ -64,33 +84,28 @@ func (e ServerMonitor) ServerInfo(c *gin.Context) {
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
memDic["used"] = mem.Used / MB
memDic["total"] = mem.Total / MB

fmt.Println("mem", int(mem.Total/mem.Used*100))
memDic["percent"] = pkg.Round(mem.UsedPercent, 2)

swapDic := make(map[string]interface{}, 0)
swapDic["used"] = mem.SwapTotal - mem.SwapFree
swapDic["total"] = mem.SwapTotal

cpuDic := make(map[string]interface{}, 0)
cpuDic["cpuInfo"], _ = cpu.Info()
percent, _ := cpu.Percent(0, false)
cpuDic["Percent"] = pkg.Round(percent[0], 2)
cpuDic["percent"] = pkg.Round(percent[0], 2)
cpuDic["cpuNum"], _ = cpu.Counts(false)

//服务器磁盘信息
disklist := make([]disk.UsageStat, 0)
//所有分区
var diskTotal, diskUsed, diskUsedPercent float64
diskInfo, err := disk.Partitions(true)
if err == nil {
for _, p := range diskInfo {
Expand All @@ -101,16 +116,72 @@ func (e ServerMonitor) ServerInfo(c *gin.Context) {
diskDetail.Used = diskDetail.Used / 1024 / 1024
diskDetail.Free = diskDetail.Free / 1024 / 1024
disklist = append(disklist, *diskDetail)

}
}
}

d, _ := disk.Usage("/")

diskTotal = float64(d.Total / GB)
diskUsed = float64(d.Used / GB)
diskUsedPercent, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", d.UsedPercent), 64)

diskDic := make(map[string]interface{}, 0)
diskDic["total"] = diskTotal
diskDic["used"] = diskUsed
diskDic["percent"] = diskUsedPercent

bootTime, _ := host.BootTime()
cachedBootTime = time.Unix(int64(bootTime), 0)

TrackNetworkSpeed()
netDic := make(map[string]interface{}, 0)
netDic["in"] = pkg.Round(float64(netInSpeed/KB), 2)
netDic["out"] = pkg.Round(float64(netOutSpeed/KB), 2)
e.Custom(gin.H{
"code": 200,
"os": osDic,
"mem": memDic,
"cpu": cpuDic,
"disk": diskDic,
"diskList": disklist,
"code": 200,
"os": osDic,
"mem": memDic,
"cpu": cpuDic,
"disk": diskDic,
"net": netDic,
"swap": swapDic,
"location": "Aliyun",
"bootTime": GetHourDiffer(cachedBootTime.Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05")),
})
}

func TrackNetworkSpeed() {
var innerNetInTransfer, innerNetOutTransfer uint64
nc, err := net.IOCounters(true)
if err == nil {
for _, v := range nc {
if isListContainsStr(excludeNetInterfaces, v.Name) {
continue
}
innerNetInTransfer += v.BytesRecv
innerNetOutTransfer += v.BytesSent
}
now := uint64(time.Now().Unix())
diff := now - lastUpdateNetStats
if diff > 0 {
netInSpeed = (innerNetInTransfer - netInTransfer) / diff
fmt.Println("netInSpeed", netInSpeed)
netOutSpeed = (innerNetOutTransfer - netOutTransfer) / diff
fmt.Println("netOutSpeed", netOutSpeed)
}
netInTransfer = innerNetInTransfer
netOutTransfer = innerNetOutTransfer
lastUpdateNetStats = now
}
}

func isListContainsStr(list []string, str string) bool {
for i := 0; i < len(list); i++ {
if strings.Contains(str, list[i]) {
return true
}
}
return false
}
4 changes: 2 additions & 2 deletions cmd/migrate/migration/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (e *Migration) Migrate() {
var err error
var count int64
for _, v := range versions {
err = e.db.Debug().Table("sys_migration").Where("version = ?", v).Count(&count).Error
err = e.db.Table("sys_migration").Where("version = ?", v).Count(&count).Error
if err != nil {
log.Fatalln(err)
}
Expand All @@ -55,7 +55,7 @@ func (e *Migration) Migrate() {
count = 0
continue
}
err = (e.version[v])(e.db.Debug(), strconv.Itoa(v))
err = (e.version[v])(e.db, strconv.Itoa(v))
if err != nil {
log.Fatalln(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/migration/models/initdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ExecSql(db *gorm.DB, filePath string) error {
fmt.Println(sqlList[i])
continue
}
sql := strings.Replace(sqlList[i]+";", "\n", "", 0)
sql := strings.Replace(sqlList[i]+";", "\n", "", -1)
sql = strings.TrimSpace(sql)
if err = db.Exec(sql).Error; err != nil {
log.Printf("error sql: %s", sql)
Expand All @@ -60,4 +60,4 @@ func Ioutil(filePath string) (string, error) {
} else {
return "", err
}
}
}
Loading

0 comments on commit 72391f0

Please sign in to comment.