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
import (
"bytes"
"git.loafle.net/overflow/ssh_crawler/ssh"
"git.loafle.net/overflow/ssh_crawler/stat"
"bytes"
)
type Crawler struct {
@ -29,7 +29,6 @@ func (c *Crawler) connectSSH(ip, port, user, pw string) error {
return nil
}
func (c *Crawler) CPUStat(ch chan interface{}) {
go func() {
cpu := &stat.CPUStat{}
@ -45,7 +44,6 @@ func (c *Crawler) CPUStat(ch chan interface{}) {
}()
}
func (c *Crawler) MemStat(ch chan interface{}) {
go func() {
mem := &stat.MemStat{}
@ -109,7 +107,7 @@ func (c *Crawler) LoadAvgStat(ch chan interface{}) {
}()
}
func (c *Crawler) NetworkStat(ch chan interface{}) {
func (c *Crawler) NetworkStat(ch chan interface{}) {
go func() {
net := &stat.NetDevStat{}
b, err := c.sshCli.RunCommand(net.Command())
@ -122,4 +120,19 @@ func (c *Crawler) NetworkStat(ch chan interface{}) {
}
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"
)
func main() {
runtime.GOMAXPROCS(4)
@ -55,10 +54,10 @@ func main() {
cr.LoadAvgStat(loadCh)
fmt.Println(<-loadCh)
close(loadCh)
//Process
psCh := make(chan interface{})
cr.ProcessStat(psCh)
fmt.Println(<-psCh)
close(psCh)
}

View File

@ -1,9 +1,9 @@
package ssh
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"bytes"
"strconv"
)
@ -16,12 +16,12 @@ type SSHConfig struct {
type SSHClient struct {
session *ssh.Session
conf *SSHConfig
conf *SSHConfig
}
func New(ip, port, user, pw string) (*SSHClient, error) {
p, _ := strconv.Atoi(port)
conf := &SSHConfig {
conf := &SSHConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pw),
@ -30,19 +30,19 @@ func New(ip, port, user, pw string) (*SSHClient, error) {
Port: p,
}
return &SSHClient{conf:conf}, nil
return &SSHClient{conf: conf}, nil
}
func (cli *SSHClient) Session() error {
sshConfig := &ssh.ClientConfig{
User: cli.conf.User,
Auth: cli.conf.Auth,
HostKeyCallback:ssh.InsecureIgnoreHostKey(),
User: cli.conf.User,
Auth: cli.conf.Auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", cli.conf.Host, cli.conf.Port), sshConfig)
if err != nil {
return err
return err
}
session, err := connection.NewSession()
if err != nil {
@ -65,7 +65,6 @@ func (cli *SSHClient) Session() error {
return nil
}
func (cli *SSHClient) RunCommand(command string) ([]byte, error) {
if err := cli.Session(); err != nil {

View File

@ -1,8 +1,8 @@
package stat
import (
"io"
"bufio"
"io"
"strings"
)
@ -24,7 +24,7 @@ func (cpu CPUStat) Command() string {
return "cat /proc/stat"
}
func (cpu CPUStat) Parse(r io.Reader) ([]CPUStat, error) {
func (cpu CPUStat) Parse(r io.Reader) (*[]CPUStat, error) {
var (
CPUStats = []CPUStat{}
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
import (
"io"
"bufio"
"io"
"strings"
)
@ -19,7 +19,7 @@ func (diskFree DiskFreeStat) Command() string {
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 stats = []DiskFreeStat{}
@ -29,21 +29,21 @@ func (diskio DiskFreeStat) Parse(r io.Reader) ([]DiskFreeStat, error) {
parts := strings.Fields(scanner.Text())
stats = append(stats, DiskFreeStat{
filesystem: parts[0],
size: parts[1],
used: parts[2],
available: parts[3],
usePerc: removePercUnit(parts[4]),
mountedOn: parts[5],
size: parts[1],
used: parts[2],
available: parts[3],
usePerc: removePercUnit(parts[4]),
mountedOn: parts[5],
})
}
return stats, scanner.Err()
return &stats, scanner.Err()
}
func removePercUnit(str string) string {
if !strings.HasSuffix(str, "%"){
if !strings.HasSuffix(str, "%") {
return str
}
str = str[:len(str)-1]
return str
}
}

View File

@ -1,10 +1,10 @@
package stat
import (
"io"
"bufio"
"strings"
"fmt"
"io"
"strings"
)
type DiskIOStat struct {
@ -26,10 +26,10 @@ func (diskio DiskIOStat) Command() string {
return "cat /proc/diskstats"
}
func (diskio DiskIOStat) Parse(r io.Reader) ([]DiskIOStat, error) {
func (diskio DiskIOStat) Parse(r io.Reader) (*[]DiskIOStat, error) {
var (
DiskIOStats = []DiskIOStat{}
scanner = bufio.NewScanner(r)
stats = []DiskIOStat{}
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
@ -41,7 +41,7 @@ func (diskio DiskIOStat) Parse(r io.Reader) ([]DiskIOStat, error) {
if !strings.HasPrefix(deviceName, "sd") {
continue
}
DiskIOStats = append(DiskIOStats, DiskIOStat{
stats = append(stats, DiskIOStat{
device: deviceName,
readCompleted: parts[3],
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
import (
"io"
"bufio"
"io"
"strings"
)
@ -16,7 +16,7 @@ func (loadavg LoadAvg) Command() string {
return "cat /proc/loadavg"
}
func (loadavg LoadAvg) Parse(r io.Reader) (LoadAvg, error) {
func (loadavg LoadAvg) Parse(r io.Reader) (*LoadAvg, error) {
var (
scanner = bufio.NewScanner(r)
)
@ -29,5 +29,5 @@ func (loadavg LoadAvg) Parse(r io.Reader) (LoadAvg, error) {
load.min5 = parts[1]
load.min15 = parts[2]
}
return load, scanner.Err()
return &load, scanner.Err()
}

View File

@ -1,20 +1,19 @@
package stat
import (
"bufio"
"io"
"strings"
"bufio"
)
type MemStat struct {
}
func (mem MemStat) Command() string {
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 (
memInfo = map[string]string{}
scanner = bufio.NewScanner(r)
@ -27,5 +26,5 @@ func (mem MemStat) Parse(r io.Reader) (map[string]string, error) {
memInfo[key] = parts[1]
}
return memInfo, scanner.Err()
return &memInfo, scanner.Err()
}

View File

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