diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..793b14f --- /dev/null +++ b/.gitignore @@ -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/ \ No newline at end of file diff --git a/config/test.json b/config/test.json new file mode 100644 index 0000000..20ca93c --- /dev/null +++ b/config/test.json @@ -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 + } + }] +} \ No newline at end of file diff --git a/crawler/crawler.go b/crawler/crawler.go index 75ec60a..6b6aca8 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -3,13 +3,21 @@ package crawler import ( "bytes" "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 { 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) { c := &Crawler{} 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 } +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() { - cpu := &stat.CPUStat{} - b, err := c.sshCli.RunCommand(cpu.Command()) + b, err := c.sshCli.RunCommand(item.QueryInfo.Query) if err != nil { 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 { ch <- err } @@ -45,131 +68,155 @@ func (c *Crawler) CPUStat(ch chan interface{}, keys []string) { close(ch) }() } - -func (c *Crawler) MemStat(ch chan interface{}, keys []string) { - go func() { - mem := &stat.MemStat{} - b, err := c.sshCli.RunCommand(mem.Command()) - if err != nil { - ch <- err - } - res, err := mem.Read(bytes.NewReader(b), keys) - if err != nil { - ch <- err - } - ch <- res - close(ch) - }() -} - -func (c *Crawler) DiskIOStat(ch chan interface{}, keys []string) { - go func() { - diskio := &stat.DiskIOStat{} - - b, err := c.sshCli.RunCommand(diskio.Command()) - if err != nil { - ch <- err - } - - res, err := diskio.Read(bytes.NewReader(b), keys) - if err != nil { - ch <- err - } - ch <- res - close(ch) - }() -} - -func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) { - go func() { - diskFree := &stat.DiskFreeStat{} - b, err := c.sshCli.RunCommand(diskFree.Command()) - if err != nil { - ch <- err - } - res, err := diskFree.Read(bytes.NewReader(b), keys) - if err != nil { - ch <- err - } - ch <- res - close(ch) - }() -} - -func (c *Crawler) DiskIFreeStat(ch chan interface{}, keys []string) { - go func() { - diskFree := &stat.DiskIFreeStat{} - b, err := c.sshCli.RunCommand(diskFree.Command()) - if err != nil { - ch <- err - } - res, err := diskFree.Read(bytes.NewReader(b), keys) - if err != nil { - ch <- err - } - ch <- res - close(ch) - }() -} - - -func (c *Crawler) LoadAvgStat(ch chan interface{}) { - go func() { - load := &stat.LoadAvg{} - - b, err := c.sshCli.RunCommand(load.Command()) - if err != nil { - ch <- err - } - res, err := load.Read(bytes.NewReader(b)) - if err != nil { - ch <- err - } - ch <- res - }() -} - -func (c *Crawler) NetworkStat(ch chan interface{}, keys []string) { - go func() { - net := &stat.NetDevStat{} - b, err := c.sshCli.RunCommand(net.Command()) - if err != nil { - ch <- err - } - res, err := net.Read(bytes.NewReader(b), keys) - if err != nil { - ch <- err - } - ch <- res - }() -} - -func (c *Crawler) ProcessStat(ch chan interface{}) { - go func() { - 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 - }() -} +// +//func (c *Crawler) CPUStat(ch chan interface{}, item model.Item) { +// go func() { +// cpu := &stat.CPUStat{} +// b, err := c.sshCli.RunCommand(item.QueryInfo.Query) +// if err != nil { +// ch <- err +// } +// res, err := cpu.Read(bytes.NewReader(b)) +// if err != nil { +// ch <- err +// } +// res, err = c.ParseResult(res, item) +// if err != nil { +// ch <- err +// } +// ch <- res +// close(ch) +// }() +//} +// +//func (c *Crawler) MemStat(ch chan interface{}, item model.Item) { +// go func() { +// mem := &stat.MemStat{} +// b, err := c.sshCli.RunCommand(mem.Command()) +// if err != nil { +// ch <- err +// } +// res, err := mem.Read(bytes.NewReader(b)) +// if err != nil { +// ch <- err +// } +// res, err = c.ParseResult(res, item) +// if err != nil { +// ch <- err +// } +// ch <- res +// close(ch) +// }() +//} +// +//func (c *Crawler) DiskIOStat(ch chan interface{}, keys []string) { +// go func() { +// diskio := &stat.DiskIOStat{} +// +// b, err := c.sshCli.RunCommand(diskio.Command()) +// if err != nil { +// ch <- err +// } +// +// res, err := diskio.Read(bytes.NewReader(b), keys) +// if err != nil { +// ch <- err +// } +// ch <- res +// close(ch) +// }() +//} +// +//func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) { +// go func() { +// diskFree := &stat.DiskFreeStat{} +// b, err := c.sshCli.RunCommand(diskFree.Command()) +// if err != nil { +// ch <- err +// } +// res, err := diskFree.Read(bytes.NewReader(b), keys) +// if err != nil { +// ch <- err +// } +// ch <- res +// close(ch) +// }() +//} +// +//func (c *Crawler) DiskIFreeStat(ch chan interface{}, keys []string) { +// go func() { +// diskFree := &stat.DiskIFreeStat{} +// b, err := c.sshCli.RunCommand(diskFree.Command()) +// if err != nil { +// ch <- err +// } +// res, err := diskFree.Read(bytes.NewReader(b), keys) +// if err != nil { +// ch <- err +// } +// ch <- res +// close(ch) +// }() +//} +// +// +//func (c *Crawler) LoadAvgStat(ch chan interface{}) { +// go func() { +// load := &stat.LoadAvg{} +// +// b, err := c.sshCli.RunCommand(load.Command()) +// if err != nil { +// ch <- err +// } +// res, err := load.Read(bytes.NewReader(b)) +// if err != nil { +// ch <- err +// } +// ch <- res +// }() +//} +// +//func (c *Crawler) NetworkStat(ch chan interface{}, keys []string) { +// go func() { +// net := &stat.NetDevStat{} +// b, err := c.sshCli.RunCommand(net.Command()) +// if err != nil { +// ch <- err +// } +// res, err := net.Read(bytes.NewReader(b), keys) +// if err != nil { +// ch <- err +// } +// ch <- res +// }() +//} +// +//func (c *Crawler) ProcessStat(ch chan interface{}) { +// go func() { +// 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 +// }() +//} diff --git a/main.go b/main.go index 03c69f5..dfdd70c 100644 --- a/main.go +++ b/main.go @@ -4,154 +4,216 @@ import ( "fmt" "git.loafle.net/overflow/ssh_crawler/crawler" "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() { + 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) + 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", + var inter crawler.SSHCrawlerModuler + for _, item := range c.Items { + mode := item.QueryInfo.Extend["mode"].(string) + + switch mode { + case "cpu" : + inter = stat.CPUStat{} + break + case "mem" : + inter = stat.MemStat{} + break + default : + continue + } + + ch := make(chan interface{}) + cr.Process(inter, ch, item) + print(<-ch) + } - cpuCh := make(chan interface{}) - cr.CPUStat(cpuCh, cpukeys) - print(<-cpuCh) +} - //Memory - 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) +func main11() { + //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 - ioKeys := []string { - "disk[0].io.device", - "disk[0].io.readcnt", - "disk[0].io.merged_readcnt", - "disk[0].io.readbytes", - "disk[0].io.readtime", - "disk[0].io.writecnt", - "disk[0].io.merged_writecnt", - "disk[0].io.writebytes", - "disk[0].io.writetime", - "disk[0].io.iotime", - "disk[0].io.weighted", - - "disk[1].io.readcnt", - "disk[1].io.merged_readcnt", - "disk[1].io.readbytes", - "disk[1].io.readtime", - } - diskioCh := make(chan interface{}) - cr.DiskIOStat(diskioCh, ioKeys) - print(<-diskioCh) - - - //Disk Free - diskFreekeys := []string { - "disk[0].usage.fs", - "disk[0].usage.used", - "disk[0].usage.available", - "disk[0].usage.usedperc", - "disk[0].usage.mounted", - - "disk[1].usage.fs", - "disk[2].usage.fs", - } - diskFreeCh := make(chan interface{}) - cr.DiskFreeStat(diskFreeCh, diskFreekeys) - print(<-diskFreeCh) - - - //Disk Inode Free - diskInodeKeys := []string { - "disk[0].inode.usage.fs", - "disk[0].inode.usage.used", - "disk[0].inode.usage.available", - "disk[0].inode.usage.usedperc", - "disk[0].inode.usage.mounted", - - "disk[1].inode.usage.fs", - "disk[2].inode.usage.fs", - } - diskInodeFreeCh := make(chan interface{}) - cr.DiskIFreeStat(diskInodeFreeCh, diskInodeKeys) - print(<-diskInodeFreeCh) - - - - //Network - netKeys := []string { - "net[0].transfer.iface", - "net[0].transfer.recv_byte", - "net[0].transfer.recv_packet", - "net[0].transfer.recv_err", - "net[0].transfer.recv_drop", - "net[0].transfer.recv_fifo", - "net[0].transfer.recv_frame", - "net[0].transfer.recv_compressed", - "net[0].transfer.recv_multicast", - "net[0].transfer.send_byte", - "net[0].transfer.send_packet", - "net[0].transfer.send_err", - "net[0].transfer.send_drop", - "net[0].transfer.send_fifo", - "net[0].transfer.send_frame", - "net[0].transfer.send_compressed", - "net[0].transfer.send_multicast", - - "net[1].transfer.recv_packet", - "net[1].transfer.send_compressed", - "net[1].transfer.send_packet", - } - netCh := make(chan interface{}) - cr.NetworkStat(netCh, netKeys) - 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) + ////Memory + //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) + // + // + ////Disk IO + //ioKeys := []string { + // "disk[0].io.device", + // "disk[0].io.readcnt", + // "disk[0].io.merged_readcnt", + // "disk[0].io.readbytes", + // "disk[0].io.readtime", + // "disk[0].io.writecnt", + // "disk[0].io.merged_writecnt", + // "disk[0].io.writebytes", + // "disk[0].io.writetime", + // "disk[0].io.iotime", + // "disk[0].io.weighted", + // + // "disk[1].io.readcnt", + // "disk[1].io.merged_readcnt", + // "disk[1].io.readbytes", + // "disk[1].io.readtime", + //} + //diskioCh := make(chan interface{}) + //cr.DiskIOStat(diskioCh, ioKeys) + //print(<-diskioCh) + // + // + ////Disk Free + //diskFreekeys := []string { + // "disk[0].usage.fs", + // "disk[0].usage.used", + // "disk[0].usage.available", + // "disk[0].usage.usedperc", + // "disk[0].usage.mounted", + // + // "disk[1].usage.fs", + // "disk[2].usage.fs", + //} + //diskFreeCh := make(chan interface{}) + //cr.DiskFreeStat(diskFreeCh, diskFreekeys) + //print(<-diskFreeCh) + // + // + ////Disk Inode Free + //diskInodeKeys := []string { + // "disk[0].inode.usage.fs", + // "disk[0].inode.usage.used", + // "disk[0].inode.usage.available", + // "disk[0].inode.usage.usedperc", + // "disk[0].inode.usage.mounted", + // + // "disk[1].inode.usage.fs", + // "disk[2].inode.usage.fs", + //} + //diskInodeFreeCh := make(chan interface{}) + //cr.DiskIFreeStat(diskInodeFreeCh, diskInodeKeys) + //print(<-diskInodeFreeCh) + // + // + // + ////Network + //netKeys := []string { + // "net[0].transfer.iface", + // "net[0].transfer.recv_byte", + // "net[0].transfer.recv_packet", + // "net[0].transfer.recv_err", + // "net[0].transfer.recv_drop", + // "net[0].transfer.recv_fifo", + // "net[0].transfer.recv_frame", + // "net[0].transfer.recv_compressed", + // "net[0].transfer.recv_multicast", + // "net[0].transfer.send_byte", + // "net[0].transfer.send_packet", + // "net[0].transfer.send_err", + // "net[0].transfer.send_drop", + // "net[0].transfer.send_fifo", + // "net[0].transfer.send_frame", + // "net[0].transfer.send_compressed", + // "net[0].transfer.send_multicast", + // + // "net[1].transfer.recv_packet", + // "net[1].transfer.send_compressed", + // "net[1].transfer.send_packet", + //} + //netCh := make(chan interface{}) + //cr.NetworkStat(netCh, netKeys) + //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{}) { diff --git a/stat/cpu.go b/stat/cpu.go index f4b5f4a..2996fd4 100644 --- a/stat/cpu.go +++ b/stat/cpu.go @@ -4,34 +4,36 @@ import ( "bufio" "io" "strings" - "git.loafle.net/overflow/ssh_crawler/util" - "strconv" + + + ) - +// 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 + //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) Command() string { +// 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 ( - stats = []CPUStat{} + //stats = []CPUStat{} scanner = bufio.NewScanner(r) ) + resMap := make(map[string]string) for scanner.Scan() { line := scanner.Text() @@ -40,78 +42,94 @@ func (cpu CPUStat) Read(r io.Reader, keys []string) (*map[string]string, error) 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]) - } + //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]) + //} - 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: steal, - Guest: guest, - GuestNice: guestNice, - }) + + //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 := steal + //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) - if err != nil { - return nil, err - } + //res, err := cpu.parse(keys, stats) + //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 { - 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) + 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) +//} diff --git a/stat/mem.go b/stat/mem.go index 0a36f0a..2d9f851 100644 --- a/stat/mem.go +++ b/stat/mem.go @@ -9,11 +9,11 @@ import ( type MemStat struct { } -func (mem MemStat) Command() string { - return "cat /proc/meminfo" -} +//func (mem MemStat) Command() string { +// 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 ( stats = map[string]string{} 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] } - res, err := mem.parse(keys, stats) - if err != nil { - return nil, err - } + //res, err := mem.parse(keys, stats) + //if err != nil { + // return nil, err + //} - return &res, 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 + 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 +//}