ing
This commit is contained in:
parent
87ae90959a
commit
d75e100746
|
@ -10,17 +10,19 @@ type Crawler struct {
|
|||
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{}
|
||||
err := c.connectSSH(ip, port, user, pw)
|
||||
err := c.connectSSH(ip, port, user, pw, keyFilePath)
|
||||
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)
|
||||
|
||||
|
||||
func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
|
||||
client, err := ssh.New(ip, port, user, pw, keyFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
53
main.go
53
main.go
|
@ -8,12 +8,13 @@ import (
|
|||
|
||||
func main() {
|
||||
|
||||
const ip = "192.168.1.15"
|
||||
const ip = "192.168.1.215"
|
||||
const port = "22"
|
||||
const user = "administrator"
|
||||
const user = "snoop"
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@ -50,6 +51,7 @@ func main() {
|
|||
cr.MemStat(memCh, memkeys)
|
||||
print(<-memCh)
|
||||
|
||||
|
||||
//Disk IO
|
||||
ioKeys := []string {
|
||||
"disk[0].io.device",
|
||||
|
@ -65,9 +67,9 @@ func main() {
|
|||
"disk[0].io.weighted",
|
||||
|
||||
"disk[1].io.readcnt",
|
||||
"disk[2].io.merged_readcnt",
|
||||
"disk[3].io.readbytes",
|
||||
"disk[4].io.readtime",
|
||||
"disk[1].io.merged_readcnt",
|
||||
"disk[1].io.readbytes",
|
||||
"disk[1].io.readtime",
|
||||
}
|
||||
diskioCh := make(chan interface{})
|
||||
cr.DiskIOStat(diskioCh, ioKeys)
|
||||
|
@ -93,27 +95,27 @@ func main() {
|
|||
|
||||
//Network
|
||||
netKeys := []string {
|
||||
"net[14].transfer.iface",
|
||||
"net[14].transfer.recv_byte",
|
||||
"net[14].transfer.recv_packet",
|
||||
"net[14].transfer.recv_err",
|
||||
"net[14].transfer.recv_drop",
|
||||
"net[14].transfer.recv_fifo",
|
||||
"net[14].transfer.recv_frame",
|
||||
"net[14].transfer.recv_compressed",
|
||||
"net[14].transfer.recv_multicast",
|
||||
"net[14].transfer.send_byte",
|
||||
"net[14].transfer.send_packet",
|
||||
"net[14].transfer.send_err",
|
||||
"net[14].transfer.send_drop",
|
||||
"net[14].transfer.send_fifo",
|
||||
"net[14].transfer.send_frame",
|
||||
"net[14].transfer.send_compressed",
|
||||
"net[14].transfer.send_multicast",
|
||||
"net[0].transfer.iface",
|
||||
"net[0].transfer.recv_byte",
|
||||
"net[0].transfer.recv_packet",
|
||||
"net[0].transfer.recv_err",
|
||||
"net[0].transfer.recv_drop",
|
||||
"net[0].transfer.recv_fifo",
|
||||
"net[0].transfer.recv_frame",
|
||||
"net[0].transfer.recv_compressed",
|
||||
"net[0].transfer.recv_multicast",
|
||||
"net[0].transfer.send_byte",
|
||||
"net[0].transfer.send_packet",
|
||||
"net[0].transfer.send_err",
|
||||
"net[0].transfer.send_drop",
|
||||
"net[0].transfer.send_fifo",
|
||||
"net[0].transfer.send_frame",
|
||||
"net[0].transfer.send_compressed",
|
||||
"net[0].transfer.send_multicast",
|
||||
|
||||
"net[1].transfer.recv_packet",
|
||||
"net[2].transfer.send_compressed",
|
||||
"net[3].transfer.send_packet",
|
||||
"net[1].transfer.send_compressed",
|
||||
"net[1].transfer.send_packet",
|
||||
}
|
||||
netCh := make(chan interface{})
|
||||
cr.NetworkStat(netCh, netKeys)
|
||||
|
@ -142,3 +144,4 @@ func print(data interface{}) {
|
|||
}
|
||||
fmt.Println(string(jsonData))
|
||||
}
|
||||
|
||||
|
|
33
ssh/ssh.go
33
ssh/ssh.go
|
@ -5,6 +5,8 @@ import (
|
|||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"strconv"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SSHConfig struct {
|
||||
|
@ -19,13 +21,23 @@ type SSHClient struct {
|
|||
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)
|
||||
|
||||
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{
|
||||
User: user,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(pw),
|
||||
},
|
||||
Auth: auth,
|
||||
Host: ip,
|
||||
Port: p,
|
||||
}
|
||||
|
@ -38,6 +50,7 @@ func (cli *SSHClient) Session() error {
|
|||
User: cli.conf.User,
|
||||
Auth: cli.conf.Auth,
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
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))
|
||||
}
|
|
@ -28,7 +28,7 @@ func (diskio DiskIOStat) Command() string {
|
|||
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 (
|
||||
stats = []DiskIOStat{}
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -34,7 +34,7 @@ func (net *NetDevStat) Command() string {
|
|||
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 (
|
||||
scanner = bufio.NewScanner(r)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue
Block a user