ssh health crawler
This commit is contained in:
parent
f54181f2d3
commit
f446a71540
2
.vscode/settings.json
vendored
Normal file
2
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
{
|
||||
}
|
|
@ -3,9 +3,11 @@ package health
|
|||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
cnsm "git.loafle.net/commons/service_matcher-go"
|
||||
cuej "git.loafle.net/commons/util-go/encoding/json"
|
||||
|
@ -35,7 +37,7 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
|
|||
|
||||
network := ""
|
||||
switch metaIPTypeKey {
|
||||
case ocmm.MetaIPTypeEnumV4.String():
|
||||
case ocmm.MetaIPTypeEnumV6.String():
|
||||
switch metaPortTypeKey {
|
||||
case ocmm.MetaPortTypeEnumUDP.String():
|
||||
network = "udp6"
|
||||
|
@ -51,7 +53,7 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
|
|||
}
|
||||
}
|
||||
|
||||
conn, err := net.Dial(network, addr)
|
||||
conn, err := net.DialTimeout(network, addr, time.Second*5)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -91,7 +93,7 @@ func ToSocketErrorEnum(err error) SocketErrorEnum {
|
|||
}
|
||||
|
||||
// Duplication Method
|
||||
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) {
|
||||
func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig) (map[string]string, error) {
|
||||
result := make(map[string]string, 0)
|
||||
|
||||
conn, cErr := s.GetConnection(config)
|
||||
|
@ -161,22 +163,19 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[strin
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.Conn, result map[string]string) (map[string]string, error) {
|
||||
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.Conn) error {
|
||||
|
||||
connection := config.Connection
|
||||
port, _ := cuej.NumberToInt(connection.Port)
|
||||
info := cnsm.NewMatchInfo(connection.IP, port)
|
||||
|
||||
if s.m.IsPrePacket() {
|
||||
result["PacketType"] = "Pre"
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := conn.Read(buf)
|
||||
p := cnsm.NewPacket(buf, n)
|
||||
if !s.m.Match(info, 0, p) {
|
||||
result["Packet"] = convertBase64(buf)
|
||||
result["Error"] = "Not Matched"
|
||||
return result, nil
|
||||
return fmt.Errorf("Not Matched")
|
||||
}
|
||||
|
||||
for i := 0; i < s.m.PacketCount(); i++ {
|
||||
|
@ -190,16 +189,14 @@ func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.
|
|||
}
|
||||
|
||||
p := cnsm.NewPacket(buf, n)
|
||||
if s.m.Match(info, i+1, p) == false {
|
||||
result["Packet"] = convertBase64(buf)
|
||||
result["Error"] = "Not Matched"
|
||||
return result, nil
|
||||
if !s.m.Match(info, i+1, p) {
|
||||
return fmt.Errorf("Not Matched")
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
result["PacketType"] = "Post"
|
||||
return nil
|
||||
|
||||
} else {
|
||||
for i := 0; i < s.m.PacketCount(); i++ {
|
||||
pack := s.m.Packet(i)
|
||||
conn.Write(pack.Buffer)
|
||||
|
@ -211,15 +208,13 @@ func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.
|
|||
}
|
||||
|
||||
p := cnsm.NewPacket(buf, n)
|
||||
if s.m.Match(info, i, p) == false {
|
||||
result["Packet"] = convertBase64(buf)
|
||||
result["Error"] = "Not Matched"
|
||||
return result, nil
|
||||
if !s.m.Match(info, i, p) {
|
||||
return fmt.Errorf("Not Matched")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertBase64(buf []byte) string {
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package ssh
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.loafle.net/commons/logging-go"
|
||||
cnsms "git.loafle.net/commons/service_matcher-go/ssh"
|
||||
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
||||
"git.loafle.net/overflow/container_network/crawler/health"
|
||||
"git.loafle.net/overflow/crawler-go"
|
||||
"git.loafle.net/commons/logging-go"
|
||||
)
|
||||
|
||||
type SSHHealthCrawler struct {
|
||||
|
@ -32,17 +34,27 @@ func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, e
|
|||
|
||||
if cErr != nil {
|
||||
sckEnum := health.ToSocketErrorEnum(cErr)
|
||||
result["Error"] = sckEnum.String()
|
||||
result["ERR"] = sckEnum.String()
|
||||
logging.Logger().Error("SSHHealthCrawler Connection Error: ", sckEnum.String())
|
||||
return result, cErr
|
||||
}
|
||||
|
||||
|
||||
rss, err := c.CheckHealth1(config, conn, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, mci := range config.MetaCollectionItems {
|
||||
switch mci.Key {
|
||||
case "service.health.response_time":
|
||||
start := time.Now().UTC()
|
||||
if err := c.CheckHealth(config, conn); err != nil {
|
||||
result["ERR"] = err.Error()
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
result[mci.Key] = elapsed.String()
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
return rss, nil
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func NewCrawler() crawler.Crawler {
|
||||
|
|
|
@ -1,26 +1,34 @@
|
|||
package ssh
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"git.loafle.net/overflow/commons-go/model/meta"
|
||||
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
config := &ocmsc.SensorConfig{}
|
||||
config.Target = &ocmsc.Target{}
|
||||
config.Target.Connection = &ocmsc.Connection{
|
||||
IP: "192.168.1.215",
|
||||
Port: json.Number(22),
|
||||
PortType: "tcp",
|
||||
SSL: false,
|
||||
config.Connection = &ocmsc.SensorConfigConnection{
|
||||
MetaIPTypeKey: "V4",
|
||||
IP: "192.168.1.103",
|
||||
MetaPortTypeKey: "TCP",
|
||||
Port: "22",
|
||||
MetaCryptoTypeKey: "SSL",
|
||||
}
|
||||
metaCollectionItem := &meta.MetaCollectionItem{
|
||||
Key: "service.health.response_time",
|
||||
}
|
||||
config.MetaCollectionItems = make([]*meta.MetaCollectionItem, 1)
|
||||
config.MetaCollectionItems[0] = metaCollectionItem
|
||||
|
||||
c := NewCrawler()
|
||||
rss, err := c.Get(config)
|
||||
|
||||
assert.Nil(t, err)
|
||||
if err != nil {
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
assert.NotNil(t, rss)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user