ssh_crawler/crawler/crawler.go

177 lines
3.2 KiB
Go
Raw Normal View History

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"
"git.loafle.net/overflow/ssh_crawler/stat"
)
type Crawler struct {
sshCli *ssh.SSHClient
}
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-10-24 07:09:17 +00:00
func (c *Crawler) CPUStat(ch chan interface{}, keys []string) {
2017-10-23 06:59:38 +00:00
go func() {
cpu := &stat.CPUStat{}
b, err := c.sshCli.RunCommand(cpu.Command())
if err != nil {
ch <- err
}
2017-10-24 07:09:17 +00:00
res, err := cpu.Read(bytes.NewReader(b), keys)
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
ch <- res
2017-10-24 07:09:17 +00:00
close(ch)
2017-10-23 06:59:38 +00:00
}()
2017-10-23 05:31:04 +00:00
}
2017-10-24 07:09:17 +00:00
func (c *Crawler) MemStat(ch chan interface{}, keys []string) {
2017-10-23 06:59:38 +00:00
go func() {
mem := &stat.MemStat{}
b, err := c.sshCli.RunCommand(mem.Command())
if err != nil {
ch <- err
}
2017-10-24 07:09:17 +00:00
res, err := mem.Read(bytes.NewReader(b), keys)
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
ch <- res
2017-10-24 07:09:17 +00:00
close(ch)
2017-10-23 06:59:38 +00:00
}()
2017-10-23 05:31:04 +00:00
}
2017-10-24 09:23:56 +00:00
func (c *Crawler) DiskIOStat(ch chan interface{}, keys []string) {
2017-10-23 06:59:38 +00:00
go func() {
diskio := &stat.DiskIOStat{}
b, err := c.sshCli.RunCommand(diskio.Command())
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
res, err := diskio.Read(bytes.NewReader(b), keys)
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
ch <- res
2017-10-24 07:09:17 +00:00
close(ch)
2017-10-23 06:59:38 +00:00
}()
2017-10-23 05:31:04 +00:00
}
2017-10-24 09:23:56 +00:00
func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) {
2017-10-23 06:59:38 +00:00
go func() {
diskFree := &stat.DiskFreeStat{}
b, err := c.sshCli.RunCommand(diskFree.Command())
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
res, err := diskFree.Read(bytes.NewReader(b), keys)
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
ch <- res
2017-10-24 07:09:17 +00:00
close(ch)
2017-10-23 06:59:38 +00:00
}()
2017-10-23 05:31:04 +00:00
}
2017-10-26 02:38:04 +00:00
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)
}()
}
2017-10-23 06:59:38 +00:00
func (c *Crawler) LoadAvgStat(ch chan interface{}) {
go func() {
load := &stat.LoadAvg{}
b, err := c.sshCli.RunCommand(load.Command())
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
res, err := load.Read(bytes.NewReader(b))
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
ch <- res
}()
2017-10-23 05:31:04 +00:00
}
2017-10-24 09:23:56 +00:00
func (c *Crawler) NetworkStat(ch chan interface{}, keys []string) {
2017-10-23 06:59:38 +00:00
go func() {
net := &stat.NetDevStat{}
b, err := c.sshCli.RunCommand(net.Command())
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
res, err := net.Read(bytes.NewReader(b), keys)
2017-10-23 06:59:38 +00:00
if err != nil {
ch <- err
}
ch <- res
}()
2017-10-23 09:08:19 +00:00
}
func (c *Crawler) ProcessStat(ch chan interface{}) {
go func() {
ps := &stat.ProcessStat{}
b, err := c.sshCli.RunCommand(ps.Command())
if err != nil {
ch <- err
}
2017-10-24 09:23:56 +00:00
res, err := ps.Read(bytes.NewReader(b))
2017-10-23 09:08:19 +00:00
if err != nil {
ch <- err
}
ch <- res
}()
}
2017-10-24 09:30:22 +00:00
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
}()
}