100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package parser
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type CPUParser struct {
|
|
}
|
|
|
|
func (p *CPUParser) Key() string {
|
|
return "cpu"
|
|
}
|
|
|
|
func (p *CPUParser) Parse(scanner *ParserScanner) (map[string]string, error) {
|
|
resMap := make(map[string]string, 0)
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
parts := strings.Fields(line)
|
|
|
|
if !strings.HasPrefix(parts[0], "cpu") {
|
|
continue
|
|
}
|
|
|
|
//var steal, guest, guestNice int64
|
|
//if len(parts) > 8 {
|
|
// steal = util.StringToInt64(parts[8])
|
|
//}
|
|
//if len(parts) > 9 {
|
|
// guest = util.StringToInt64(parts[9])
|
|
//}
|
|
if 10 >= len(parts) {
|
|
//guestNice = util.StringToInt64(parts[10])
|
|
continue
|
|
}
|
|
|
|
//stats = append(stats, CPUStat{
|
|
//Device := util.StringToInt64(parts[0])
|
|
// User := util.StringToInt64(parts[1])
|
|
// Nice := util.StringToInt64(parts[2])
|
|
// System := util.StringToInt64(parts[3])
|
|
// Idle := util.StringToInt64(parts[4])
|
|
// Iowait := util.StringToInt64(parts[5])
|
|
// Irq := util.StringToInt64(parts[6])
|
|
// SoftIrq := util.StringToInt64(parts[7])
|
|
// Steal := util.StringToInt64(parts[8])
|
|
// Guest := util.StringToInt64(parts[9])
|
|
// GuestNice := util.StringToInt64(parts[10])
|
|
//})
|
|
|
|
// sum := User + Nice + System + Idle + Iowait + Irq + SoftIrq + Steal + Guest + GuestNice
|
|
sum := sumFromStrings(parts, 1, 11, nil)
|
|
|
|
resMap["sum"] = strconv.FormatInt(sum, 10)
|
|
resMap["user"] = parts[1]
|
|
resMap["nice"] = parts[2]
|
|
resMap["system"] = parts[3]
|
|
resMap["idle"] = parts[4]
|
|
resMap["iowait"] = parts[5]
|
|
resMap["irq"] = parts[6]
|
|
resMap["softIrq"] = parts[7]
|
|
resMap["steal"] = parts[8]
|
|
resMap["guest"] = parts[9]
|
|
resMap["gnice"] = parts[10]
|
|
|
|
break // first line only --- cpu
|
|
}
|
|
|
|
//res, err := cpu.parse(keys, stats)
|
|
//if err != nil {
|
|
// return nil, err
|
|
//}
|
|
|
|
return resMap, scanner.Err()
|
|
|
|
}
|
|
|
|
func sumFromStrings(ss []string, startIndex, endIndex int, exclude []int) int64 {
|
|
if nil == ss {
|
|
return 0
|
|
}
|
|
|
|
var result int64
|
|
Loop:
|
|
for i := startIndex; i < endIndex; i++ {
|
|
if nil != exclude {
|
|
for j := 0; j < len(exclude); j++ {
|
|
if exclude[j] == i {
|
|
continue Loop
|
|
}
|
|
}
|
|
}
|
|
i64, _ := strconv.ParseInt(ss[i], 10, 64)
|
|
result += i64
|
|
}
|
|
|
|
return result
|
|
}
|