2020-09-01 10:34:11 +08:00
|
|
|
|
package machine
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"io/ioutil"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
"mayfly-go/base/biz"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-11-18 14:40:12 +08:00
|
|
|
|
const BasePath = "./devops/infrastructure/machine/shell/"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
2021-11-18 14:40:12 +08:00
|
|
|
|
// const MonitorTemp = "cpuRate:{cpuRate}%,memRate:{memRate}%,sysLoad:{sysLoad}\n"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
|
|
|
|
|
// shell文件内容缓存,避免每次读取文件
|
|
|
|
|
|
var shellCache = make(map[string]string)
|
|
|
|
|
|
|
2021-11-18 14:40:12 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
// }
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取shell内容
|
|
|
|
|
|
func getShellContent(name string) string {
|
|
|
|
|
|
cacheShell := shellCache[name]
|
|
|
|
|
|
if cacheShell != "" {
|
|
|
|
|
|
return cacheShell
|
|
|
|
|
|
}
|
2021-11-18 14:40:12 +08:00
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
bytes, err := ioutil.ReadFile(BasePath + name + ".sh")
|
2021-03-24 17:18:39 +08:00
|
|
|
|
biz.ErrIsNil(err, "获取shell文件失败")
|
2020-09-01 10:34:11 +08:00
|
|
|
|
shellStr := string(bytes)
|
|
|
|
|
|
shellCache[name] = shellStr
|
|
|
|
|
|
return shellStr
|
|
|
|
|
|
}
|