This commit is contained in:
insanity 2017-10-25 17:37:32 +09:00
parent 87ae90959a
commit d75e100746
5 changed files with 73 additions and 35 deletions

View File

@ -10,17 +10,19 @@ type Crawler struct {
sshCli *ssh.SSHClient sshCli *ssh.SSHClient
} }
func New(ip, port, user, pw string) (*Crawler, error) { func New(ip, port, user, pw string, keyFilePath string) (*Crawler, error) {
c := &Crawler{} c := &Crawler{}
err := c.connectSSH(ip, port, user, pw) err := c.connectSSH(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c, nil return c, nil
} }
func (c *Crawler) connectSSH(ip, port, user, pw string) error {
client, err := ssh.New(ip, port, user, pw)
func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
client, err := ssh.New(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
return err return err
} }

53
main.go
View File

@ -8,12 +8,13 @@ import (
func main() { func main() {
const ip = "192.168.1.15" const ip = "192.168.1.215"
const port = "22" const port = "22"
const user = "administrator" const user = "snoop"
const pw = "!@#$qwer1234" const pw = "!@#$qwer1234"
const keyFilePath = "/home/insanity/.ssh/id_rsa"
cr, err := crawler.New(ip, port, user, pw) cr, err := crawler.New(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -50,6 +51,7 @@ func main() {
cr.MemStat(memCh, memkeys) cr.MemStat(memCh, memkeys)
print(<-memCh) print(<-memCh)
//Disk IO //Disk IO
ioKeys := []string { ioKeys := []string {
"disk[0].io.device", "disk[0].io.device",
@ -65,9 +67,9 @@ func main() {
"disk[0].io.weighted", "disk[0].io.weighted",
"disk[1].io.readcnt", "disk[1].io.readcnt",
"disk[2].io.merged_readcnt", "disk[1].io.merged_readcnt",
"disk[3].io.readbytes", "disk[1].io.readbytes",
"disk[4].io.readtime", "disk[1].io.readtime",
} }
diskioCh := make(chan interface{}) diskioCh := make(chan interface{})
cr.DiskIOStat(diskioCh, ioKeys) cr.DiskIOStat(diskioCh, ioKeys)
@ -93,27 +95,27 @@ func main() {
//Network //Network
netKeys := []string { netKeys := []string {
"net[14].transfer.iface", "net[0].transfer.iface",
"net[14].transfer.recv_byte", "net[0].transfer.recv_byte",
"net[14].transfer.recv_packet", "net[0].transfer.recv_packet",
"net[14].transfer.recv_err", "net[0].transfer.recv_err",
"net[14].transfer.recv_drop", "net[0].transfer.recv_drop",
"net[14].transfer.recv_fifo", "net[0].transfer.recv_fifo",
"net[14].transfer.recv_frame", "net[0].transfer.recv_frame",
"net[14].transfer.recv_compressed", "net[0].transfer.recv_compressed",
"net[14].transfer.recv_multicast", "net[0].transfer.recv_multicast",
"net[14].transfer.send_byte", "net[0].transfer.send_byte",
"net[14].transfer.send_packet", "net[0].transfer.send_packet",
"net[14].transfer.send_err", "net[0].transfer.send_err",
"net[14].transfer.send_drop", "net[0].transfer.send_drop",
"net[14].transfer.send_fifo", "net[0].transfer.send_fifo",
"net[14].transfer.send_frame", "net[0].transfer.send_frame",
"net[14].transfer.send_compressed", "net[0].transfer.send_compressed",
"net[14].transfer.send_multicast", "net[0].transfer.send_multicast",
"net[1].transfer.recv_packet", "net[1].transfer.recv_packet",
"net[2].transfer.send_compressed", "net[1].transfer.send_compressed",
"net[3].transfer.send_packet", "net[1].transfer.send_packet",
} }
netCh := make(chan interface{}) netCh := make(chan interface{})
cr.NetworkStat(netCh, netKeys) cr.NetworkStat(netCh, netKeys)
@ -142,3 +144,4 @@ func print(data interface{}) {
} }
fmt.Println(string(jsonData)) fmt.Println(string(jsonData))
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"strconv" "strconv"
"io/ioutil"
"time"
) )
type SSHConfig struct { type SSHConfig struct {
@ -19,13 +21,23 @@ type SSHClient struct {
conf *SSHConfig conf *SSHConfig
} }
func New(ip, port, user, pw string) (*SSHClient, error) { func New(ip, port, user, pw, keyFilePath string) (*SSHClient, error) {
p, _ := strconv.Atoi(port) p, _ := strconv.Atoi(port)
auth := make([]ssh.AuthMethod, 0)
if keyFilePath != "" {
key, err := parsePrivateKey(keyFilePath, pw)
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(key))
}
auth = append(auth, ssh.Password(pw))
conf := &SSHConfig{ conf := &SSHConfig{
User: user, User: user,
Auth: []ssh.AuthMethod{ Auth: auth,
ssh.Password(pw),
},
Host: ip, Host: ip,
Port: p, Port: p,
} }
@ -38,6 +50,7 @@ func (cli *SSHClient) Session() error {
User: cli.conf.User, User: cli.conf.User,
Auth: cli.conf.Auth, Auth: cli.conf.Auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Second * 10,
} }
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", cli.conf.Host, cli.conf.Port), sshConfig) connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", cli.conf.Host, cli.conf.Port), sshConfig)
@ -83,3 +96,15 @@ func (cli *SSHClient) RunCommand(command string) ([]byte, error) {
return b.Bytes(), nil return b.Bytes(), nil
} }
func parsePrivateKey(keyPath, pw string) (ssh.Signer, error) {
buff, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
if pw == "" {
return ssh.ParsePrivateKey(buff)
}
return ssh.ParsePrivateKeyWithPassphrase(buff, []byte(pw))
}

View File

@ -28,7 +28,7 @@ func (diskio DiskIOStat) Command() string {
return "cat /proc/diskstats" return "cat /proc/diskstats"
} }
func (diskio DiskIOStat) Read(r io.Reader, keys []string) (*map[string]string, error) { func (diskio DiskIOStat) Read(r io.Reader, keys []string) (interface{}, error) {
var ( var (
stats = []DiskIOStat{} stats = []DiskIOStat{}
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
@ -59,6 +59,10 @@ func (diskio DiskIOStat) Read(r io.Reader, keys []string) (*map[string]string, e
}) })
} }
if keys == nil {
return &stats, nil
}
res, err :=diskio.parse(keys, stats) res, err :=diskio.parse(keys, stats)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -34,7 +34,7 @@ func (net *NetDevStat) Command() string {
return "cat /proc/net/dev" return "cat /proc/net/dev"
} }
func (net *NetDevStat) Read(r io.Reader, keys []string) (*map[string]string, error) { func (net *NetDevStat) Read(r io.Reader, keys []string) (interface{}, error) {
var ( var (
scanner = bufio.NewScanner(r) scanner = bufio.NewScanner(r)
procNetDevFieldSep = regexp.MustCompile("[ :] *") procNetDevFieldSep = regexp.MustCompile("[ :] *")
@ -78,6 +78,10 @@ func (net *NetDevStat) Read(r io.Reader, keys []string) (*map[string]string, err
}) })
} }
if keys == nil {
return &stats, nil
}
res, err := net.parse(keys, stats) res, err := net.parse(keys, stats)
if err != nil { if err != nil {
return nil, err return nil, err