2017-10-23 05:31:04 +00:00
|
|
|
package crawler
|
|
|
|
|
|
|
|
import (
|
2017-10-23 09:08:19 +00:00
|
|
|
"bytes"
|
2017-10-23 05:31:04 +00:00
|
|
|
"git.loafle.net/overflow/ssh_crawler/ssh"
|
2017-11-09 09:04:07 +00:00
|
|
|
|
|
|
|
config "git.loafle.net/overflow/overflow_commons_go/modules/config/model"
|
|
|
|
configUtil "git.loafle.net/overflow/overflow_commons_go/modules/config/util"
|
|
|
|
"io"
|
|
|
|
|
2017-10-23 05:31:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Crawler struct {
|
|
|
|
sshCli *ssh.SSHClient
|
|
|
|
}
|
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
type SSHCrawlerModuler interface {
|
|
|
|
Read(r io.Reader) (map[string]string, error)
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:37:32 +00:00
|
|
|
func New(ip, port, user, pw string, keyFilePath string) (*Crawler, error) {
|
2017-10-23 05:31:04 +00:00
|
|
|
c := &Crawler{}
|
2017-10-25 08:37:32 +00:00
|
|
|
err := c.connectSSH(ip, port, user, pw, keyFilePath)
|
2017-10-23 05:31:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:37:32 +00:00
|
|
|
func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
|
|
|
|
client, err := ssh.New(ip, port, user, pw, keyFilePath)
|
2017-10-23 05:31:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.sshCli = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
func (c *Crawler) ParseResult(collectMap map[string]string, item config.Item) (map[string]string, error) {
|
2017-11-08 02:43:25 +00:00
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
mappingMap := configUtil.KeysToMap(item.Keys)
|
2017-10-23 05:31:04 +00:00
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
resMap := make(map[string]string)
|
2017-10-23 06:59:38 +00:00
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
for key, value := range mappingMap {
|
|
|
|
resMap[value] = collectMap[key]
|
|
|
|
}
|
2017-10-23 06:59:38 +00:00
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
return resMap, nil
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 09:04:07 +00:00
|
|
|
func (c *Crawler) Process(moduler SSHCrawlerModuler, ch chan interface{}, item config.Item) {
|
2017-10-23 06:59:38 +00:00
|
|
|
go func() {
|
2017-11-09 09:04:07 +00:00
|
|
|
b, err := c.sshCli.RunCommand(item.QueryInfo.Query)
|
2017-10-23 06:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
}
|
2017-11-09 09:04:07 +00:00
|
|
|
res, err := moduler.Read(bytes.NewReader(b))
|
2017-10-26 02:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
}
|
2017-11-09 09:04:07 +00:00
|
|
|
res, err = c.ParseResult(res, item)
|
2017-10-26 02:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
}
|
|
|
|
ch <- res
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
}
|
2017-11-09 09:04:07 +00:00
|
|
|
//
|
|
|
|
//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
|
|
|
|
// }()
|
|
|
|
//}
|