ssh_crawler/crawler/crawler.go
insanity 87ae90959a ing
2017-10-24 18:30:22 +09:00

158 lines
2.8 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) (*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
}
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) 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
}()
}