func UsagePreInstall() { if kmgCmd.Exist("mpstat") && kmgCmd.Exist("netstat") { return } kmgCmd.MustCombinedOutput("sudo apt-get update") kmgCmd.MustCombinedOutput("sudo apt-get install -y sysstat") }
//获取外网网卡 func FindDeviceNameByIp(ip string) string { o := string(kmgCmd.MustCombinedOutput("ifconfig -s")) lines := strings.Split(o, "\n") for i, l := range lines { if i == 0 { continue } dn := strings.Split(l, " ") deviceName := dn[0] s := string(kmgCmd.MustCombinedOutput("ifconfig " + deviceName)) if strings.Contains(s, ip) { return deviceName } } return "" }
// 仅支持linux ipv4 func MustGetRouteTable() []*RouteRule { if !kmgPlatform.IsLinux() { panic("[MustGetRouteTable] only support linux now") } outputS := kmgCmd.MustCombinedOutput("netstat -nr") lineList := strings.Split(string(outputS), "\n") if len(lineList) < 2 { panic("[getRouteTable] len(lineList)<2 " + string(outputS)) } output := []*RouteRule{} for i, line := range lineList[2:] { line = strings.TrimSpace(line) if line == "" { continue } part := strings.Fields(line) if len(part) < 4 { panic(fmt.Errorf("[getRouteTable] len(part)<4 lineNum:%d content:%s", i, string(outputS))) } thisRule := &RouteRule{ Destination: part[0], Gateway: part[1], Genmask: part[2], Iface: part[len(part)-1], } output = append(output, thisRule) } return output }
func GetDefaultGateway() string { if !kmgPlatform.IsDarwin() { panic("not support platform") } output := kmgCmd.MustCombinedOutput("netstat -nr") for _, line := range strings.Split(string(output), "\n") { if strings.Contains(line, "default") { return strings.Fields(line)[1] } } return "" }
/* // 返回最小变化的提交的名字 // 找到当前文件和commitId里面变化最小的版本 // localCommit 是当前文件的提交 // targetCommit 是需要寻找最小版本的commit 无.git文件恢复 git clone xxx ./tmp1 mv ./tmp1/.git ./ git checkout --orphan current kmg GitSmallestChange -local=current -target=master git checkout xx //返回的那个分支地址 */ func (repo *Repository) MustSmallestChange(localCommit string, targetCommit string) string { commitList := repo.MustGetAllParentCommitId(targetCommit) minNum := 2 << 31 minCommit := "" lineBreak := []byte("\n") for _, commitName := range commitList { output := kmgCmd.MustCombinedOutput("git diff " + localCommit + " " + commitName) num := bytes.Count(output, lineBreak) if minNum > num { minNum = num minCommit = commitName if num == 0 { break } } } return minCommit }
func MustGetIptableRuleList() []IptableRule { content := kmgCmd.MustCombinedOutput("iptables-save") return parseIptableSave(string(content)) }
//只返回 / 挂载的磁盘空间 //total 1024byte 的默认单位,没有选项改成byte func Disk() (used float64, total int) { return disk(string(kmgCmd.MustCombinedOutput("df"))) }
func Cpu() (used float64, numOfCore int) { return cpu(string(kmgCmd.MustCombinedOutput("mpstat"))) }
//byte func Memory() (used float64, total int) { return memory(string(kmgCmd.MustCombinedOutput("free -b"))) }
//byte func NetworkRXTX(deviceName string) (rx int, tx int) { return networkRXTX(string(kmgCmd.MustCombinedOutput("ifconfig " + deviceName))) }
//是否是root,此处只返回是否,其他错误抛panic // TODO 名字比较费解. func MustIsRoot() bool { return bytes.Equal(kmgCmd.MustCombinedOutput("whoami"), []byte("root\n")) }