Beispiel #1
0
func cpu(output string) (used float64, numOfCore int) {
	o := strings.Split(output, "\n")
	o0 := strings.TrimSuffix(o[0], " CPU)")
	ol0 := strings.Split(o0, "(")
	numOfCore = kmgStrconv.AtoIDefault0(ol0[len(ol0)-1])
	o1 := strings.Split(o[3], " ")
	_used := kmgStrconv.ParseFloat64Default0(o1[len(o1)-1])
	used = (float64(100) - _used) / float64(100)
	used = kmgMath.Float64RoundToRelativePrec(used, 4)
	return
}
Beispiel #2
0
func memory(output string) (used float64, total int) {
	o := strings.Split(output, "\n")
	o0 := strings.TrimPrefix(o[2], "-/+ buffers/cache:")
	o1 := strings.Split(o0, " ")
	usedByte := 0
	free := 0
	for _, s := range o1 {
		if s == "" {
			continue
		}
		if usedByte == 0 {
			usedByte = kmgStrconv.AtoIDefault0(s)
			continue
		}
		free = kmgStrconv.AtoIDefault0(s)
		break
	}
	total = usedByte + free
	used = float64(usedByte) / float64(total)
	used = kmgMath.Float64RoundToRelativePrec(used, 4)
	return
}
Beispiel #3
0
func disk(output string) (used float64, total int) {
	o := strings.Split(output, "\n")
	o0 := strings.Split(o[1], " ")
	after := 0
	for i, s := range o0 {
		if i == 0 {
			continue
		}
		if s == "" {
			continue
		}
		if total == 0 {
			total = kmgStrconv.AtoIDefault0(s)
			continue
		}
		after++
		if after == 3 {
			p := kmgStrconv.ParseFloat64Default0(strings.TrimSuffix(s, "%"))
			used = kmgMath.Float64RoundToRelativePrec(p/float64(100), 4)
			break
		}
	}
	return
}