fix: shell脚本文件用字符串替换

This commit is contained in:
meilin.huang
2022-05-05 21:26:55 +08:00
parent 8d334cc0e6
commit 483f5b7604
10 changed files with 29 additions and 195 deletions

3
.gitignore vendored
View File

@@ -11,5 +11,8 @@
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out
*.lock
*.sum
*/node_modules/ */node_modules/
**/vendor/ **/vendor/

View File

@@ -12,7 +12,7 @@
"countup.js": "^2.0.7", "countup.js": "^2.0.7",
"cropperjs": "^1.5.11", "cropperjs": "^1.5.11",
"echarts": "^5.3.2", "echarts": "^5.3.2",
"element-plus": "^2.1.10", "element-plus": "^2.1.11",
"@element-plus/icons-vue": "^1.1.3", "@element-plus/icons-vue": "^1.1.3",
"jsonlint": "^1.6.3", "jsonlint": "^1.6.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",

View File

@@ -50,7 +50,7 @@
</el-radio> </el-radio>
</template> </template>
</el-table-column> </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"> <el-table-column prop="ip" label="ip:port" min-width="140">
<template #default="scope"> <template #default="scope">
<el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{ <el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
@@ -58,7 +58,7 @@
}}</el-link> }}</el-link>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column prop="status" label="状态" min-width="60"> <el-table-column prop="status" label="状态" min-width="75">
<template #default="scope"> <template #default="scope">
<el-switch <el-switch
v-auth:disabled="'machine:update'" v-auth:disabled="'machine:update'"

View File

@@ -1,8 +1,8 @@
<template> <template>
<div> <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-row :gutter="20">
<el-col :lg="10" :md="10"> <el-col :lg="12" :md="12">
<el-descriptions size="small" title="基础信息" :column="2" border> <el-descriptions size="small" title="基础信息" :column="2" border>
<template #extra> <template #extra>
<el-link @click="onRefresh" icon="refresh" :underline="false" type="success"></el-link> <el-link @click="onRefresh" icon="refresh" :underline="false" type="success"></el-link>
@@ -23,11 +23,11 @@
</el-descriptions> </el-descriptions>
</el-col> </el-col>
<el-col :lg="7" :md="7"> <el-col :lg="6" :md="6">
<div class="card-item-chart" ref="memRef"></div> <div class="card-item-chart" ref="memRef"></div>
</el-col> </el-col>
<el-col :lg="7" :md="7"> <el-col :lg="6" :md="6">
<div class="card-item-chart" ref="cpuRef"></div> <div class="card-item-chart" ref="cpuRef"></div>
</el-col> </el-col>
</el-row> </el-row>

View File

@@ -613,7 +613,7 @@ echarts@^5.3.2:
tslib "2.3.0" tslib "2.3.0"
zrender "5.3.1" zrender "5.3.1"
element-plus@^2.1.10: element-plus@^2.1.11:
version "2.1.11" version "2.1.11"
resolved "https://registry.npmmirror.com/element-plus/-/element-plus-2.1.11.tgz#6c1be29f5d78ea78720e0dda519960fd0c7d8fde" resolved "https://registry.npmmirror.com/element-plus/-/element-plus-2.1.11.tgz#6c1be29f5d78ea78720e0dda519960fd0c7d8fde"
integrity sha512-s4X0I8s787tv+9UdekBC1g7v42Fj4bucPAmu03EjbgrGrV7BJvkoBGuK52lNfu4yC76bl6Uyjesd5Fu8CMakSw== integrity sha512-s4X0I8s787tv+9UdekBC1g7v42Fj4bucPAmu03EjbgrGrV7BJvkoBGuK52lNfu4yC76bl6Uyjesd5Fu8CMakSw==

View File

@@ -1,54 +1,19 @@
package machine package machine
import ( const StatsShell = `
"io/ioutil" cat /proc/uptime
"mayfly-go/base/biz" echo '-----'
) /bin/hostname -f
echo '-----'
const BasePath = "./devops/infrastructure/machine/shell/" cat /proc/loadavg
echo '-----'
// const MonitorTemp = "cpuRate:{cpuRate}%,memRate:{memRate}%,sysLoad:{sysLoad}\n" cat /proc/meminfo
echo '-----'
// shell文件内容缓存避免每次读取文件 df -B1
var shellCache = make(map[string]string) echo '-----'
/sbin/ip -o addr
// func (c *Cli) GetProcessByName(name string) (*string, error) { echo '-----'
// return c.Run(getShellContent("sys_info")) /bin/cat /proc/net/dev
// } echo '-----'
top -b -n 1 | grep Cpu
// 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
}

View File

@@ -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

View File

@@ -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

View File

@@ -54,7 +54,7 @@ type Stats struct {
} }
func (c *Cli) GetAllStats() *Stats { func (c *Cli) GetAllStats() *Stats {
res, _ := c.Run(getShellContent("stats")) res, _ := c.Run(StatsShell)
infos := strings.Split(*res, "-----") infos := strings.Split(*res, "-----")
stats := new(Stats) stats := new(Stats)
getUptime(infos[0], stats) getUptime(infos[0], stats)

View File

@@ -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"`
// 空闲的idleCPU时间占比
CpuId float32 `json:"cpuId"`
// 等待waitI/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
}