137 lines
3.0 KiB
Go
137 lines
3.0 KiB
Go
package stat
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
|
|
"strconv"
|
|
"git.loafle.net/overflow/ssh_crawler/util"
|
|
)
|
|
//
|
|
type CPUStat struct {
|
|
//Device,
|
|
//User,
|
|
//Nice,
|
|
//System,
|
|
//Idle,
|
|
//Iowait,
|
|
//Irq,
|
|
//SoftIrq,
|
|
//Steal, // (over 2.6.11)
|
|
//Guest, // (over 2.6.24)
|
|
//GuestNice, //(over 2.6.33)
|
|
//Sum int64
|
|
}
|
|
|
|
//func (cpu CPUStat) Command() string {
|
|
// return "cat /proc/stat"
|
|
//}
|
|
|
|
func (cpu CPUStat) Read(r io.Reader) (map[string]string, error) {
|
|
var (
|
|
//stats = []CPUStat{}
|
|
scanner = bufio.NewScanner(r)
|
|
)
|
|
resMap := make(map[string]string)
|
|
|
|
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 len(parts) <= 10 {
|
|
//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
|
|
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 (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 {
|
|
// var value int64 = 0
|
|
// 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":
|
|
// value = d.User
|
|
// case "cpu.usage.nice":
|
|
// value = d.Nice
|
|
// case "cpu.usage.system":
|
|
// value = d.System
|
|
// case "cpu.usage.idle":
|
|
// value = d.Idle
|
|
// case "cpu.usage.iowait":
|
|
// value = d.Iowait
|
|
// case "cpu.usage.irq":
|
|
// value = d.Irq
|
|
// case "cpu.usage.softirq":
|
|
// value = d.SoftIrq
|
|
// case "cpu.usage.steal":
|
|
// value = d.Steal
|
|
// case "cpu.usage.guest":
|
|
// value = d.Guest
|
|
// case "cpu.usage.gnice":
|
|
// value = d.GuestNice
|
|
// default:
|
|
//
|
|
// }
|
|
// return strconv.FormatInt(value, 10)
|
|
//}
|