package crawler import ( "git.loafle.net/overflow/ssh_crawler/ssh" "git.loafle.net/overflow/ssh_crawler/stat" "bytes" ) type Crawler struct { sshCli *ssh.SSHClient } func New(ip, port, user, pw string) (*Crawler, error) { c := &Crawler{} err := c.connectSSH(ip, port, user, pw) if err != nil { return nil, err } return c, nil } func (c *Crawler) connectSSH(ip, port, user, pw string) error { client, err := ssh.New(ip, port, user, pw) if err != nil { return err } c.sshCli = client return nil } func (c *Crawler) CPUStat(ch chan interface{}) { go func() { cpu := &stat.CPUStat{} b, err := c.sshCli.RunCommand(cpu.Command()) if err != nil { ch <- err } res, err := cpu.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() } func (c *Crawler) MemStat(ch chan interface{}) { go func() { mem := &stat.MemStat{} b, err := c.sshCli.RunCommand(mem.Command()) if err != nil { ch <- err } res, err := mem.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() } func (c *Crawler) DiskIOStat(ch chan interface{}) { go func() { diskio := &stat.DiskIOStat{} b, err := c.sshCli.RunCommand(diskio.Command()) if err != nil { ch <- err } res, err := diskio.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() } func (c *Crawler) DiskFreeStat(ch chan interface{}) { go func() { diskFree := &stat.DiskFreeStat{} b, err := c.sshCli.RunCommand(diskFree.Command()) if err != nil { ch <- err } res, err := diskFree.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() } 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.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() } func (c *Crawler) NetworkStat(ch chan interface{}) { go func() { net := &stat.NetDevStat{} b, err := c.sshCli.RunCommand(net.Command()) if err != nil { ch <- err } res, err := net.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() }