ssh_crawler/ssh_crawler.go
2017-11-10 17:33:29 +09:00

90 lines
1.7 KiB
Go

package ssh_crawler
import (
"encoding/json"
config "git.loafle.net/overflow/overflow_commons_go/modules/config/model"
"fmt"
"git.loafle.net/overflow/ssh_crawler/stat"
crawler "git.loafle.net/overflow/ssh_crawler/crawler"
"reflect"
rpcCrawler "git.loafle.net/overflow/crawler_go"
)
type SSHCrawler struct {
rpcCrawler.CrawlerImpl
}
func (r *SSHCrawler) Internal(params config.Config) ([]byte, error) {
b, err := r.start(&params)
if err != nil {
return nil, err
}
return json.Marshal(b)
}
func NewSSHCrawler() *SSHCrawler {
ad := &SSHCrawler{}
return ad
}
func (r *SSHCrawler) start(c *config.Config) (*map[string]string, error){
ip := c.Target.Connection.Ip
port := c.Target.Connection.Port
user := c.Target.Auth["id"].(string)
pw := c.Target.Auth["pw"].(string)
keyFilePathObj := c.Target.Auth["keyFilePath"]
var keyFilePath string = ""
if keyFilePathObj != nil {
keyFilePath = keyFilePathObj.(string)
}
cr, err := crawler.New(ip, port, user, pw, keyFilePath)
if err != nil {
fmt.Println(err)
}
var inter crawler.SSHCrawlerModuler
var resultMap map[string]string = make(map[string]string)
var tempMap map[string]string = nil
var temp interface{} = nil
for _, item := range c.Items {
mode := item.QueryInfo.Extend["mode"].(string)
switch mode {
case "cpu" :
inter = stat.CPUStat{}
break
case "mem" :
inter = stat.MemStat{}
break
default :
continue
}
ch := make(chan interface{})
cr.Process(inter, ch, item)
temp = <-ch
if reflect.TypeOf(temp).String() == "map[string]string" {
tempMap = temp.(map[string]string)
for k, v := range tempMap {
resultMap[k] = v
}
} else {
var errr error = temp.(error)
return nil, errr
}
}
return &resultMap, nil
}