package crawler import ( "bytes" "encoding/json" "git.loafle.net/overflow/ssh_crawler/ssh" "git.loafle.net/overflow/ssh_crawler/stat" ) 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{}, keys []string) { go func() { cpu := &stat.CPUStat{} b, err := c.sshCli.RunCommand(cpu.Command()) if err != nil { ch <- err } res, err := cpu.Read(bytes.NewReader(b), keys) if err != nil { ch <- err } jsonData, err := json.Marshal(res) if err != nil { ch <- err } ch <- string(jsonData) 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 } jsonData, err := json.Marshal(res) if err != nil { ch <- err } ch <- string(jsonData) close(ch) }() } 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 close(ch) }() } 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 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.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 }() } 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.Parse(bytes.NewReader(b)) if err != nil { ch <- err } ch <- res }() }