This commit is contained in:
insanity 2018-07-05 12:06:41 +09:00
parent f040689a24
commit 3228ee9601
4 changed files with 15 additions and 13 deletions

View File

@ -7,7 +7,6 @@ import (
"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"
@ -53,9 +52,10 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
} }
} }
conn, err := net.DialTimeout(network, addr, time.Second*5) conn, err := net.Dial(network, addr)
if err != nil { if err != nil {
return nil, err ee := ToSocketErrorEnum(err)
return nil, fmt.Errorf(ee.String())
} }
switch metaCryptoTypeKey { switch metaCryptoTypeKey {
@ -78,6 +78,9 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
} }
func ToSocketErrorEnum(err error) SocketErrorEnum { func ToSocketErrorEnum(err error) SocketErrorEnum {
if e, ok := err.(net.Error); ok && e.Timeout() {
return SocketErrorEnumTIMOUT
}
n, ok := err.(*net.OpError).Err.(*os.SyscallError).Err.(syscall.Errno) n, ok := err.(*net.OpError).Err.(*os.SyscallError).Err.(syscall.Errno)
if !ok { if !ok {
return SocketErrorEnumUNKNOWN return SocketErrorEnumUNKNOWN
@ -90,6 +93,7 @@ func ToSocketErrorEnum(err error) SocketErrorEnum {
default: default:
return SocketErrorEnumUNKNOWN return SocketErrorEnumUNKNOWN
} }
} }
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.Conn) error { func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.Conn) error {

View File

@ -11,6 +11,7 @@ const (
SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1 SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1
SocketErrorEnumHOSTUNREACH SocketErrorEnumHOSTUNREACH
SocketErrorEnumCONNREFUSED SocketErrorEnumCONNREFUSED
SocketErrorEnumTIMOUT
) )
var ( var (
@ -18,12 +19,14 @@ var (
SocketErrorEnumUNKNOWN: "UNKNOWN", SocketErrorEnumUNKNOWN: "UNKNOWN",
SocketErrorEnumHOSTUNREACH: "HOSTUNREACH", SocketErrorEnumHOSTUNREACH: "HOSTUNREACH",
SocketErrorEnumCONNREFUSED: "CONNREFUSED", SocketErrorEnumCONNREFUSED: "CONNREFUSED",
SocketErrorEnumTIMOUT: "TIMEOUT",
} }
socketErrorEnumKey = map[string]SocketErrorEnum{ socketErrorEnumKey = map[string]SocketErrorEnum{
"UNKNOWN": SocketErrorEnumUNKNOWN, "UNKNOWN": SocketErrorEnumUNKNOWN,
"HOSTUNREACH": SocketErrorEnumHOSTUNREACH, "HOSTUNREACH": SocketErrorEnumHOSTUNREACH,
"CONNREFUSED": SocketErrorEnumCONNREFUSED, "CONNREFUSED": SocketErrorEnumCONNREFUSED,
"TIMEOUT": SocketErrorEnumTIMOUT,
} }
) )

View File

@ -3,7 +3,6 @@ package ssh
import ( import (
"time" "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"
@ -29,13 +28,9 @@ func (c *SSHHealthCrawler) Auth(auth map[string]interface{}) error {
func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) { func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) {
result := make(map[string]string, 0) result := make(map[string]string, 0)
conn, cErr := c.GetConnection(config) conn, err := c.GetConnection(config)
if err != nil {
if cErr != nil { return nil, err
sckEnum := health.ToSocketErrorEnum(cErr)
result["ERR"] = sckEnum.String()
logging.Logger().Error("SSHHealthCrawler Connection Error: ", sckEnum.String())
return result, cErr
} }
defer conn.Close() defer conn.Close()
@ -44,7 +39,7 @@ func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, e
case "service.health.response_time": case "service.health.response_time":
start := time.Now().UTC() start := time.Now().UTC()
if err := c.CheckHealth(config, conn); err != nil { if err := c.CheckHealth(config, conn); err != nil {
result["ERR"] = err.Error() return nil, err
} }
elapsed := time.Since(start) elapsed := time.Since(start)
result[mci.Key] = elapsed.String() result[mci.Key] = elapsed.String()

View File

@ -12,7 +12,7 @@ func TestMatch(t *testing.T) {
config := &ocmsc.SensorConfig{} config := &ocmsc.SensorConfig{}
config.Connection = &ocmsc.SensorConfigConnection{ config.Connection = &ocmsc.SensorConfigConnection{
MetaIPTypeKey: "V4", MetaIPTypeKey: "V4",
IP: "192.168.1.111", IP: "192.168.1.103",
MetaPortTypeKey: "TCP", MetaPortTypeKey: "TCP",
Port: "22", Port: "22",
MetaCryptoTypeKey: "SSL", MetaCryptoTypeKey: "SSL",