fixed by crawler config

This commit is contained in:
snoop 2017-11-09 18:04:07 +09:00
parent 192f72ff83
commit 0d81aa25a5
6 changed files with 662 additions and 395 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea/
.vendor/
*.iml
.vscode/

119
config/test.json Normal file
View File

@ -0,0 +1,119 @@
{
"id": "99",
"target": {
"auth": {
"pw": "qwe123",
"id": "snoop"
},
"connection": {
"ip": "192.168.1.215",
"port": "22",
"portType": "tcp",
"ssl": false
}
},
"schedule": {
"interval": "5"
},
"crawler": {
"name": "SSH_CRAWLER",
"container": "go_proxy"
},
"items": [{
"keys": [
{
"metric": "cpu.usage.user",
"key": "user"
},
{
"metric": "cpu.usage.nice",
"key": "nice"
},
{
"metric": "cpu.usage.system",
"key": "system"
},
{
"metric": "cpu.usage.iowait",
"key": "iowait"
},
{
"metric": "cpu.usage.irq",
"key": "irq"
},
{
"metric": "cpu.usage.softirq",
"key": "softirq"
},
{
"metric": "cpu.usage.steal",
"key": "steal"
},
{
"metric": "cpu.usage.guest",
"key": "guest"
},
{
"metric": "cpu.usage.gnice",
"key": "gnice"
}],
"queryInfo": {
"query": "cat /proc/stat",
"extend": {
"mode" : "cpu"
}
},
"mappingInfo": {
"parseDirection": null,
"arrayColumns": null,
"keyColumns": null,
"valueColumn": null
}
},
{
"keys": [{
"metric": "mem.usage.total",
"key": "MemTotal"
},
{
"metric": "mem.usage.free",
"key": "MemFree"
},
{
"metric": "mem.usage.available",
"key": "MemAvailable"
},
{
"metric": "mem.usage.buffers",
"key": "Buffers"
},
{
"metric": "mem.usage.cached",
"key": "Cached"
},
{
"metric": "mem.swap.usage.total",
"key": "SwapTotal"
},
{
"metric": "mem.swap.usage.free",
"key": "SwapFree"
},
{
"metric": "mem.swap.usage.cached",
"key": "SwapCached"
}],
"queryInfo": {
"query": "cat /proc/meminfo",
"extend": {
"mode": "mem"
}
},
"mappingInfo": {
"parseDirection": null,
"arrayColumns": null,
"keyColumns": null,
"valueColumn": null
}
}]
}

View File

