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"
|
2017-10-24 07:09:17 +00:00
|
|
|
"git.loafle.net/overflow/ssh_crawler/util"
|
2017-11-08 02:43:25 +00:00
|
|
|
"strconv"
|
2017-10-20 06:10:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CPUStat struct {
|
2017-10-24 07:09:17 +00:00
|
|
|
Device,
|
|
|
|
User,
|
|
|
|
Nice,
|
|
|
|
System,
|
|
|
|
Idle,
|
|
|
|
Iowait,
|
|
|
|
Irq,
|
|
|
|
SoftIrq,
|
|
|
|
Steal, // (over 2.6.11)
|
|
|
|
Guest, // (over 2.6.24)
|
|
|
|
GuestNice, //(over 2.6.33)
|
2017-11-08 02:43:25 +00:00
|
|
|
Sum int64
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cpu CPUStat) Command() string {
|
|
|
|
return "cat /proc/stat"
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:09:17 +00:00
|
|
|
func (cpu CPUStat) Read(r io.Reader, keys []string) (*map[string]string, error) {
|
2017-10-20 06:10:30 +00:00
|
|
|
var (
|
2017-10-24 07:09:17 +00:00
|
|
|
stats = []CPUStat{}
|
2017-10-20 06:10:30 +00:00
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if !strings.HasPrefix(parts[0], "cpu") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-08 02:43:25 +00:00
|
|
|
var steal, guest, guestNice int64
|
2017-10-20 06:10:30 +00:00
|
|
|
if len(parts) > 8 {
|
2017-11-08 02:43:25 +00:00
|
|
|
steal = util.StringToInt64(parts[8])
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|
|
|
|
if len(parts) > 9 {
|
2017-11-08 02:43:25 +00:00
|
|
|
guest = util.StringToInt64(parts[9])
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|
|
|
|
if len(parts) > 10 {
|
2017-11-08 02:43:25 +00:00
|
|
|
guestNice = util.StringToInt64(parts[10])
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 07:09:17 +00:00
|
|
|
stats = append(stats, CPUStat{
|
2017-11-08 02:43:25 +00:00
|
|
|
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]),
|
2017-10-24 07:09:17 +00:00
|
|
|
Steal: steal,
|
|
|
|
Guest: guest,
|
|
|
|
GuestNice: guestNice,
|
2017-10-20 06:10:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:23:56 +00:00
|
|
|
res, err := cpu.parse(keys, stats)
|
2017-10-24 07:09:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cpu CPUStat) parse(keys []string, data []CPUStat) (map[string]string, error) {
|
|
|
|
resMap := make(map[string]string)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
resMap[key] = cpu.calc(key, data[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return resMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cpu CPUStat) calc(key string, d CPUStat) string {
|
2017-11-08 02:43:25 +00:00
|
|
|
var value int64 = 0
|
2017-10-24 07:09:17 +00:00
|
|
|
sum := d.User + d.Nice + d.System + d.Idle + d.Iowait + d.Irq + d.SoftIrq + d.Steal + d.Guest + d.GuestNice
|
|
|
|
switch key {
|
|
|
|
case "cpu.usage.sum":
|
|
|
|
value = sum
|
|
|
|
case "cpu.usage.user":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.User
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.nice":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Nice
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.system":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.System
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.idle":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Idle
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.iowait":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Iowait
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.irq":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Irq
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.softirq":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.SoftIrq
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.steal":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Steal
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.guest":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.Guest
|
2017-10-24 07:09:17 +00:00
|
|
|
case "cpu.usage.gnice":
|
2017-11-08 02:43:25 +00:00
|
|
|
value = d.GuestNice
|
2017-10-24 07:09:17 +00:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
2017-11-08 02:43:25 +00:00
|
|
|
return strconv.FormatInt(value, 10)
|
2017-10-20 06:10:30 +00:00
|
|
|
}
|