From 7be3d472b2b7612db10e9404846ef935b37182a5 Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 5 Jul 2018 18:58:08 +0900 Subject: [PATCH] dns health sensor --- crawler/health/SocketHeahthCrawler.go | 14 ++++++++++++ crawler/health/dns/DNSHealthCrawler.go | 22 ++++++++++++++++-- crawler/health/dns/DNSHealthCrawler_test.go | 25 ++++++++++++--------- crawler/health/ssh/SSHHealthCrawler.go | 14 +++--------- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/crawler/health/SocketHeahthCrawler.go b/crawler/health/SocketHeahthCrawler.go index 8f21278..9162a0e 100644 --- a/crawler/health/SocketHeahthCrawler.go +++ b/crawler/health/SocketHeahthCrawler.go @@ -6,7 +6,9 @@ import ( "fmt" "net" "os" + "strings" "syscall" + "time" cnsm "git.loafle.net/commons/service_matcher-go" cuej "git.loafle.net/commons/util-go/encoding/json" @@ -186,6 +188,18 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.C return nil } +func (s *SocketHealthCrawler) ResponseTime(config *ocmsc.SensorConfig, conn net.Conn) (string, error) { + start := time.Now().UTC() + if err := s.CheckHealth(config, conn); err != nil { + return "", err + } + elapsed := time.Since(start) + if !strings.Contains(elapsed.String(), "ms") { + return "", fmt.Errorf("Not valid data : %s", elapsed) + } + return strings.Split(elapsed.String(), "ms")[0], nil +} + func convertBase64(buf []byte) string { return base64.StdEncoding.EncodeToString(buf) } diff --git a/crawler/health/dns/DNSHealthCrawler.go b/crawler/health/dns/DNSHealthCrawler.go index f513fd5..c8779a9 100644 --- a/crawler/health/dns/DNSHealthCrawler.go +++ b/crawler/health/dns/DNSHealthCrawler.go @@ -24,11 +24,29 @@ func (c *DNSHealthCrawler) Auth(auth map[string]interface{}) error { } func (c *DNSHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) { - rss, err := c.CheckHealth(config) + result := make(map[string]string, 0) + + conn, err := c.GetConnection(config) if err != nil { return nil, err } - return rss, nil + defer conn.Close() + + for _, mci := range config.MetaCollectionItems { + switch mci.Key { + case "dns.response_time": + value, err := c.ResponseTime(config, conn) + if err != nil { + return nil, err + } + result[mci.Key] = value + break + default: + break + } + } + + return result, nil } func NewCrawler() crawler.Crawler { diff --git a/crawler/health/dns/DNSHealthCrawler_test.go b/crawler/health/dns/DNSHealthCrawler_test.go index 56e8513..b66c357 100644 --- a/crawler/health/dns/DNSHealthCrawler_test.go +++ b/crawler/health/dns/DNSHealthCrawler_test.go @@ -1,27 +1,32 @@ package dns 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(53), - PortType: "udp", - SSL: false, + config.Connection = &ocmsc.SensorConfigConnection{ + MetaIPTypeKey: "V4", + IP: "192.168.1.254", + MetaPortTypeKey: "UDP", + Port: "53", } + metaCollectionItem := &meta.MetaCollectionItem{ + Key: "dns.response_time", + } + config.MetaCollectionItems = make([]*meta.MetaCollectionItem, 1) + config.MetaCollectionItems[0] = metaCollectionItem c := NewCrawler() - rss, err := c.Get(config) - assert.Nil(t, err) - assert.NotNil(t, rss) + if err != nil { + t.Log(err.Error()) + } + assert.NotNil(t, rss["dns.response_time"]) } diff --git a/crawler/health/ssh/SSHHealthCrawler.go b/crawler/health/ssh/SSHHealthCrawler.go index 3a87ae5..db4674b 100644 --- a/crawler/health/ssh/SSHHealthCrawler.go +++ b/crawler/health/ssh/SSHHealthCrawler.go @@ -1,10 +1,6 @@ package ssh import ( - "fmt" - "strings" - "time" - 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" @@ -39,15 +35,11 @@ func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, e for _, mci := range config.MetaCollectionItems { switch mci.Key { case "ssh.response_time": - start := time.Now().UTC() - if err := c.CheckHealth(config, conn); err != nil { + value, err := c.ResponseTime(config, conn) + if err != nil { return nil, err } - elapsed := time.Since(start) - if !strings.Contains(elapsed.String(), "ms") { - return nil, fmt.Errorf("Not valid data : %s", elapsed) - } - result[mci.Key] = strings.Split(elapsed.String(), "ms")[0] + result[mci.Key] = value break default: break