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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}()
|
|
|
|
}
|