ssh_crawler/crawler/crawler.go
insanity 192f72ff83 ing
2017-11-08 11:43:25 +09:00

176 lines
3.2 KiB
Go

package crawler
import (
"bytes"
"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, keyFilePath string) (*Crawler, error) {
c := &Crawler{}
err := c.connectSSH(ip, port, user, pw, keyFilePath)
if err != nil {
return nil, err
}
return c, nil
}
func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
client, err := ssh.New(ip, port, user, pw, keyFilePath)
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
}
ch <- res
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
}
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
}()
}