@ -3,13 +3,21 @@ package crawler
import ( import (
"bytes" "bytes"
"git.loafle.net/overflow/ssh_crawler/ssh" "git.loafle.net/overflow/ssh_crawler/ssh"
"git.loafle.net/overflow/ssh_crawler/stat"
config "git.loafle.net/overflow/overflow_commons_go/modules/config/model"
configUtil "git.loafle.net/overflow/overflow_commons_go/modules/config/util"
"io"
) )
type Crawler struct { type Crawler struct {
sshCli *ssh.SSHClient sshCli *ssh.SSHClient
} }
type SSHCrawlerModuler interface {
Read(r io.Reader) (map[string]string, error)
}
func New(ip, port, user, pw string, keyFilePath string) (*Crawler, error) { func New(ip, port, user, pw string, keyFilePath string) (*Crawler, error) {
c := &Crawler{} c := &Crawler{}
err := c.connectSSH(ip, port, user, pw, keyFilePath) err := c.connectSSH(ip, port, user, pw, keyFilePath)
@ -29,15 +37,30 @@ func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
return nil return nil
} }
func (c *Crawler) ParseResult(collectMap map[string]string, item config.Item) (map[string]string, error) {
func (c *Crawler) CPUStat(ch chan interface{}, keys []string) { mappingMap := configUtil.KeysToMap(item.Keys)
resMap := make(map[string]string)
for key, value := range mappingMap {
resMap[value] = collectMap[key]
}
return resMap, nil
}
func (c *Crawler) Process(moduler SSHCrawlerModuler, ch chan interface{}, item config.Item) {
go func() { go func() {
cpu := &stat.CPUStat{} b, err := c.sshCli.RunCommand(item.QueryInfo.Query)
b, err := c.sshCli.RunCommand(cpu.Command())
if err != nil { if err != nil {
ch <- err ch <- err
} }
res, err := cpu.Read(bytes.NewReader(b), keys) res, err := moduler.Read(bytes.NewReader(b))
if err != nil {
ch <- err
}
res, err = c.ParseResult(res, item)
if err != nil { if err != nil {
ch <- err ch <- err
} }
@ -45,131 +68,155 @@ func (c *Crawler) CPUStat(ch chan interface{}, keys []string) {
close(ch) close(ch)
}() }()
} }
//
func (c *Crawler) MemStat(ch chan interface{}, keys []string) { //func (c *Crawler) CPUStat(ch chan interface{}, item model.Item) {
go func() { // go func() {
mem := &stat.MemStat{} // cpu := &stat.CPUStat{}
b, err := c.sshCli.RunCommand(mem.Command()) // b, err := c.sshCli.RunCommand(item.QueryInfo.Query)
if err != nil { // if err != nil {
ch <- err // ch <- err
} // }
res, err := mem.Read(bytes.NewReader(b), keys) // res, err := cpu.Read(bytes.NewReader(b))
if err != nil { // if err != nil {
ch <- err // ch <- err
} // }
ch <- res // res, err = c.ParseResult(res, item)
close(ch) // if err != nil {
}() // ch <- err
} // }
// ch <- res
func (c *Crawler) DiskIOStat(ch chan interface{}, keys []string) { // close(ch)
go func() { // }()
diskio := &stat.DiskIOStat{} //}
//
b, err := c.sshCli.RunCommand(diskio.Command()) //func (c *Crawler) MemStat(ch chan interface{}, item model.Item) {
if err != nil { // go func() {
ch <- err // mem := &stat.MemStat{}
} // b, err := c.sshCli.RunCommand(mem.Command())
// if err != nil {
res, err := diskio.Read(bytes.NewReader(b), keys) // ch <- err
if err != nil { // }
ch <- err // res, err := mem.Read(bytes.NewReader(b))
} // if err != nil {
ch <- res // ch <- err
close(ch) // }
}() // res, err = c.ParseResult(res, item)
} // if err != nil {
// ch <- err
func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) { // }
go func() { // ch <- res
diskFree := &stat.DiskFreeStat{} // close(ch)
b, err := c.sshCli.RunCommand(diskFree.Command()) // }()
if err != nil { //}
ch <- err //
} //func (c *Crawler) DiskIOStat(ch chan interface{}, keys []string) {
res, err := diskFree.Read(bytes.NewReader(b), keys) // go func() {
if err != nil { // diskio := &stat.DiskIOStat{}
ch <- err //
} // b, err := c.sshCli.RunCommand(diskio.Command())
ch <- res // if err != nil {
close(ch) // ch <- err
}() // }
} //
// res, err := diskio.Read(bytes.NewReader(b), keys)
func (c *Crawler) DiskIFreeStat(ch chan interface{}, keys []string) { // if err != nil {
go func() { // ch <- err
diskFree := &stat.DiskIFreeStat{} // }
b, err := c.sshCli.RunCommand(diskFree.Command()) // ch <- res
if err != nil { // close(ch)
ch <- err // }()
} //}
res, err := diskFree.Read(bytes.NewReader(b), keys) //
if err != nil { //func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) {
ch <- err // go func() {
} // diskFree := &stat.DiskFreeStat{}
ch <- res // b, err := c.sshCli.RunCommand(diskFree.Command())
close(ch) // if err != nil {
}() // ch <- err
} // }
// res, err := diskFree.Read(bytes.NewReader(b), keys)
// if err != nil {
func (c *Crawler) LoadAvgStat(ch chan interface{}) { // ch <- err
go func() { // }
load := &stat.LoadAvg{} // ch <- res
// close(ch)
b, err := c.sshCli.RunCommand(load.Command()) // }()
if err != nil { //}
ch <- err //
} //func (c *Crawler) DiskIFreeStat(ch chan interface{}, keys []string) {
res, err := load.Read(bytes.NewReader(b)) // go func() {
if err != nil { // diskFree := &stat.DiskIFreeStat{}
ch <- err // b, err := c.sshCli.RunCommand(diskFree.Command())
} // if err != nil {
ch <- res // ch <- err
}() // }
} // res, err := diskFree.Read(bytes.NewReader(b), keys)
// if err != nil {
func (c *Crawler) NetworkStat(ch chan interface{}, keys []string) { // ch <- err
go func() { // }
net := &stat.NetDevStat{} // ch <- res
b, err := c.sshCli.RunCommand(net.Command()) // close(ch)
if err != nil { // }()
ch <- err //}
} //
res, err := net.Read(bytes.NewReader(b), keys) //
if err != nil { //func (c *Crawler) LoadAvgStat(ch chan interface{}) {
ch <- err // go func() {
} // load := &stat.LoadAvg{}
ch <- res //
}() // b, err := c.sshCli.RunCommand(load.Command())
} // if err != nil {
// ch <- err
func (c *Crawler) ProcessStat(ch chan interface{}) { // }
go func() { // res, err := load.Read(bytes.NewReader(b))
ps := &stat.ProcessStat{} // if err != nil {
b, err := c.sshCli.RunCommand(ps.Command()) // ch <- err
if err != nil { // }
ch <- err // ch <- res
} // }()
res, err := ps.Read(bytes.NewReader(b)) //}
if err != nil { //
ch <- err //func (c *Crawler) NetworkStat(ch chan interface{}, keys []string) {
} // go func() {
ch <- res // net := &stat.NetDevStat{}
}() // b, err := c.sshCli.RunCommand(net.Command())
} // if err != nil {
// ch <- err
func (c *Crawler) Uptime(ch chan interface{}) { // }
go func() { // res, err := net.Read(bytes.NewReader(b), keys)
upt := &stat.Uptime{} // if err != nil {
b, err := c.sshCli.RunCommand(upt.Command()) // ch <- err
if err != nil { // }
ch <- err // ch <- res
} // }()
res, err := upt.Read(bytes.NewReader(b)) //}
if err != nil { //
ch <- err //func (c *Crawler) ProcessStat(ch chan interface{}) {
} // go func() {
ch <- res // ps := &stat.ProcessStat{}
}() // b, err := c.sshCli.RunCommand(ps.Command())
} // if err != nil {
// ch <- err
// }
// res, err := ps.Read(bytes.NewReader(b))
// if err != nil {
// ch <- err
// }
// ch <- res
// }()
//}
//
//func (c *Crawler) Uptime(ch chan interface{}) {
// go func() {
// upt := &stat.Uptime{}
// b, err := c.sshCli.RunCommand(upt.Command())
// if err != nil {
// ch <- err
// }
// res, err := upt.Read(bytes.NewReader(b))
// if err != nil {
// ch <- err
// }
// ch <- res
// }()
//}

