68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
package stat
|
|
|
|
import (
|
|
"io"
|
|
"bufio"
|
|
"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"
|
|
}
|
|
|
|
func (cpu CPUStat) Parse(r io.Reader) ([]CPUStat, error) {
|
|
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,
|
|
})
|
|
}
|
|
|
|
return CPUStats, scanner.Err()
|
|
}
|