This commit is contained in:
insanity 2017-10-23 15:59:38 +09:00
parent 1552f4eaba
commit 2ee7fa5853
3 changed files with 110 additions and 85 deletions

View File

@ -30,67 +30,96 @@ func (c *Crawler) connectSSH(ip, port, user, pw string) error {
} }
func (c *Crawler) CPUStat()([]stat.CPUStat, error) { func (c *Crawler) CPUStat(ch chan interface{}) {
cpu := &stat.CPUStat{} go func() {
cpu := &stat.CPUStat{}
b, err := c.sshCli.RunCommand(cpu.Command()) b, err := c.sshCli.RunCommand(cpu.Command())
if err != nil { if err != nil {
return nil, err ch <- err
} }
res, err := cpu.Parse(bytes.NewReader(b))
return cpu.Parse(bytes.NewReader(b)) if err != nil {
ch <- err
}
ch <- res
}()
} }
func (c *Crawler) MEMStat()(map[string]string, error) {
mem := &stat.MEMStat{}
b, err := c.sshCli.RunCommand(mem.Command()) func (c *Crawler) MemStat(ch chan interface{}) {
if err != nil { go func() {
return nil, err mem := &stat.MemStat{}
} b, err := c.sshCli.RunCommand(mem.Command())
if err != nil {
return mem.Parse(bytes.NewReader(b)) ch <- err
}
res, err := mem.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
} }
func (c *Crawler) DiskIOStat()([]stat.DiskIOStat, error) { func (c *Crawler) DiskIOStat(ch chan interface{}) {
diskio := &stat.DiskIOStat{} go func() {
diskio := &stat.DiskIOStat{}
b, err := c.sshCli.RunCommand(diskio.Command()) b, err := c.sshCli.RunCommand(diskio.Command())
if err != nil { if err != nil {
return nil, err ch <- err
} }
return diskio.Parse(bytes.NewReader(b)) res, err := diskio.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
} }
func (c *Crawler) DiskFreeStat()([]stat.DiskFreeStat, error) { func (c *Crawler) DiskFreeStat(ch chan interface{}) {
diskFree := &stat.DiskFreeStat{} go func() {
diskFree := &stat.DiskFreeStat{}
b, err := c.sshCli.RunCommand(diskFree.Command()) b, err := c.sshCli.RunCommand(diskFree.Command())
if err != nil { if err != nil {
return nil, err ch <- err
} }
res, err := diskFree.Parse(bytes.NewReader(b))
return diskFree.Parse(bytes.NewReader(b)) if err != nil {
ch <- err
}
ch <- res
}()
} }
func (c *Crawler) LoadAvgStat()(stat.LoadAvg, error) { func (c *Crawler) LoadAvgStat(ch chan interface{}) {
load := &stat.LoadAvg{} go func() {
load := &stat.LoadAvg{}
b, err := c.sshCli.RunCommand(load.Command()) b, err := c.sshCli.RunCommand(load.Command())
if err != nil { if err != nil {
return stat.LoadAvg{}, err ch <- err
} }
res, err := load.Parse(bytes.NewReader(b))
return load.Parse(bytes.NewReader(b)) if err != nil {
ch <- err
}
ch <- res
}()
} }
func (c *Crawler) NetworkStat() ([]stat.NetDevStat, error) { func (c *Crawler) NetworkStat(ch chan interface{}) {
net := &stat.NetDevStat{} go func() {
b, err := c.sshCli.RunCommand(net.Command()) net := &stat.NetDevStat{}
if err != nil { b, err := c.sshCli.RunCommand(net.Command())
return nil, err if err != nil {
} ch <- err
}
return net.Parse(bytes.NewReader(b)) res, err := net.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
} }

62
main.go
View File

@ -3,10 +3,12 @@ package main
import ( import (
"fmt" "fmt"
"git.loafle.net/overflow/ssh_crawler/crawler" "git.loafle.net/overflow/ssh_crawler/crawler"
"runtime"
) )
func main() { func main() {
runtime.GOMAXPROCS(4)
const ip = "192.168.1.15" const ip = "192.168.1.15"
const port = "22" const port = "22"
@ -19,46 +21,40 @@ func main() {
} }
//CPU //CPU
cpu, err := cr.CPUStat() cpuCh := make(chan interface{})
if err != nil { cr.CPUStat(cpuCh)
fmt.Printf("err : %s\n", err) fmt.Println(<-cpuCh)
} close(cpuCh)
fmt.Printf("CPU: %s\n", cpu)
//MEM //Memory
mem, err := cr.MEMStat() memCh := make(chan interface{})
if err != nil { cr.MemStat(memCh)
fmt.Printf("err : %s\n", err) fmt.Println(<-memCh)
} close(memCh)
fmt.Printf("MEM: %s\n", mem)
//Disk IO //Disk IO
diskio, err := cr.DiskIOStat() diskioCh := make(chan interface{})
if err != nil { cr.DiskIOStat(diskioCh)
fmt.Printf("err : %s\n", err) fmt.Println(<-diskioCh)
} close(diskioCh)
fmt.Printf("DiskIO: %s\n", diskio)
//Disk Free //Disk Free
diskFree, err := cr.DiskFreeStat() diskFreeCh := make(chan interface{})
if err != nil { cr.DiskFreeStat(diskFreeCh)
fmt.Printf("err : %s\n", err) fmt.Println(<-diskFreeCh)
} close(diskFreeCh)
fmt.Printf("DiskFree: %s\n", diskFree)
//Load Avg.
load, err := cr.LoadAvgStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
fmt.Printf("Load: %s\n", load)
//Network //Network
net, err := cr.NetworkStat() netCh := make(chan interface{})
if err != nil { cr.NetworkStat(netCh)
fmt.Printf("err : %s\n", err) fmt.Println(<-netCh)
} close(netCh)
fmt.Printf("Net: %s\n", net)
//Load Avg.
loadCh := make(chan interface{})
cr.LoadAvgStat(loadCh)
fmt.Println(<-loadCh)
close(loadCh)
} }

View File

@ -6,15 +6,15 @@ import (
"bufio" "bufio"
) )
type MEMStat struct { type MemStat struct {
} }
func (cpu MEMStat) Command() string { func (mem MemStat) Command() string {
return "cat /proc/meminfo" return "cat /proc/meminfo"
} }
func (cpu MEMStat) Parse(r io.Reader) (map[string]string, error) { func (mem MemStat) Parse(r io.Reader) (map[string]string, error) {
var ( var (
memInfo = map[string]string{} memInfo = map[string]string{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)