diff --git a/crawler/health/SocketHeahthCrawler.go b/crawler/health/SocketHeahthCrawler.go index 6cbaaf0..a1aa377 100644 --- a/crawler/health/SocketHeahthCrawler.go +++ b/crawler/health/SocketHeahthCrawler.go @@ -4,6 +4,8 @@ import ( "crypto/tls" "encoding/base64" "net" + "os" + "syscall" cnsm "git.loafle.net/commons/service_matcher-go" cuej "git.loafle.net/commons/util-go/encoding/json" @@ -73,6 +75,21 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con return conn, nil } +func ToSocketErrorEnum(err error) SocketErrorEnum { + n, ok := err.(*net.OpError).Err.(*os.SyscallError).Err.(syscall.Errno) + if !ok { + return SocketErrorEnumUNKNOWN + } + switch n { + case syscall.EHOSTUNREACH: + return SocketErrorEnumHOSTUNREACH + case syscall.ECONNREFUSED: + return SocketErrorEnumCONNREFUSED + default: + return SocketErrorEnumUNKNOWN + } +} + // Duplication Method func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) { result := make(map[string]string, 0) diff --git a/crawler/health/socket_error.go b/crawler/health/socket_error.go new file mode 100644 index 0000000..e47a226 --- /dev/null +++ b/crawler/health/socket_error.go @@ -0,0 +1,55 @@ +package health + +import ( + "encoding/json" + "fmt" +) + +type SocketErrorEnum int + +const ( + SocketErrorEnumUNKNOWN SocketErrorEnum = iota + 1 + SocketErrorEnumHOSTUNREACH + SocketErrorEnumCONNREFUSED +) + +var ( + socketErrorEnumID = map[SocketErrorEnum]string{ + SocketErrorEnumUNKNOWN: "UNKNOWN", + SocketErrorEnumHOSTUNREACH: "HOSTUNREACH", + SocketErrorEnumCONNREFUSED: "CONNREFUSED", + } + + socketErrorEnumKey = map[string]SocketErrorEnum{ + "UNKNOWN": SocketErrorEnumUNKNOWN, + "HOSTUNREACH": SocketErrorEnumHOSTUNREACH, + "CONNREFUSED": SocketErrorEnumCONNREFUSED, + } +) + +func (e SocketErrorEnum) String() string { + return socketErrorEnumID[e] +} + +func (e SocketErrorEnum) MarshalJSON() ([]byte, error) { + value, ok := socketErrorEnumID[e] + if !ok { + return nil, fmt.Errorf("Invalid EnumType[%s] value", e) + } + return json.Marshal(value) +} + +func (e SocketErrorEnum) UnmarshalJSON(b []byte) error { + var s string + err := json.Unmarshal(b, &s) + if err != nil { + return err + } + + value, ok := socketErrorEnumKey[s] + if !ok { + return fmt.Errorf("Invalid EnumType[%s] value", s) + } + e = value + return nil +}