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{}) {
go func() {
cpu := &stat.CPUStat{}
b, err := c.sshCli.RunCommand(cpu.Command())
if err != nil {
return nil, err
ch <- err
}
res, err := cpu.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}
return cpu.Parse(bytes.NewReader(b))
}
func (c *Crawler) MEMStat()(map[string]string, error) {
mem := &stat.MEMStat{}
func (c *Crawler) MemStat(ch chan interface{}) {
go func() {
mem := &stat.MemStat{}
b, err := c.sshCli.RunCommand(mem.Command())
if err != nil {
return nil, err
ch <- err
}
res, err := mem.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}
return mem.Parse(bytes.NewReader(b))
}
func (c *Crawler) DiskIOStat()([]stat.DiskIOStat, error) {
func (c *Crawler) DiskIOStat(ch chan interface{}) {
go func() {
diskio := &stat.DiskIOStat{}
b, err := c.sshCli.RunCommand(diskio.Command())
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{}) {
go func() {
diskFree := &stat.DiskFreeStat{}
b, err := c.sshCli.RunCommand(diskFree.Command())
if err != nil {
return nil, err
ch <- err
}
res, err := diskFree.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}
return diskFree.Parse(bytes.NewReader(b))
}
func (c *Crawler) LoadAvgStat()(stat.LoadAvg, error) {
func (c *Crawler) LoadAvgStat(ch chan interface{}) {
go func() {
load := &stat.LoadAvg{}
b, err := c.sshCli.RunCommand(load.Command())
if err != nil {
return stat.LoadAvg{}, err
ch <- err
}
res, err := load.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}
return load.Parse(bytes.NewReader(b))
}
func (c *Crawler) NetworkStat() ([]stat.NetDevStat, error) {
func (c *Crawler) NetworkStat(ch chan interface{}) {
go func() {
net := &stat.NetDevStat{}
b, err := c.sshCli.RunCommand(net.Command())
if err != nil {
return nil, err
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 (
"fmt"
"git.loafle.net/overflow/ssh_crawler/crawler"
"runtime"
)
func main() {
runtime.GOMAXPROCS(4)
const ip = "192.168.1.15"
const port = "22"
@ -19,46 +21,40 @@ func main() {
}
//CPU
cpu, err := cr.CPUStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
fmt.Printf("CPU: %s\n", cpu)
cpuCh := make(chan interface{})
cr.CPUStat(cpuCh)
fmt.Println(<-cpuCh)
close(cpuCh)
//MEM
mem, err := cr.MEMStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
fmt.Printf("MEM: %s\n", mem)
//Memory
memCh := make(chan interface{})
cr.MemStat(memCh)
fmt.Println(<-memCh)
close(memCh)
//Disk IO
diskio, err := cr.DiskIOStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
fmt.Printf("DiskIO: %s\n", diskio)
diskioCh := make(chan interface{})
cr.DiskIOStat(diskioCh)
fmt.Println(<-diskioCh)
close(diskioCh)
//Disk Free
diskFree, err := cr.DiskFreeStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
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)
diskFreeCh := make(chan interface{})
cr.DiskFreeStat(diskFreeCh)
fmt.Println(<-diskFreeCh)
close(diskFreeCh)
//Network
net, err := cr.NetworkStat()
if err != nil {
fmt.Printf("err : %s\n", err)
}
fmt.Printf("Net: %s\n", net)
netCh := make(chan interface{})
cr.NetworkStat(netCh)
fmt.Println(<-netCh)
close(netCh)
//Load Avg.
loadCh := make(chan interface{})
cr.LoadAvgStat(loadCh)
fmt.Println(<-loadCh)
close(loadCh)
}

View File

@ -6,15 +6,15 @@ import (
"bufio"
)
type MEMStat struct {
type MemStat struct {
}
func (cpu MEMStat) Command() string {
func (mem MemStat) Command() string {
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 (
memInfo = map[string]string{}
scanner = bufio.NewScanner(r)