This commit is contained in:
crusader 2018-04-12 18:38:04 +09:00
parent 354edcc78a
commit 796da7bbfe
78 changed files with 1156 additions and 22 deletions

View File

@ -1,13 +0,0 @@
package external
import (
"git.loafle.net/overflow/commons-go/config/external/grpc"
"git.loafle.net/overflow/commons-go/config/external/kafka"
"git.loafle.net/overflow/commons-go/config/external/redis"
)
type External struct {
GRPC *grpc.GRPC `json:"grpc"`
Kafka *kafka.Kafka `json:"kafka"`
Redis *redis.Redis `json:"redis"`
}

View File

@ -0,0 +1,5 @@
package interfaces
type EndableStarter interface {
EndableStart() (<-chan error, error)
}

View File

@ -0,0 +1,6 @@
package interfaces
type Service interface {
Starter
Stopper
}

View File

@ -0,0 +1,5 @@
package interfaces
type Starter interface {
Start() error
}

View File

@ -0,0 +1,7 @@
package interfaces
import "context"
type Stopper interface {
Stop(ctx context.Context) error
}

108
core/util/executer.go Normal file
View File

@ -0,0 +1,108 @@
package util
import (
"context"
"fmt"
"reflect"
"time"
"git.loafle.net/overflow/commons-go/core/interfaces"
)
func ExecuteStarters(instances []interface{}, targetTypes []reflect.Type, reverse bool) error {
ilen := len(instances)
tlen := len(targetTypes)
if 0 == ilen || 0 == tlen {
return nil
}
if reverse {
for indexI := tlen - 1; indexI >= 0; indexI-- {
if err := executeStarter(instances, targetTypes[indexI]); nil != err {
return err
}
}
} else {
for indexI := 0; indexI < tlen; indexI++ {
if err := executeStarter(instances, targetTypes[indexI]); nil != err {
return err
}
}
}
return nil
}
func executeStarter(instances []interface{}, t reflect.Type) error {
i := findInstanceByType(instances, t)
if nil == i {
return fmt.Errorf("Cannot find instance[%s]", t.String())
}
s, ok := i.(interfaces.Starter)
if !ok {
return fmt.Errorf("Instance is not Starter[%s]", t.String())
}
if err := s.Start(); nil != err {
return err
}
return nil
}
func ExecuteStoppers(instances []interface{}, targetTypes []reflect.Type, reverse bool) error {
ilen := len(instances)
tlen := len(targetTypes)
if 0 == ilen || 0 == tlen {
return nil
}
if reverse {
for indexI := tlen - 1; indexI >= 0; indexI-- {
if err := executeStarter(instances, targetTypes[indexI]); nil != err {
return err
}
}
} else {
for indexI := 0; indexI < tlen; indexI++ {
if err := executeStarter(instances, targetTypes[indexI]); nil != err {
return err
}
}
}
return nil
}
func executeStopper(instances []interface{}, t reflect.Type) error {
i := findInstanceByType(instances, t)
if nil == i {
return fmt.Errorf("Cannot find instance[%s]", t.String())
}
s, ok := i.(interfaces.Stopper)
if !ok {
return fmt.Errorf("Instance is not Stopper[%s]", t.String())
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Stop(ctx); nil != err {
return err
}
return nil
}
func findInstanceByType(instances []interface{}, t reflect.Type) interface{} {
if 0 == len(instances) {
return nil
}
for _, i := range instances {
if reflect.TypeOf(i) == t {
return i
}
}
return nil
}

37
core/util/timestamp.go Normal file
View File

@ -0,0 +1,37 @@
package util
import (
"fmt"
"strconv"
"time"
)
type Timestamp time.Time
func (t Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(t).Unix()
stamp := fmt.Sprint(ts * 1000)
return []byte(stamp), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*t = Timestamp(time.Unix(int64(ts)/1000, 0))
return nil
}
func (t Timestamp) String() string {
return time.Time(t).String()
}
func Now() Timestamp {
return Timestamp(time.Now())
}
func Date(year int, month time.Month, day int) Timestamp {
return Timestamp(time.Date(year, month, day, 0, 0, 0, 0, time.UTC))
}

View File

@ -0,0 +1,10 @@
package model
type DiscoveryHost struct {
FirstScanRange string `json:"firstScanRange,omitempty"`
LastScanRange string `json:"lastScanRange,omitempty"`
ExcludeHosts []string `json:"excludeHosts,omitempty"`
IncludeHosts []string `json:"includeHosts,omitempty"`
DiscoveryPort *DiscoveryPort `json:"discoveryPort,omitempty"`
}

View File

@ -0,0 +1,28 @@
package model
type DiscoveryPort struct {
FirstScanRange int `json:"firstScanRange,omitempty"`
LastScanRange int `json:"lastScanRange,omitempty"`
ExcludePorts []int `json:"excludePorts,omitempty"`
IncludeTCP bool `json:"includeTCP,omitempty"`
IncludeUDP bool `json:"includeUDP,omitempty"`
DiscoveryService *DiscoveryService `json:"discoveryService,omitempty"`
}
func (dp *DiscoveryPort) Contains(port int) bool {
if dp.FirstScanRange > port {
return false
}
if dp.LastScanRange < port {
return false
}
for _, p := range dp.ExcludePorts {
if p == port {
return false
}
}
return true
}

View File

@ -0,0 +1,5 @@
package model
type DiscoveryService struct {
IncludeServices []string `json:"includeServices,omitempty"`
}

View File

@ -0,0 +1,7 @@
package model
type DiscoveryZone struct {
ExcludePatterns []string `json:"excludePatterns,omitempty"`
DiscoveryHost *DiscoveryHost `json:"discoveryHost,omitempty"`
}

18
discovery/model/Host.go Normal file
View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type Host struct {
Zone *Zone `json:"zone"`
ID json.Number `json:"id,Number,omitempty"`
IP string `json:"ip,omitempty"`
Mac string `json:"mac,omitempty"`
OS string `json:"os,omitempty"`
DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"`
}

21
discovery/model/Port.go Normal file
View File

@ -0,0 +1,21 @@
package model
import (
"encoding/json"
"github.com/google/gopacket"
"git.loafle.net/overflow/commons-go/core/util"
)
type Port struct {
Host *Host `json:"host,omitempty"`
ID json.Number `json:"id,Number,omitempty"`
PortType string `json:"portType,omitempty"`
PortNumber json.Number `json:"portNumber,omitempty"`
DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"`
UDPLayer gopacket.Layer `json:"-"`
}

View File

@ -0,0 +1,17 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type Service struct {
Port *Port `json:"port,omitempty"`
ID json.Number `json:"id,Number,omitempty"`
CryptoType string `json:"cryptoType,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"`
}

20
discovery/model/Zone.go Normal file
View File

@ -0,0 +1,20 @@
package model
import (
"encoding/json"
"sync"
"git.loafle.net/overflow/commons-go/core/util"
)
type Zone struct {
ID json.Number `json:"id,Number,omitempty"`
Network string `json:"network,omitempty"`
IP string `json:"ip,omitempty"`
Iface string `json:"iface,omitempty"`
Mac string `json:"mac,omitempty"`
DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"`
mtx sync.RWMutex `json:"-"`
}

13
domain/model/Domain.go Normal file
View File

@ -0,0 +1,13 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type Domain struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,15 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
member "git.loafle.net/overflow/commons-go/member/model"
)
type DomainMember struct {
ID json.Number `json:"id,Number,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Member *member.Member `json:"member,omitempty"`
Domain *Domain `json:"domain,omitempty"`
}

7
external/config/external.go vendored Normal file
View File

@ -0,0 +1,7 @@
package config
type External struct {
GRPC *GRPC `json:"grpc"`
Kafka *Kafka `json:"kafka"`
Redis *Redis `json:"redis"`
}

View File

@ -1,4 +1,4 @@
package grpc package config
type GRPC struct { type GRPC struct {
Network string `json:"network"` Network string `json:"network"`

View File

@ -1,4 +1,4 @@
package kafka package config
type Kafka struct { type Kafka struct {
Network string `json:"network"` Network string `json:"network"`

View File

@ -1,4 +1,4 @@
package redis package config
type Redis struct { type Redis struct {
Network string `json:"network"` Network string `json:"network"`

View File

@ -1,2 +1,4 @@
package: git.loafle.net/overflow/commons-go package: git.loafle.net/overflow/commons-go
import: [] import:
- package: github.com/google/gopacket
version: ^1.1.14

18
infra/model/Infra.go Normal file
View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
probe "git.loafle.net/overflow/commons-go/probe/model"
target "git.loafle.net/overflow/commons-go/target/model"
)
type Infra struct {
ID json.Number `json:"id,Number,omitempty"`
MetaInfraType *meta.MetaInfraType `json:"type,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Probe *probe.Probe `json:"probe,omitempty"`
Target *target.Target `json:"target,omitempty"`
}