330
main.go
View File

@ -4,154 +4,216 @@ import (
"fmt" "fmt"
"git.loafle.net/overflow/ssh_crawler/crawler" "git.loafle.net/overflow/ssh_crawler/crawler"
"encoding/json" "encoding/json"
"io/ioutil"
config "git.loafle.net/overflow/overflow_commons_go/modules/config/model"
"log"
"git.loafle.net/overflow/ssh_crawler/stat"
) )
func main() { func main() {
cc := config.Config{}
data, err := ioutil.ReadFile("./config/test.json")
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(data, &cc)
if err != nil {
log.Fatal(err)
}
start(&cc)
}
func start(c *config.Config) {
ip := c.Target.Connection.Ip
port := c.Target.Connection.Port
user := c.Target.Auth["id"].(string)
pw := c.Target.Auth["pw"].(string)
keyFilePathObj := c.Target.Auth["keyFilePath"]
var keyFilePath string = ""
if keyFilePathObj != nil {
keyFilePath = keyFilePathObj.(string)
}
const ip = "192.168.1.209"
const port = "22"
const user = "snoop"
const pw = "qwe123"
const keyFilePath = "" // /home/insanity/.ssh/id_rsa
cr, err := crawler.New(ip, port, user, pw, keyFilePath) cr, err := crawler.New(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
cpukeys := []string{ var inter crawler.SSHCrawlerModuler
"cpu.usage.sum", for _, item := range c.Items {
"cpu.usage.user", mode := item.QueryInfo.Extend["mode"].(string)
"cpu.usage.system",
"cpu.usage.nice", switch mode {
"cpu.usage.idle", case "cpu" :
"cpu.usage.iowait", inter = stat.CPUStat{}
"cpu.usage.irq", break
"cpu.usage.softirq", case "mem" :
"cpu.usage.steal", inter = stat.MemStat{}
"cpu.usage.guest", break
"cpu.usage.gnice", default :
continue
}
ch := make(chan interface{})
cr.Process(inter, ch, item)
print(<-ch)
} }
cpuCh := make(chan interface{}) }
cr.CPUStat(cpuCh, cpukeys)
print(<-cpuCh)
//Memory func main11() {
memkeys := []string {
"mem.usage.total",
"mem.usage.free",
"mem.usage.available",
"mem.usage.buffers",
"mem.usage.cached",
"mem.swap.usage.total",
"mem.swap.usage.free",
"mem.swap.usage.cached",
}
memCh := make(chan interface{})
cr.MemStat(memCh, memkeys)
print(<-memCh)
//const ip = "192.168.1.215"
//const port = "22"
//const user = "snoop"
//const pw = "qwe123"
//const keyFilePath = "" // /home/insanity/.ssh/id_rsa
//
//cr, err := crawler.New(ip, port, user, pw, keyFilePath)
//if err != nil {
// fmt.Println(err)
//}
//
//cpukeys := []string{
// "cpu.usage.sum",
// "cpu.usage.user",
// "cpu.usage.system",
// "cpu.usage.nice",
// "cpu.usage.idle",
// "cpu.usage.iowait",
// "cpu.usage.irq",
// "cpu.usage.softirq",
// "cpu.usage.steal",
// "cpu.usage.guest",
// "cpu.usage.gnice",
//}
//cpuCh := make(chan interface{})
//cr.CPUStat(cpuCh, cpukeys)
//print(<-cpuCh)
//Disk IO ////Memory
ioKeys := []string { //memkeys := []string {
"disk[0].io.device", // "mem.usage.total",
"disk[0].io.readcnt", // "mem.usage.free",
"disk[0].io.merged_readcnt", // "mem.usage.available",
"disk[0].io.readbytes", // "mem.usage.buffers",
"disk[0].io.readtime", // "mem.usage.cached",
"disk[0].io.writecnt", // "mem.swap.usage.total",
"disk[0].io.merged_writecnt", // "mem.swap.usage.free",
"disk[0].io.writebytes", // "mem.swap.usage.cached",
"disk[0].io.writetime", //}
"disk[0].io.iotime", //memCh := make(chan interface{})
"disk[0].io.weighted", //cr.MemStat(memCh, memkeys)
//print(<-memCh)
"disk[1].io.readcnt", //
"disk[1].io.merged_readcnt", //
"disk[1].io.readbytes", ////Disk IO
"disk[1].io.readtime", //ioKeys := []string {
} // "disk[0].io.device",
diskioCh := make(chan interface{}) // "disk[0].io.readcnt",
cr.DiskIOStat(diskioCh, ioKeys) // "disk[0].io.merged_readcnt",
print(<-diskioCh) // "disk[0].io.readbytes",
// "disk[0].io.readtime",
// "disk[0].io.writecnt",
//Disk Free // "disk[0].io.merged_writecnt",
diskFreekeys := []string { // "disk[0].io.writebytes",
"disk[0].usage.fs", // "disk[0].io.writetime",
"disk[0].usage.used", // "disk[0].io.iotime",
"disk[0].usage.available", // "disk[0].io.weighted",
"disk[0].usage.usedperc", //
"disk[0].usage.mounted", // "disk[1].io.readcnt",
// "disk[1].io.merged_readcnt",
"disk[1].usage.fs", // "disk[1].io.readbytes",
"disk[2].usage.fs", // "disk[1].io.readtime",
} //}
diskFreeCh := make(chan interface{}) //diskioCh := make(chan interface{})
cr.DiskFreeStat(diskFreeCh, diskFreekeys) //cr.DiskIOStat(diskioCh, ioKeys)
print(<-diskFreeCh) //print(<-diskioCh)
//
//
//Disk Inode Free ////Disk Free
diskInodeKeys := []string { //diskFreekeys := []string {
"disk[0].inode.usage.fs", // "disk[0].usage.fs",
"disk[0].inode.usage.used", // "disk[0].usage.used",
"disk[0].inode.usage.available", // "disk[0].usage.available",
"disk[0].inode.usage.usedperc", // "disk[0].usage.usedperc",
"disk[0].inode.usage.mounted", // "disk[0].usage.mounted",
//
"disk[1].inode.usage.fs", // "disk[1].usage.fs",
"disk[2].inode.usage.fs", // "disk[2].usage.fs",
} //}
diskInodeFreeCh := make(chan interface{}) //diskFreeCh := make(chan interface{})
cr.DiskIFreeStat(diskInodeFreeCh, diskInodeKeys) //cr.DiskFreeStat(diskFreeCh, diskFreekeys)
print(<-diskInodeFreeCh) //print(<-diskFreeCh)
//
//
////Disk Inode Free
//Network //diskInodeKeys := []string {
netKeys := []string { // "disk[0].inode.usage.fs",
"net[0].transfer.iface", // "disk[0].inode.usage.used",
"net[0].transfer.recv_byte", // "disk[0].inode.usage.available",
"net[0].transfer.recv_packet", // "disk[0].inode.usage.usedperc",
"net[0].transfer.recv_err", // "disk[0].inode.usage.mounted",
"net[0].transfer.recv_drop", //
"net[0].transfer.recv_fifo", // "disk[1].inode.usage.fs",
"net[0].transfer.recv_frame", // "disk[2].inode.usage.fs",
"net[0].transfer.recv_compressed", //}
"net[0].transfer.recv_multicast", //diskInodeFreeCh := make(chan interface{})
"net[0].transfer.send_byte", //cr.DiskIFreeStat(diskInodeFreeCh, diskInodeKeys)
"net[0].transfer.send_packet", //print(<-diskInodeFreeCh)
"net[0].transfer.send_err", //
"net[0].transfer.send_drop", //
"net[0].transfer.send_fifo", //
"net[0].transfer.send_frame", ////Network
"net[0].transfer.send_compressed", //netKeys := []string {
"net[0].transfer.send_multicast", // "net[0].transfer.iface",
// "net[0].transfer.recv_byte",
"net[1].transfer.recv_packet", // "net[0].transfer.recv_packet",
"net[1].transfer.send_compressed", // "net[0].transfer.recv_err",
"net[1].transfer.send_packet", // "net[0].transfer.recv_drop",
} // "net[0].transfer.recv_fifo",
netCh := make(chan interface{}) // "net[0].transfer.recv_frame",
cr.NetworkStat(netCh, netKeys) // "net[0].transfer.recv_compressed",
print(<-netCh) // "net[0].transfer.recv_multicast",
// "net[0].transfer.send_byte",
// "net[0].transfer.send_packet",
//Load Avg. // "net[0].transfer.send_err",
loadCh := make(chan interface{}) // "net[0].transfer.send_drop",
cr.LoadAvgStat(loadCh) // "net[0].transfer.send_fifo",
print(<-loadCh) // "net[0].transfer.send_frame",
// "net[0].transfer.send_compressed",
//Uptime // "net[0].transfer.send_multicast",
uptCh := make(chan interface{}) //
cr.Uptime(uptCh) // "net[1].transfer.recv_packet",
print(<-uptCh) // "net[1].transfer.send_compressed",
// "net[1].transfer.send_packet",
//Process (It doesn't to be a Map. will be displayed by a list) //}
psCh := make(chan interface{}) //netCh := make(chan interface{})
cr.ProcessStat(psCh) //cr.NetworkStat(netCh, netKeys)
print(<-psCh) //print(<-netCh)
//
//
////Load Avg.
//loadCh := make(chan interface{})
//cr.LoadAvgStat(loadCh)
//print(<-loadCh)
//
////Uptime
//uptCh := make(chan interface{})
//cr.Uptime(uptCh)
//print(<-uptCh)
//
////Process (It doesn't to be a Map. will be displayed by a list)
//psCh := make(chan interface{})
//cr.ProcessStat(psCh)
//print(<-psCh)
} }
func print(data interface{}) { func print(data interface{}) {

View File

@ -4,34 +4,36 @@ import (
"bufio" "bufio"
"io" "io"
"strings" "strings"
"git.loafle.net/overflow/ssh_crawler/util"
"strconv"
) )
//
type CPUStat struct { type CPUStat struct {
Device, //Device,
User, //User,
Nice, //Nice,
System, //System,
Idle, //Idle,
Iowait, //Iowait,
Irq, //Irq,
SoftIrq, //SoftIrq,
Steal, // (over 2.6.11) //Steal, // (over 2.6.11)
Guest, // (over 2.6.24) //Guest, // (over 2.6.24)
GuestNice, //(over 2.6.33) //GuestNice, //(over 2.6.33)
Sum int64 //Sum int64
} }
func (cpu CPUStat) Command() string { //func (cpu CPUStat) Command() string {
return "cat /proc/stat" // return "cat /proc/stat"
} //}
func (cpu CPUStat) Read(r io.Reader, keys []string) (*map[string]string, error) { func (cpu CPUStat) Read(r io.Reader) (map[string]string, error) {
var ( var (
stats = []CPUStat{} //stats = []CPUStat{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
) )
resMap := make(map[string]string)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
@ -40,78 +42,94 @@ func (cpu CPUStat) Read(r io.Reader, keys []string) (*map[string]string, error)
continue continue
} }
var steal, guest, guestNice int64 //var steal, guest, guestNice int64
if len(parts) > 8 { //if len(parts) > 8 {
steal = util.StringToInt64(parts[8]) // steal = util.StringToInt64(parts[8])
} //}
if len(parts) > 9 { //if len(parts) > 9 {
guest = util.StringToInt64(parts[9]) // guest = util.StringToInt64(parts[9])
} //}
if len(parts) > 10 { //if len(parts) > 10 {
guestNice = util.StringToInt64(parts[10]) // guestNice = util.StringToInt64(parts[10])
} //}
stats = append(stats, CPUStat{
Device: util.StringToInt64(parts[0]), //stats = append(stats, CPUStat{
User: util.StringToInt64(parts[1]), //Device := util.StringToInt64(parts[0])
Nice: util.StringToInt64(parts[2]), //User := util.StringToInt64(parts[1])
System: util.StringToInt64(parts[3]), //Nice := util.StringToInt64(parts[2])
Idle: util.StringToInt64(parts[4]), //System := util.StringToInt64(parts[3])
Iowait: util.StringToInt64(parts[5]), //Idle := util.StringToInt64(parts[4])
Irq: util.StringToInt64(parts[6]), //Iowait := util.StringToInt64(parts[5])
SoftIrq: util.StringToInt64(parts[7]), //Irq := util.StringToInt64(parts[6])
Steal: steal, //SoftIrq := util.StringToInt64(parts[7])
Guest: guest, //Steal := steal
GuestNice: guestNice, //Guest := guest
}) //GuestNice := guestNice
//})
//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) //res, err := cpu.parse(keys, stats)
if err != nil { //if err != nil {
return nil, err // return nil, err
} //}
return &res, scanner.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)
} }
//
//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)
//}

