mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 23:40:24 +08:00
fix: shell脚本文件用字符串替换
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -11,5 +11,8 @@
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
*.lock
|
||||
*.sum
|
||||
|
||||
*/node_modules/
|
||||
**/vendor/
|
||||
@@ -12,7 +12,7 @@
|
||||
"countup.js": "^2.0.7",
|
||||
"cropperjs": "^1.5.11",
|
||||
"echarts": "^5.3.2",
|
||||
"element-plus": "^2.1.10",
|
||||
"element-plus": "^2.1.11",
|
||||
"@element-plus/icons-vue": "^1.1.3",
|
||||
"jsonlint": "^1.6.3",
|
||||
"lodash": "^4.17.21",
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
</el-radio>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="name" label="名称" min-width="130" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="ip" label="ip:port" min-width="140">
|
||||
<template #default="scope">
|
||||
<el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
|
||||
@@ -58,7 +58,7 @@
|
||||
}}</el-link>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="status" label="状态" min-width="60">
|
||||
<el-table-column prop="status" label="状态" min-width="75">
|
||||
<template #default="scope">
|
||||
<el-switch
|
||||
v-auth:disabled="'machine:update'"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="true" :destroy-on-close="true" :before-close="cancel" width="980px">
|
||||
<el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="true" :destroy-on-close="true" :before-close="cancel" width="1050px">
|
||||
<el-row :gutter="20">
|
||||
<el-col :lg="10" :md="10">
|
||||
<el-col :lg="12" :md="12">
|
||||
<el-descriptions size="small" title="基础信息" :column="2" border>
|
||||
<template #extra>
|
||||
<el-link @click="onRefresh" icon="refresh" :underline="false" type="success"></el-link>
|
||||
@@ -23,11 +23,11 @@
|
||||
</el-descriptions>
|
||||
</el-col>
|
||||
|
||||
<el-col :lg="7" :md="7">
|
||||
<el-col :lg="6" :md="6">
|
||||
<div class="card-item-chart" ref="memRef"></div>
|
||||
</el-col>
|
||||
|
||||
<el-col :lg="7" :md="7">
|
||||
<el-col :lg="6" :md="6">
|
||||
<div class="card-item-chart" ref="cpuRef"></div>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
@@ -613,7 +613,7 @@ echarts@^5.3.2:
|
||||
tslib "2.3.0"
|
||||
zrender "5.3.1"
|
||||
|
||||
element-plus@^2.1.10:
|
||||
element-plus@^2.1.11:
|
||||
version "2.1.11"
|
||||
resolved "https://registry.npmmirror.com/element-plus/-/element-plus-2.1.11.tgz#6c1be29f5d78ea78720e0dda519960fd0c7d8fde"
|
||||
integrity sha512-s4X0I8s787tv+9UdekBC1g7v42Fj4bucPAmu03EjbgrGrV7BJvkoBGuK52lNfu4yC76bl6Uyjesd5Fu8CMakSw==
|
||||
|
||||
@@ -1,54 +1,19 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"mayfly-go/base/biz"
|
||||
)
|
||||
|
||||
const BasePath = "./devops/infrastructure/machine/shell/"
|
||||
|
||||
// const MonitorTemp = "cpuRate:{cpuRate}%,memRate:{memRate}%,sysLoad:{sysLoad}\n"
|
||||
|
||||
// shell文件内容缓存,避免每次读取文件
|
||||
var shellCache = make(map[string]string)
|
||||
|
||||
// func (c *Cli) GetProcessByName(name string) (*string, error) {
|
||||
// return c.Run(getShellContent("sys_info"))
|
||||
// }
|
||||
|
||||
// func (c *Cli) GetSystemInfo() (*string, error) {
|
||||
// return c.Run(getShellContent("system_info"))
|
||||
// }
|
||||
|
||||
// func (c *Cli) GetMonitorInfo() *entity.MachineMonitor {
|
||||
// mm := new(entity.MachineMonitor)
|
||||
// res, _ := c.Run(getShellContent("monitor"))
|
||||
// if res == nil {
|
||||
// return nil
|
||||
// }
|
||||
// resMap := make(map[string]interface{})
|
||||
// utils.ReverStrTemplate(MonitorTemp, *res, resMap)
|
||||
|
||||
// err := utils.Map2Struct(resMap, mm)
|
||||
// if err != nil {
|
||||
// global.Log.Error("解析machine monitor: %s", err.Error())
|
||||
// return nil
|
||||
// }
|
||||
// mm.MachineId = c.machine.Id
|
||||
// mm.CreateTime = time.Now()
|
||||
// return mm
|
||||
// }
|
||||
|
||||
// 获取shell内容
|
||||
func getShellContent(name string) string {
|
||||
cacheShell := shellCache[name]
|
||||
if cacheShell != "" {
|
||||
return cacheShell
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadFile(BasePath + name + ".sh")
|
||||
biz.ErrIsNil(err, "获取shell文件失败")
|
||||
shellStr := string(bytes)
|
||||
shellCache[name] = shellStr
|
||||
return shellStr
|
||||
}
|
||||
const StatsShell = `
|
||||
cat /proc/uptime
|
||||
echo '-----'
|
||||
/bin/hostname -f
|
||||
echo '-----'
|
||||
cat /proc/loadavg
|
||||
echo '-----'
|
||||
cat /proc/meminfo
|
||||
echo '-----'
|
||||
df -B1
|
||||
echo '-----'
|
||||
/sbin/ip -o addr
|
||||
echo '-----'
|
||||
/bin/cat /proc/net/dev
|
||||
echo '-----'
|
||||
top -b -n 1 | grep Cpu
|
||||
`
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
|
||||
|
||||
# 获取监控信息
|
||||
function get_monitor_info() {
|
||||
cpu_rate=$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
|
||||
mem_rate=$(free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}')
|
||||
sys_load=$(uptime | cut -d: -f5)
|
||||
cat <<EOF | column -t
|
||||
cpuRate:${cpu_rate},memRate:${mem_rate},sysLoad:${sys_load}
|
||||
EOF
|
||||
}
|
||||
|
||||
get_monitor_info
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
cat /proc/uptime
|
||||
echo '-----'
|
||||
/bin/hostname -f
|
||||
echo '-----'
|
||||
cat /proc/loadavg
|
||||
echo '-----'
|
||||
cat /proc/meminfo
|
||||
echo '-----'
|
||||
df -B1
|
||||
echo '-----'
|
||||
/sbin/ip -o addr
|
||||
echo '-----'
|
||||
/bin/cat /proc/net/dev
|
||||
echo '-----'
|
||||
top -b -n 1 | grep Cpu
|
||||
@@ -54,7 +54,7 @@ type Stats struct {
|
||||
}
|
||||
|
||||
func (c *Cli) GetAllStats() *Stats {
|
||||
res, _ := c.Run(getShellContent("stats"))
|
||||
res, _ := c.Run(StatsShell)
|
||||
infos := strings.Split(*res, "-----")
|
||||
stats := new(Stats)
|
||||
getUptime(infos[0], stats)
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"mayfly-go/base/biz"
|
||||
"mayfly-go/base/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SystemVersion struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
func (c *Cli) GetSystemVersion() *SystemVersion {
|
||||
res, _ := c.Run("cat /etc/redhat-release")
|
||||
return &SystemVersion{
|
||||
Version: *res,
|
||||
}
|
||||
}
|
||||
|
||||
//top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05
|
||||
//Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie
|
||||
//%Cpu(s): 6.2 us, 0.0 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
//KiB Mem : 1882012 total, 73892 free, 770360 used, 1037760 buff/cache
|
||||
//KiB Swap: 0 total, 0 free, 0 used. 933492 avail Mem
|
||||
type Top struct {
|
||||
Time string `json:"time"`
|
||||
// 从本次开机到现在经过的时间
|
||||
Up string `json:"up"`
|
||||
// 当前有几个用户登录到该机器
|
||||
NowUsers int `json:"nowUsers"`
|
||||
// load average: 0.03, 0.04, 0.05 (系统1分钟、5分钟、15分钟内的平均负载值)
|
||||
OneMinLoadavg float32 `json:"oneMinLoadavg"`
|
||||
FiveMinLoadavg float32 `json:"fiveMinLoadavg"`
|
||||
FifteenMinLoadavg float32 `json:"fifteenMinLoadavg"`
|
||||
// 进程总数
|
||||
TotalTask int `json:"totalTask"`
|
||||
// 正在运行的进程数,对应状态TASK_RUNNING
|
||||
RunningTask int `json:"runningTask"`
|
||||
SleepingTask int `json:"sleepingTask"`
|
||||
StoppedTask int `json:"stoppedTask"`
|
||||
ZombieTask int `json:"zombieTask"`
|
||||
// 进程在用户空间(user)消耗的CPU时间占比,不包含调整过优先级的进程
|
||||
CpuUs float32 `json:"cpuUs"`
|
||||
// 进程在内核空间(system)消耗的CPU时间占比
|
||||
CpuSy float32 `json:"cpuSy"`
|
||||
// 调整过用户态优先级的(niced)进程的CPU时间占比
|
||||
CpuNi float32 `json:"cpuNi"`
|
||||
// 空闲的(idle)CPU时间占比
|
||||
CpuId float32 `json:"cpuId"`
|
||||
// 等待(wait)I/O完成的CPU时间占比
|
||||
CpuWa float32 `json:"cpuWa"`
|
||||
// 处理硬中断(hardware interrupt)的CPU时间占比
|
||||
CpuHi float32 `json:"cpuHi"`
|
||||
// 处理硬中断(hardware interrupt)的CPU时间占比
|
||||
CpuSi float32 `json:"cpuSi"`
|
||||
// 当Linux系统是在虚拟机中运行时,等待CPU资源的时间(steal time)占比
|
||||
CpuSt float32 `json:"cpuSt"`
|
||||
|
||||
TotalMem int `json:"totalMem"`
|
||||
FreeMem int `json:"freeMem"`
|
||||
UsedMem int `json:"usedMem"`
|
||||
CacheMem int `json:"cacheMem"`
|
||||
|
||||
TotalSwap int `json:"totalSwap"`
|
||||
FreeSwap int `json:"freeSwap"`
|
||||
UsedSwap int `json:"usedSwap"`
|
||||
AvailMem int `json:"availMem"`
|
||||
}
|
||||
|
||||
func (c *Cli) GetTop() *Top {
|
||||
res, _ := c.Run("top -b -n 1 | head -5")
|
||||
topTemp := "top - {upAndUsers}, load average: {loadavg}\n" +
|
||||
"Tasks:{totalTask} total,{runningTask} running,{sleepingTask} sleeping,{stoppedTask} stopped,{zombieTask} zombie\n" +
|
||||
"%Cpu(s):{cpuUs} us,{cpuSy} sy,{cpuNi} ni,{cpuId} id,{cpuWa} wa,{cpuHi} hi,{cpuSi} si,{cpuSt} st\n" +
|
||||
"KiB Mem :{totalMem} total,{freeMem} free,{usedMem} used,{cacheMem} buff/cache\n" +
|
||||
"KiB Swap:{totalSwap} total,{freeSwap} free,{usedSwap} used. {availMem} avail Mem \n"
|
||||
resMap := make(map[string]interface{})
|
||||
utils.ReverStrTemplate(topTemp, *res, resMap)
|
||||
|
||||
//17:14:07 up 5 days, 6:30, 2
|
||||
timeUpAndUserStr := resMap["upAndUsers"].(string)
|
||||
timeUpAndUser := strings.Split(timeUpAndUserStr, "up")
|
||||
time := utils.StrTrim(timeUpAndUser[0])
|
||||
upAndUsers := strings.Split(timeUpAndUser[1], ",")
|
||||
up := utils.StrTrim(upAndUsers[0]) + upAndUsers[1]
|
||||
users, _ := strconv.Atoi(utils.StrTrim(strings.Split(utils.StrTrim(upAndUsers[2]), " ")[0]))
|
||||
// 0.03, 0.04, 0.05
|
||||
loadavgs := strings.Split(resMap["loadavg"].(string), ",")
|
||||
oneMinLa, _ := strconv.ParseFloat(loadavgs[0], 32)
|
||||
fiveMinLa, _ := strconv.ParseFloat(utils.StrTrim(loadavgs[1]), 32)
|
||||
fifMinLa, _ := strconv.ParseFloat(utils.StrTrim(loadavgs[2]), 32)
|
||||
|
||||
top := &Top{Time: time, Up: up, NowUsers: users, OneMinLoadavg: float32(oneMinLa), FiveMinLoadavg: float32(fiveMinLa), FifteenMinLoadavg: float32(fifMinLa)}
|
||||
err := utils.Map2Struct(resMap, top)
|
||||
biz.ErrIsNil(err, "解析top出错")
|
||||
return top
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
// 系统版本
|
||||
SysVersion SystemVersion
|
||||
// top信息
|
||||
Top Top
|
||||
}
|
||||
Reference in New Issue
Block a user