2017-10-20 06:10:30 +00:00
|
|
|
package stat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2017-10-23 09:08:19 +00:00
|
|
|
"io"
|
2017-10-20 06:10:30 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CPUStat struct {
|
|
|
|
device,
|
|
|
|
user,
|
|
|
|
nice,
|
|
|
|
system,
|
|
|
|
idle,
|
|
|
|
iowait,
|
|
|
|
irq,
|
|
|
|
softIrq,
|
|
|
|
steal, // (over 2.6.11)
|
|
|
|
guest, // (over 2.6.24)
|
|
|
|
guestNice string //(over 2.6.33)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cpu CPUStat) Command() string {
|
|
|
|
return "cat /proc/stat"
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:08:19 +00:00
|
|
|
func (cpu CPUStat) Parse(r io.Reader) (*[]CPUStat, error) {
|
2017-10-20 06:10:30 +00:00
|
|
|
var (
|
|
|
|
CPUStats = []CPUStat{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if !strings.HasPrefix(parts[0], "cpu") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
steal, guest, guestNice := "", "", ""
|
|
|
|
if len(parts) > 8 {
|
|
|
|
steal = parts[8]
|
|
|
|
}
|
|
|
|
if len(parts) > 9 {
|
|
|
|
guest = parts[9]
|
|
|
|
}
|
|
|
|
if len(parts) > 10 {
|
|
|
|
guestNice = parts[10]
|
|
|
|
}
|
|
|
|
|
|
|
|
CPUStats = append(CPUStats, CPUStat{
|
|
|
|
device: parts[0],
|
|
|
|
user: parts[1],
|
|
|
|
nice: parts[2],
|
|
|
|
system: parts[3],
|
|
|
|
idle: parts[4],
|
|
|
|
iowait: parts[5],
|
|
|
|
irq: parts[6],
|
|
|
|
softIrq: parts[7],
|
|
|
|
steal: steal,
|
|
|
|
guest: guest,
|
|
|
|
guestNice: guestNice,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:08:19 +00:00
|
|
|
return &CPUStats, scanner.Err()
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|