15
infra/model/InfraHost.go Normal file
View File

@ -0,0 +1,15 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type InfraHost struct {
Infra
InfraOS *InfraOS `json:"os,omitempty"`
IP json.Number `json:"ip,omitempty"`
Mac json.Number `json:"mac,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,9 @@
package model
import "git.loafle.net/overflow/commons-go/core/util"
type InfraMachine struct {
Infra
Meta string `json:"meta,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

14
infra/model/InfraOS.go Normal file
View File

@ -0,0 +1,14 @@
package model
import (
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type InfraOS struct {
Infra
InfraMachine *InfraMachine `json:"machine,omitempty"`
Meta string `json:"meta,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"`
}

View File

@ -0,0 +1,10 @@
package model
import "git.loafle.net/overflow/commons-go/core/util"
type InfraOSApplication struct {
Infra
InfraOS *InfraOS `json:"os,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,12 @@
package model
import (
"git.loafle.net/overflow/commons-go/core/util"
)
type InfraOSDaemon struct {
Infra
InfraOS *InfraOS `json:"os,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type InfraOSPort struct {
Infra
InfraOS *InfraOS `json:"os,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Port json.Number `json:"port,omitempty"`
PortType string `json:"portType,omitempty"`
MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"`
TLSType bool `json:"tlsType,omitempty"`
}

View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type InfraServiceApplication struct {
Infra
InfraHost *InfraHost `json:"host,omitempty"`
PortType string `json:"portType,omitempty"`
Port json.Number `json:"port,omitempty"`
MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
TLSType bool `json:"tlsType,omitempty"`
}

19
member/model/Member.go Normal file
View File

@ -0,0 +1,19 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type Member struct {
ID json.Number `json:"id,Number,omitempty"`
Email string `json:"email,omitempty"`
Pw string `json:"pw,omitempty"`
Name string `json:"name,omitempty"`
Phone string `json:"phone,omitempty"`
CompanyName string `json:"companyName,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Status *meta.MetaMemberStatus `json:"status,omitempty"`
}

14
meta/model/MetaCrawler.go Normal file
View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaCrawler struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,13 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaHistoryType struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,13 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaInfraType struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaInfraVendor struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
MetaInfraType *MetaInfraType `json:"metaInfraType,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
import "encoding/json"
type MetaMemberStatus struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
import "encoding/json"
type MetaNoAuthProbeStatus struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
import "encoding/json"
type MetaProbeStatus struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
}

View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaProbeTaskType struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,16 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaSensorDisplayItem struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
Crawler *MetaCrawler `json:"crawler,omitempty"`
Unit *MetaSensorItemUnit `json:"unit,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
ItemType *MetaSensorItemType `json:"itemType,omitempty"`
}

View File

@ -0,0 +1,15 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaSensorItem struct {
ID json.Number `json:"id,Number,omitempty"`
MetaSensorItemType *MetaSensorItemType `json:"metaSensorItemType,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaSensorItemKey struct {
ID json.Number `json:"id,Number,omitempty"`
Item *MetaSensorItem `json:"item,omitempty"`
Key string `json:"key,omitempty"`
Froms string `json:"froms,omitempty"`
Option string `json:"option,omitempty"`
Crawler *MetaCrawler `json:"crawler,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Unit *MetaSensorItemUnit `json:"unit,omitempty"`
}

View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaSensorItemType struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type MetaSensorItemUnit struct {
ID json.Number `json:"id,Number,omitempty"`
Unit string `json:"unit,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Mark string `json:"mark,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
import "encoding/json"
type MetaSensorStatus struct {
ID json.Number `json:"id,Number,omitempty"`
Name string `json:"name,omitempty"`
}

View File

@ -1,9 +1,9 @@
package probe package config
import "time" import "time"
const ( const (
AuthConfigFileName = "auth.json" ConfigFileName = "auth.json"
) )
type AuthStateType int type AuthStateType int

View File

@ -0,0 +1,18 @@
package constants
const (
HTTPEntry_Auth = "/auth"
)
const (
HTTPRequestHeaderKey_NoAuthProbe_Method = "overFlow-NoAuthProbe-Method"
HTTPRequestHeaderKey_NoAuthProbe_Info = "overFlow-NoAuthProbe-Info"
HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey = "overFlow-NoAuthProbe-TempProbeKey"
HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey = "overFlow-NoAuthProbe-SetTempProbeKey"
)
const (
HTTPRequestHeaderValue_NoAuthProbe_Method_Regist = "REGIST"
HTTPRequestHeaderValue_NoAuthProbe_Method_Connect = "CONNECT"
)

View File

@ -0,0 +1,21 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
domain "git.loafle.net/overflow/commons-go/domain/model"
meta "git.loafle.net/overflow/commons-go/meta/model"
probe "git.loafle.net/overflow/commons-go/probe/model"
)
type NoAuthProbe struct {
ID json.Number `json:"id,Number,omitempty"`
Description string `json:"description,omitempty"`
Status *meta.MetaNoAuthProbeStatus `json:"status,omitempty"`
TempProbeKey string `json:"tempProbeKey,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
APIKey string `json:"apiKey,omitempty"`
Domain *domain.DomainMember `json:"domain,omitempty"`
Probe *probe.Probe `json:"probe,omitempty"`
}

View File

@ -0,0 +1,6 @@
package model
type NoAuthProbeDescription struct {
Host *NoAuthProbeDescriptionHost `json:"host,omitempty"`
Network *NoAuthProbeDescriptionNetwork `json:"network,omitempty"`
}

View File

@ -0,0 +1,11 @@
package model
type NoAuthProbeDescriptionHost struct {
Name string `json:"name,omitempty"`
OS string `json:"os,omitempty"`
Platform string `json:"paltform,omitempty"`
PlatformFamily string `json:"platformFamily,omitempty"`
PlatformVersion string `json:"platformVersion,omitempty"`
KernelVersion string `json:"kernelVersion,omitempty"`
HostID string `json:"hostID,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
type NoAuthProbeDescriptionNetwork struct {
Name string `json:"name,omitempty"`
Address string `json:"address,omitempty"`
Gateway string `json:"gateway,omitempty"`
MacAddress string `json:"macAddress,omitempty"`
}

View File

@ -0,0 +1,18 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
member "git.loafle.net/overflow/commons-go/member/model"
)
type Notification struct {
ID json.Number `json:"id,Number,omitempty"`
CreateDate util.Timestamp `json:"date,omitempty"`
Title string `json:"title,omitempty"`
Message string `json:"message,omitempty"`
Member *member.Member `json:"member,omitempty"`
ConfirmDate util.Timestamp `json:"confirmDate,omitempty"`
Url string `json:"url,omitempty"`
}

View File

@ -1,4 +1,4 @@
package probe package config
type Account struct { type Account struct {
Name string `json:"name"` Name string `json:"name"`

View File

@ -1,4 +1,4 @@
package probe package config
type Central struct { type Central struct {
Host string `json:"host"` Host string `json:"host"`

View File

@ -1,4 +1,4 @@
package probe package config
type ProbeStateType int type ProbeStateType int

View File

@ -0,0 +1,17 @@
package constants
const (
HTTPEntry_Probe = "/probe"
HTTPEntry_Data = "/data"
)
const (
HTTPRequestHeaderKey_Probe_Method = "overFlow-Probe-Method"
HTTPRequestHeaderKey_Probe_ProbeKey = "overFlow-Probe-ProbeKey"
HTTPResponseHeaderKey_Probe_SetEncryptionKey = "overFlow-Probe-SetEncryptionKey"
)
const (
HTTPRequestHeaderValue_Probe_Method_Connect = "CONNECT"
)

25
probe/model/Probe.go Normal file
View File

@ -0,0 +1,25 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
domain "git.loafle.net/overflow/commons-go/domain/model"
member "git.loafle.net/overflow/commons-go/member/model"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type Probe struct {
ID json.Number `json:"id,Number,omitempty"`
Status *meta.MetaProbeStatus `json:"status,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Domain *domain.Domain `json:"domain,omitempty"`
ProbeKey string `json:"probeKey,omitempty"`
EncryptionKey string `json:"encryptionKey,omitempty"`
TargetCount int `json:"targetCount,omitempty"`
SensorCount int `json:"sensorCount,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Cidr string `json:"cidr,omitempty"`
AuthorizeDate util.Timestamp `json:"authorizeDate,omitempty"`
AuthorizeMember *member.Member `json:"authorizeMember,omitempty"`
}

20
probe/model/ProbeTask.go Normal file
View File

@ -0,0 +1,20 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type ProbeTask struct {
ID json.Number `json:"id,Number,omitempty"`
MetaProbeTaskType *meta.MetaProbeTaskType `json:"metaProbeTaskType,omitempty"`
Probe *Probe `json:"probe,omitempty"`
Data string `json:"data,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
SendDate util.Timestamp `json:"sendDate,omitempty"`
StartDate util.Timestamp `json:"startDate,omitempty"`
EndDate util.Timestamp `json:"endDate,omitempty"`
Succeed bool `json:"succeed,omitempty"`
}

19
sensor/model/Sensor.go Normal file
View File

@ -0,0 +1,19 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
target "git.loafle.net/overflow/commons-go/target/model"
)
type Sensor struct {
ID json.Number `json:"id,Number,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
Description string `json:"description,omitempty"`
MetaSensorStatus *meta.MetaSensorStatus `json:"status,omitempty"`
Target *target.Target `json:"target,omitempty"`
Crawler *meta.MetaCrawler `json:"crawler,omitempty"`
CrawlerInputItems string `json:"crawlerInputItems,omitempty"`
}

View File

@ -0,0 +1,15 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type SensorItem struct {
ID json.Number `json:"id,Number,omitempty"`
Sensor *Sensor `json:"sensor,omitempty"`
MetaSensorItem *meta.MetaSensorItem `json:"item,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
}

View File

@ -0,0 +1,13 @@
package model
import (
"encoding/json"
meta "git.loafle.net/overflow/commons-go/meta/model"
)
type SensorItemDependency struct {
ID json.Number `json:"id,Number,omitempty"`
DisplayItem *meta.MetaSensorDisplayItem `json:"displayItem,omitempty"`
SensorItem *meta.MetaSensorItemKey `json:"sensorItem,omitempty"`
}

View File

@ -0,0 +1,10 @@
package model
import "encoding/json"
type Connection struct {
IP string `json:"ip,omitempty"`
Port json.Number `json:"port,Number,omitempty"`
PortType string `json:"portType,omitempty"`
SSL bool `json:"ssl,omitempty"`
}

View File

@ -0,0 +1,6 @@
package model
type Crawler struct {
Name string `json:"name,omitempty"`
Container string `json:"container,omitempty"`
}

View File

@ -0,0 +1,7 @@
package model
type Item struct {
Keys []Keys `json:"keys,omitempty"`
QueryInfo *QueryInfo `json:"queryInfo,omitempty"`
MappingInfo *MappingInfo `json:"mappingInfo,omitempty"`
}

View File

@ -0,0 +1,6 @@
package model
type Keys struct {
Metric string `json:"metric,omitempty"`
Key string `json:"key,omitempty"`
}

View File

@ -0,0 +1,8 @@
package model
type MappingInfo struct {
ParseDirection string `json:"parseDirection,omitempty"`
ArrayColumns []string `json:"arrayColumns,omitempty"`
KeyColumns []string `json:"keyColumns,omitempty"`
ValueColumn string `json:"valueColumn,omitempty"`
}

View File

@ -0,0 +1,6 @@
package model
type QueryInfo struct {
Query string `json:"query,omitempty"`
Extend map[string]interface{} `json:"extend,omitempty"`
}

View File

@ -0,0 +1,46 @@
package model
import "encoding/json"
type ResultSetter interface {
AddRow(row []string)
GetMeta() map[string]json.Number
GetData() map[string]string
}
type ResultSet struct {
Item *Item `json:"item,omitempty"`
Rows [][]string `json:"rows,omitempty"`
Meta map[string]json.Number `json:"meta,omitempty"`
}
func (rs *ResultSet) AddRow(row []string) {
rs.Rows = append(rs.Rows, row)
}
func (rs *ResultSet) GetMeta() map[string]json.Number {
return rs.Meta
}
func NewResultSet(item *Item) ResultSetter {
if nil == item.MappingInfo {
item.MappingInfo = &MappingInfo{}
}
rs := &ResultSet{}
rs.Item = item
rs.Rows = make([][]string, 0)
switch item.MappingInfo.ParseDirection {
case "row":
rsr := &ResultSetRow{}
rsr.ResultSet = rs
rsr.setMeta()
return rsr
default:
rsc := &ResultSetCol{}
rsc.ResultSet = rs
rsc.setMeta()
return rsc
}
}

View File

@ -0,0 +1,85 @@
package model
import (
"encoding/json"
"fmt"
"strconv"
"strings"
cuej "git.loafle.net/commons_go/util/encoding/json"
)
type ResultSetCol struct {
*ResultSet
}
func (rsc *ResultSetCol) setMeta() {
meta := rsc.Item.Keys
arrayColumns := rsc.Item.MappingInfo.ArrayColumns
if nil == rsc.Meta {
rsc.Meta = make(map[string]json.Number, 0)
}
for i := 0; i < len(meta); i++ {
rsc.Meta[meta[i].Key] = json.Number(strconv.Itoa(i))
}
if nil != arrayColumns {
for i := 0; i < len(arrayColumns); i++ {
if _, ok := rsc.Meta[arrayColumns[i]]; ok {
continue
}
rsc.Meta[arrayColumns[i]] = json.Number(strconv.Itoa(i + len(meta)))
}
}
}
func (rsc *ResultSetCol) GetData() map[string]string {
return rsc.parse()
}
func (rsc *ResultSetCol) parse() map[string]string {
metrics := rsc.Item.Keys
arrayColumns := rsc.Item.MappingInfo.ArrayColumns
rm := make(map[string]string, 0)
for i := 0; i < len(rsc.Rows); i++ {
row := rsc.Rows[i]
for j := 0; j < len(row); j++ {
arrayValue := make([]string, 0)
if nil != arrayColumns {
for k := 0; k < len(arrayColumns); k++ {
idxN := rsc.Meta[arrayColumns[k]]
idx, _ := cuej.NumberToInt(idxN)
arrayValue = append(arrayValue, row[idx])
}
}
for k := 0; k < len(metrics); k++ {
metric := metrics[k].Metric
metric = rsc.convertMetric(metric, arrayValue)
rm[metric] = row[k]
}
}
}
return rm
}
func (rsc *ResultSetCol) convertMetric(metric string, arrayValue []string) string {
if nil == arrayValue || 0 == len(arrayValue) {
return metric
}
convertChar := "$"
for i := 0; i < len(arrayValue); i++ {
convertStr := fmt.Sprintf("%s%d", convertChar, i)
metric = strings.Replace(metric, convertStr, arrayValue[i], -1)
}
return metric
}

View File

@ -0,0 +1,105 @@
package model
import (
"encoding/json"
"fmt"
"strconv"
"strings"
cuej "git.loafle.net/commons_go/util/encoding/json"
)
type ResultSetRow struct {
*ResultSet
}
func (rsr *ResultSetRow) setMeta() {
meta := make([]string, 0)
arrayColumns := rsr.Item.MappingInfo.ArrayColumns
keyColumns := rsr.Item.MappingInfo.KeyColumns
valueColumn := rsr.Item.MappingInfo.ValueColumn
if nil != arrayColumns {
for _, c := range arrayColumns {
meta = append(meta, c)
}
}
if nil != keyColumns {
for _, c := range keyColumns {
meta = append(meta, c)
}
}
if "" != valueColumn {
meta = append(meta, valueColumn)
}
if nil == rsr.Meta {
rsr.Meta = make(map[string]json.Number, 0)
}
for i := 0; i < len(meta); i++ {
rsr.Meta[meta[i]] = json.Number(strconv.Itoa(i))
}
}
func (rsr *ResultSetRow) GetData() map[string]string {
return rsr.parse()
}
func (rsr *ResultSetRow) parse() map[string]string {
valueColumn := rsr.Item.MappingInfo.ValueColumn
rm := make(map[string]string, 0)
for _, row := range rsr.Rows {
key := rsr.makeKey(row)
if "" == key {
continue
}
idx, _ := cuej.NumberToInt(rsr.Meta[valueColumn])
rm[key] = row[idx]
}
return rm
}
func (rsr *ResultSetRow) makeKey(data []string) string {
metrics := rsr.Item.Keys
arrayColumns := rsr.Item.MappingInfo.ArrayColumns
keyColumns := rsr.Item.MappingInfo.KeyColumns
keys := rsr.Item.Keys
findIndex := -1
Loop:
for _, keyColumn := range keyColumns {
idx, _ := cuej.NumberToInt(rsr.Meta[keyColumn])
row := data[idx]
for i := 0; i < len(keys); i++ {
if row == keys[i].Key {
findIndex = i
break Loop
}
}
}
if 0 > findIndex {
return ""
}
metric := metrics[findIndex].Metric
convertChar := "$"
if nil != arrayColumns {
for i := 0; i < len(arrayColumns); i++ {
convertStr := fmt.Sprintf("%s%d", convertChar, i)
idx, _ := cuej.NumberToInt(rsr.Meta[arrayColumns[i]])
replaceString := data[idx]
metric = strings.Replace(metric, convertStr, "'"+replaceString+"'", -1)
}
}
return metric
}

View File

@ -0,0 +1,5 @@
package model
type Schedule struct {
Interval string `json:"interval,omitempty"`
}

View File

@ -0,0 +1,11 @@
package model
import "encoding/json"
type SensorConfig struct {
ID json.Number `json:"id,Number,omitempty"`
Target *Target `json:"target,omitempty"`
Schedule *Schedule `json:"schedule,omitempty"`
Crawler *Crawler `json:"crawler,omitempty"`
Items []Item `json:"items,omitempty"`
}

View File

@ -0,0 +1,6 @@
package model
type Target struct {
Auth map[string]interface{} `json:"auth,omitempty"`
Connection *Connection `json:"connection,omitempty"`
}

14
target/model/Target.go Normal file
View File

@ -0,0 +1,14 @@
package model
import (
"encoding/json"
"git.loafle.net/overflow/commons-go/core/util"
)
type Target struct {
ID json.Number `json:"id,Number,omitempty"`
CreateDate util.Timestamp `json:"createDate,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Description string `json:"description,omitempty"`
}