ssh health crawler

This commit is contained in:
insanity 2018-07-04 21:28:28 +09:00
parent f54181f2d3
commit f446a71540
4 changed files with 51 additions and 34 deletions

2
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,2 @@
{
}

View File

@ -3,9 +3,11 @@ package health
import ( import (
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"fmt"
"net" "net"
"os" "os"
"syscall" "syscall"
"time"
cnsm "git.loafle.net/commons/service_matcher-go" cnsm "git.loafle.net/commons/service_matcher-go"
cuej "git.loafle.net/commons/util-go/encoding/json" cuej "git.loafle.net/commons/util-go/encoding/json"
@ -35,7 +37,7 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
network := "" network := ""
switch metaIPTypeKey { switch metaIPTypeKey {
case ocmm.MetaIPTypeEnumV4.String(): case ocmm.MetaIPTypeEnumV6.String():
switch metaPortTypeKey { switch metaPortTypeKey {
case ocmm.MetaPortTypeEnumUDP.String(): case ocmm.MetaPortTypeEnumUDP.String():
network = "udp6" 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 { if err != nil {
return nil, err return nil, err
} }
@ -91,7 +93,7 @@ func ToSocketErrorEnum(err error) SocketErrorEnum {
} }
// Duplication Method // 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) result := make(map[string]string, 0)
conn, cErr := s.GetConnection(config) conn, cErr := s.GetConnection(config)
@ -161,22 +163,19 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[strin
return nil, nil 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 connection := config.Connection
port, _ := cuej.NumberToInt(connection.Port) port, _ := cuej.NumberToInt(connection.Port)
info := cnsm.NewMatchInfo(connection.IP, port) info := cnsm.NewMatchInfo(connection.IP, port)
if s.m.IsPrePacket() { if s.m.IsPrePacket() {
result["PacketType"] = "Pre"
buf := make([]byte, 1024) buf := make([]byte, 1024)
n, _ := conn.Read(buf) n, _ := conn.Read(buf)
p := cnsm.NewPacket(buf, n) p := cnsm.NewPacket(buf, n)
if !s.m.Match(info, 0, p) { if !s.m.Match(info, 0, p) {
result["Packet"] = convertBase64(buf) return fmt.Errorf("Not Matched")
result["Error"] = "Not Matched"
return result, nil
} }
for i := 0; i < s.m.PacketCount(); i++ { 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) p := cnsm.NewPacket(buf, n)
if s.m.Match(info, i+1, p) == false { if !s.m.Match(info, i+1, p) {
result["Packet"] = convertBase64(buf) return fmt.Errorf("Not Matched")
result["Error"] = "Not Matched"
return result, nil
} }
} }
} else { return nil
result["PacketType"] = "Post"
} else {
for i := 0; i < s.m.PacketCount(); i++ { for i := 0; i < s.m.PacketCount(); i++ {
pack := s.m.Packet(i) pack := s.m.Packet(i)
conn.Write(pack.Buffer) conn.Write(pack.Buffer)
@ -211,15 +208,13 @@ func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.
} }
p := cnsm.NewPacket(buf, n) p := cnsm.NewPacket(buf, n)
if s.m.Match(info, i, p) == false { if !s.m.Match(info, i, p) {
result["Packet"] = convertBase64(buf) return fmt.Errorf("Not Matched")
result["Error"] = "Not Matched"
return result, nil
} }
} }
} }
return nil, nil return nil
} }
func convertBase64(buf []byte) string { func convertBase64(buf []byte) string {

View File

@ -1,11 +1,13 @@
package ssh package ssh
import ( import (
"time"
"git.loafle.net/commons/logging-go"
cnsms "git.loafle.net/commons/service_matcher-go/ssh" cnsms "git.loafle.net/commons/service_matcher-go/ssh"
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig" ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
"git.loafle.net/overflow/container_network/crawler/health" "git.loafle.net/overflow/container_network/crawler/health"
"git.loafle.net/overflow/crawler-go" "git.loafle.net/overflow/crawler-go"
"git.loafle.net/commons/logging-go"
) )
type SSHHealthCrawler struct { type SSHHealthCrawler struct {
@ -32,17 +34,27 @@ func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, e
if cErr != nil { if cErr != nil {
sckEnum := health.ToSocketErrorEnum(cErr) sckEnum := health.ToSocketErrorEnum(cErr)
result["Error"] = sckEnum.String() result["ERR"] = sckEnum.String()
logging.Logger().Error("SSHHealthCrawler Connection Error: ", sckEnum.String()) logging.Logger().Error("SSHHealthCrawler Connection Error: ", sckEnum.String())
return result, cErr return result, cErr
} }
for _, mci := range config.MetaCollectionItems {
rss, err := c.CheckHealth1(config, conn, result) switch mci.Key {
if err != nil { case "service.health.response_time":
return nil, err 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 { func NewCrawler() crawler.Crawler {

View File

@ -1,26 +1,34 @@
package ssh package ssh
import ( import (
"encoding/json"
"testing" "testing"
"git.loafle.net/overflow/commons-go/model/meta"
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig" ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {
config := &ocmsc.SensorConfig{} config := &ocmsc.SensorConfig{}
config.Target = &ocmsc.Target{} config.Connection = &ocmsc.SensorConfigConnection{
config.Target.Connection = &ocmsc.Connection{ MetaIPTypeKey: "V4",
IP: "192.168.1.215", IP: "192.168.1.103",
Port: json.Number(22), MetaPortTypeKey: "TCP",
PortType: "tcp", Port: "22",
SSL: false, MetaCryptoTypeKey: "SSL",
} }
metaCollectionItem := &meta.MetaCollectionItem{
Key: "service.health.response_time",
}
config.MetaCollectionItems = make([]*meta.MetaCollectionItem, 1)
config.MetaCollectionItems[0] = metaCollectionItem
c := NewCrawler() c := NewCrawler()
rss, err := c.Get(config) rss, err := c.Get(config)
assert.Nil(t, err) if err != nil {
t.Log(err.Error())
}
assert.NotNil(t, rss) assert.NotNil(t, rss)
} }