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"
"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)
}

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) {
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 {

View File

@ -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"])
}

View File

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