This commit is contained in:
insanity 2017-10-23 18:08:19 +09:00
parent 2ee7fa5853
commit 3ef6659f46
11 changed files with 107 additions and 62 deletions

View File

@ -1,9 +1,9 @@
package crawler package crawler
import ( import (
"bytes"
"git.loafle.net/overflow/ssh_crawler/ssh" "git.loafle.net/overflow/ssh_crawler/ssh"
"git.loafle.net/overflow/ssh_crawler/stat" "git.loafle.net/overflow/ssh_crawler/stat"
"bytes"
) )
type Crawler struct { type Crawler struct {
@ -29,7 +29,6 @@ func (c *Crawler) connectSSH(ip, port, user, pw string) error {
return nil return nil
} }
func (c *Crawler) CPUStat(ch chan interface{}) { func (c *Crawler) CPUStat(ch chan interface{}) {
go func() { go func() {
cpu := &stat.CPUStat{} cpu := &stat.CPUStat{}
@ -45,7 +44,6 @@ func (c *Crawler) CPUStat(ch chan interface{}) {
}() }()
} }
func (c *Crawler) MemStat(ch chan interface{}) { func (c *Crawler) MemStat(ch chan interface{}) {
go func() { go func() {
mem := &stat.MemStat{} mem := &stat.MemStat{}
@ -123,3 +121,18 @@ func (c *Crawler) NetworkStat(ch chan interface{}) {
ch <- res 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.Parse(bytes.NewReader(b))
if err != nil {
ch <- err
}
ch <- res
}()
}

13
main.go
View File

@ -6,7 +6,6 @@ import (
"runtime" "runtime"
) )
func main() { func main() {
runtime.GOMAXPROCS(4) runtime.GOMAXPROCS(4)
@ -55,10 +54,10 @@ func main() {
cr.LoadAvgStat(loadCh) cr.LoadAvgStat(loadCh)
fmt.Println(<-loadCh) fmt.Println(<-loadCh)
close(loadCh) close(loadCh)
//Process
psCh := make(chan interface{})
cr.ProcessStat(psCh)
fmt.Println(<-psCh)
close(psCh)
} }

View File

@ -1,9 +1,9 @@
package ssh package ssh
import ( import (
"bytes"
"fmt" "fmt"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"bytes"
"strconv" "strconv"
) )
@ -65,7 +65,6 @@ func (cli *SSHClient) Session() error {
return nil return nil
} }
func (cli *SSHClient) RunCommand(command string) ([]byte, error) { func (cli *SSHClient) RunCommand(command string) ([]byte, error) {
if err := cli.Session(); err != nil { if err := cli.Session(); err != nil {

View File

@ -1,8 +1,8 @@
package stat package stat
import ( import (
"io"
"bufio" "bufio"
"io"
"strings" "strings"
) )
@ -24,7 +24,7 @@ func (cpu CPUStat) Command() string {
return "cat /proc/stat" return "cat /proc/stat"
} }
func (cpu CPUStat) Parse(r io.Reader) ([]CPUStat, error) { func (cpu CPUStat) Parse(r io.Reader) (*[]CPUStat, error) {
var ( var (
CPUStats = []CPUStat{} CPUStats = []CPUStat{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
@ -63,5 +63,5 @@ func (cpu CPUStat) Parse(r io.Reader) ([]CPUStat, error) {
}) })
} }
return CPUStats, scanner.Err() return &CPUStats, scanner.Err()
} }

View File

@ -1,8 +1,8 @@
package stat package stat
import ( import (
"io"
"bufio" "bufio"
"io"
"strings" "strings"
) )
@ -19,7 +19,7 @@ func (diskFree DiskFreeStat) Command() string {
return "df -k" return "df -k"
} }
func (diskio DiskFreeStat) Parse(r io.Reader) ([]DiskFreeStat, error) { func (diskio DiskFreeStat) Parse(r io.Reader) (*[]DiskFreeStat, error) {
var scanner = bufio.NewScanner(r) var scanner = bufio.NewScanner(r)
var stats = []DiskFreeStat{} var stats = []DiskFreeStat{}
@ -37,7 +37,7 @@ func (diskio DiskFreeStat) Parse(r io.Reader) ([]DiskFreeStat, error) {
}) })
} }
return stats, scanner.Err() return &stats, scanner.Err()
} }
func removePercUnit(str string) string { func removePercUnit(str string) string {

View File

@ -1,10 +1,10 @@
package stat package stat
import ( import (
"io"
"bufio" "bufio"
"strings"
"fmt" "fmt"
"io"
"strings"
) )
type DiskIOStat struct { type DiskIOStat struct {
@ -26,9 +26,9 @@ func (diskio DiskIOStat) Command() string {
return "cat /proc/diskstats" return "cat /proc/diskstats"
} }
func (diskio DiskIOStat) Parse(r io.Reader) ([]DiskIOStat, error) { func (diskio DiskIOStat) Parse(r io.Reader) (*[]DiskIOStat, error) {
var ( var (
DiskIOStats = []DiskIOStat{} stats = []DiskIOStat{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
) )
@ -41,7 +41,7 @@ func (diskio DiskIOStat) Parse(r io.Reader) ([]DiskIOStat, error) {
if !strings.HasPrefix(deviceName, "sd") { if !strings.HasPrefix(deviceName, "sd") {
continue continue
} }
DiskIOStats = append(DiskIOStats, DiskIOStat{ stats = append(stats, DiskIOStat{
device: deviceName, device: deviceName,
readCompleted: parts[3], readCompleted: parts[3],
readMerged: parts[4], readMerged: parts[4],
@ -58,5 +58,5 @@ func (diskio DiskIOStat) Parse(r io.Reader) ([]DiskIOStat, error) {
} }
return DiskIOStats, scanner.Err() return &stats, scanner.Err()
} }

View File

@ -1,8 +1,8 @@
package stat package stat
import ( import (
"io"
"bufio" "bufio"
"io"
"strings" "strings"
) )
@ -16,7 +16,7 @@ func (loadavg LoadAvg) Command() string {
return "cat /proc/loadavg" return "cat /proc/loadavg"
} }
func (loadavg LoadAvg) Parse(r io.Reader) (LoadAvg, error) { func (loadavg LoadAvg) Parse(r io.Reader) (*LoadAvg, error) {
var ( var (
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
) )
@ -29,5 +29,5 @@ func (loadavg LoadAvg) Parse(r io.Reader) (LoadAvg, error) {
load.min5 = parts[1] load.min5 = parts[1]
load.min15 = parts[2] load.min15 = parts[2]
} }
return load, scanner.Err() return &load, scanner.Err()
} }

View File

@ -1,20 +1,19 @@
package stat package stat
import ( import (
"bufio"
"io" "io"
"strings" "strings"
"bufio"
) )
type MemStat struct { type MemStat struct {
} }
func (mem MemStat) Command() string { func (mem MemStat) Command() string {
return "cat /proc/meminfo" return "cat /proc/meminfo"
} }
func (mem MemStat) Parse(r io.Reader) (map[string]string, error) { func (mem MemStat) Parse(r io.Reader) (*map[string]string, error) {
var ( var (
memInfo = map[string]string{} memInfo = map[string]string{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
@ -27,5 +26,5 @@ func (mem MemStat) Parse(r io.Reader) (map[string]string, error) {
memInfo[key] = parts[1] memInfo[key] = parts[1]
} }
return memInfo, scanner.Err() return &memInfo, scanner.Err()
} }

View File

@ -1,11 +1,11 @@
package stat package stat
import ( import (
"io"
"bufio" "bufio"
"fmt"
"io"
"regexp" "regexp"
"strings" "strings"
"fmt"
) )
type NetDevStat struct { type NetDevStat struct {
@ -32,7 +32,7 @@ func (net *NetDevStat) Command() string {
return "cat /proc/net/dev" return "cat /proc/net/dev"
} }
func (net *NetDevStat) Parse(r io.Reader) ([]NetDevStat, error) { func (net *NetDevStat) Parse(r io.Reader) (*[]NetDevStat, error) {
var ( var (
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
procNetDevFieldSep = regexp.MustCompile("[ :] *") procNetDevFieldSep = regexp.MustCompile("[ :] *")
@ -75,5 +75,5 @@ func (net *NetDevStat) Parse(r io.Reader) ([]NetDevStat, error) {
transMulticast: parts[16], transMulticast: parts[16],
}) })
} }
return netDevStats, scanner.Err() return &netDevStats, scanner.Err()
} }

43
stat/process.go Normal file
View File

@ -0,0 +1,43 @@
package stat
import (
"bufio"
"io"
"strings"
)
type ProcessStat struct {
user,
pid,
size,
pcpu,
pmem,
cmd string
}
func (ps *ProcessStat) Command() string {
return "ps -eo user,pid,size,pcpu,pmem,comm --sort=-pmem c"
}
func (ps *ProcessStat) Parse(r io.Reader) (*[]ProcessStat, error) {
var (
psStats = []ProcessStat{}
scanner = bufio.NewScanner(r)
)
scanner.Scan()
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
psStats = append(psStats, ProcessStat{
user: parts[0],
pid: parts[1],
size: parts[2],
pcpu: parts[3],
pmem: parts[4],
cmd: parts[5],
})
}
return &psStats, scanner.Err()
}

View File

@ -1,8 +0,0 @@
package stat
import "io"
type Stat interface {
Command() string
Parse(r io.Reader) (interface{}, error)
}