This commit is contained in:
crusader 2018-07-04 18:33:58 +09:00
parent 5089fc2478
commit a7480afdd5
2 changed files with 72 additions and 0 deletions

View File

@ -4,6 +4,8 @@ import (
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"net" "net"
"os"
"syscall"
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"
@ -73,6 +75,21 @@ func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Con
return conn, nil 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 // Duplication Method
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) { func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) {
result := make(map[string]string, 0) result := make(map[string]string, 0)

View File

@ -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
}