View File

@ -9,11 +9,11 @@ import (
type MemStat struct { type MemStat struct {
} }
func (mem MemStat) Command() string { //func (mem MemStat) Command() string {
return "cat /proc/meminfo" // return "cat /proc/meminfo"
} //}
func (mem MemStat) Read(r io.Reader, keys []string) (*map[string]string, error) { func (mem MemStat) Read(r io.Reader) (map[string]string, error) {
var ( var (
stats = map[string]string{} stats = map[string]string{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
@ -27,39 +27,39 @@ func (mem MemStat) Read(r io.Reader, keys []string) (*map[string]string, error)
stats[key] = parts[1] stats[key] = parts[1]
} }
res, err := mem.parse(keys, stats) //res, err := mem.parse(keys, stats)
if err != nil { //if err != nil {
return nil, err // return nil, err
} //}
return &res, scanner.Err() return stats, scanner.Err()
}
func (mem MemStat) parse(keys []string, data map[string]string) (map[string]string, error) {
resMap := make(map[string]string)
for _, key := range keys {
switch key {
case "mem.usage.total":
resMap[key] = data["MemTotal"]
case "mem.usage.free":
resMap[key] = data["MemFree"]
case "mem.usage.available":
resMap[key] = data["MemAvailable"]
case "mem.usage.buffers":
resMap[key] = data["Buffers"]
case "mem.usage.cached":
resMap[key] = data["Cached"]
case "mem.swap.usage.total":
resMap[key] = data["SwapTotal"]
case "mem.swap.usage.free":
resMap[key] = data["SwapFree"]
case "mem.swap.usage.cached":
resMap[key] = data["SwapCached"]
default:
}
}
return resMap, nil
} }
//
//func (mem MemStat) parse(keys []string, data map[string]string) (map[string]string, error) {
// resMap := make(map[string]string)
//
// for _, key := range keys {
// switch key {
// case "mem.usage.total":
// resMap[key] = data["MemTotal"]
// case "mem.usage.free":
// resMap[key] = data["MemFree"]
// case "mem.usage.available":
// resMap[key] = data["MemAvailable"]
// case "mem.usage.buffers":
// resMap[key] = data["Buffers"]
// case "mem.usage.cached":
// resMap[key] = data["Cached"]
// case "mem.swap.usage.total":
// resMap[key] = data["SwapTotal"]
// case "mem.swap.usage.free":
// resMap[key] = data["SwapFree"]
// case "mem.swap.usage.cached":
// resMap[key] = data["SwapCached"]
// default:
//
// }
// }
//
// return resMap, nil
//}