dns health sensor

This commit is contained in:
insanity 2018-07-05 18:58:08 +09:00
parent 6c5db94606
commit 7be3d472b2
4 changed files with 52 additions and 23 deletions

View File

@ -6,7 +6,9 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"strings"
"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"
@ -186,6 +188,18 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.C
return nil 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 { func convertBase64(buf []byte) string {
return base64.StdEncoding.EncodeToString(buf) return base64.StdEncoding.EncodeToString(buf)
} }

View File

@ -24,11 +24,29 @@ func (c *DNSHealthCrawler) Auth(auth map[string]interface{}) error {
} }
func (c *DNSHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, 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 { if err != nil {
return nil, err 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 { func NewCrawler() crawler.Crawler {

View File

@ -1,27 +1,32 @@
package dns package dns
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.254",
Port: json.Number(53), MetaPortTypeKey: "UDP",
PortType: "udp", Port: "53",
SSL: false,
} }
metaCollectionItem := &meta.MetaCollectionItem{
Key: "dns.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 {
assert.NotNil(t, rss) t.Log(err.Error())
}
assert.NotNil(t, rss["dns.response_time"])
} }

View File

@ -1,10 +1,6 @@
package ssh package ssh
import ( import (
"fmt"
"strings"
"time"
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"
@ -39,15 +35,11 @@ func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, e
for _, mci := range config.MetaCollectionItems { for _, mci := range config.MetaCollectionItems {
switch mci.Key { switch mci.Key {
case "ssh.response_time": case "ssh.response_time":
start := time.Now().UTC() value, err := c.ResponseTime(config, conn)
if err := c.CheckHealth(config, conn); err != nil { if err != nil {
return nil, err return nil, err
} }
elapsed := time.Since(start) result[mci.Key] = value
if !strings.Contains(elapsed.String(), "ms") {
return nil, fmt.Errorf("Not valid data : %s", elapsed)
}
result[mci.Key] = strings.Split(elapsed.String(), "ms")[0]
break break
default: default:
break break