diff --git a/.gitignore b/.gitignore index 3733e36..f088aff 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ glide.lock .DS_Store dist/ debug +Gopkg.lock \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock index 3a3588c..052e890 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -135,22 +135,13 @@ [[projects]] name = "github.com/shirou/gopsutil" packages = [ - "cpu", "host", "internal/common", - "mem", - "net", "process" ] revision = "4a180b209f5f494e5923cfce81ea30ba23915877" version = "v2.18.06" -[[projects]] - branch = "master" - name = "github.com/shirou/w32" - packages = ["."] - revision = "bb4de0191aa41b5507caa14b0650cdbddcd9280b" - [[projects]] name = "github.com/valyala/fasthttp" packages = [ diff --git a/_build/bin/auth.json b/_build/bin/auth.json deleted file mode 100644 index 7035f96..0000000 --- a/_build/bin/auth.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "tempKey": "e5f6a72149dc11e890a40242ac120004", - "acceptedDate": 1524807456000 -} \ No newline at end of file diff --git a/_build/bin/config.json b/_build/bin/config.json index 4d0bc26..2ff39d1 100644 --- a/_build/bin/config.json +++ b/_build/bin/config.json @@ -4,7 +4,7 @@ "apiKey": "52abd6fd57e511e7ac52080027658d13" }, "central": { - "address": "192.168.1.101:19100", + "address": "192.168.1.103:19100", "connector": { "reconnectInterval": 5, "reconnectTryTime": 10, diff --git a/_build/config/container/general/postgresql/989238744 b/_build/config/container/general/postgresql/989238744 deleted file mode 100644 index 3029654..0000000 --- a/_build/config/container/general/postgresql/989238744 +++ /dev/null @@ -1,39 +0,0 @@ -{ - "configID" : "989238744", - "target" : { - "connection" : { - "ip" : "192.168.1.50", - "port" : "5432", - "ssl" : false, - "portType" : "tcp" - }, - "auth" : { - "url":"jdbc:postgresql://192.168.1.50:5432/overflow", - "id":"overflow", - "pw":"qwer5795" - }, - "meta": { - } - }, - "schedule" : { - "interval" : "3" - }, - "crawler" : { - "name":"POSTGRESQL", - "container":"GENERAL" - }, - "items" : [ - { - "keys" : [ - { - "metric":"net.pgsql.connection_count", - "key" : "connection_count" - } - ], - "queryInfo":{ - "query" : "select count(pid) as connection_count from pg_catalog.pg_stat_activity where state <> 'idle';" - }, - "mappingInfo" : {} - } - ] -} \ No newline at end of file diff --git a/_build/config_/_ b/_build/config_/_ new file mode 100644 index 0000000..e69de29 diff --git a/_build/config/container/network/postgresql/112103115113108 b/_build/config_/container/network/postgresql/112103115113108 similarity index 100% rename from _build/config/container/network/postgresql/112103115113108 rename to _build/config_/container/network/postgresql/112103115113108 diff --git a/_build/config/container/network/ssh/115115104 b/_build/config_/container/network/ssh/115115104 similarity index 100% rename from _build/config/container/network/ssh/115115104 rename to _build/config_/container/network/ssh/115115104 diff --git a/config/path-sensorconfig.go b/config/path-sensorconfig.go index 395a439..eb72c7e 100644 --- a/config/path-sensorconfig.go +++ b/config/path-sensorconfig.go @@ -11,5 +11,5 @@ func SensorConfigContainerDir(name string) string { } func SensorConfigFilePath(sensorConfig *ocmsc.SensorConfig) string { - return path.Join(SensorConfigContainerDir(sensorConfig.Crawler.Container), sensorConfig.Crawler.Name, sensorConfig.ID.String()) + return path.Join(SensorConfigContainerDir(sensorConfig.Crawler.MetaCrawlerContainerKey), sensorConfig.SensorID.String()) } diff --git a/service/CollectorService.go b/service/CollectorService.go index 5668a6e..4ed3bd6 100644 --- a/service/CollectorService.go +++ b/service/CollectorService.go @@ -3,7 +3,6 @@ package service import ( "fmt" "reflect" - "strconv" "time" cda "git.loafle.net/commons/di-go/annotation" @@ -65,13 +64,13 @@ func (s *CollectorService) addScheduleAll() error { return nil } - for _, sensorConfig := range sensorConfigs { - interval, err := strconv.ParseInt(sensorConfig.Schedule.Interval, 10, 64) - if nil != err { - return fmt.Errorf("Cannot convert interval[%s] %v", sensorConfig.Schedule.Interval, err) - } - s.addSchedule(interval, sensorConfig) - } + //for _, sensorConfig := range sensorConfigs { + // interval, err := strconv.ParseInt(sensorConfig.Schedule.Interval, 10, 64) + // if nil != err { + // return fmt.Errorf("Cannot convert interval[%s] %v", sensorConfig.Schedule.Interval, err) + // } + // s.addSchedule(interval, sensorConfig) + //} return nil } diff --git a/service/MetricService.go b/service/MetricService.go index e8ab709..131c2f6 100644 --- a/service/MetricService.go +++ b/service/MetricService.go @@ -49,14 +49,14 @@ func (s *MetricService) StartService() error { return fmt.Errorf("MetricService: StartService failed %v", err) } - s.connector = connector - - readChan, writeChan, err := s.connector.Connect() - if nil != err { - return fmt.Errorf("MetricService: StartService failed %v", err) - } - s.readChan = readChan - s.writeChan = writeChan + //s.connector = connector + // + //readChan, writeChan, err := s.connector.Connect() + //if nil != err { + // return fmt.Errorf("MetricService: StartService failed %v", err) + //} + //s.readChan = readChan + //s.writeChan = writeChan return nil } diff --git a/service/SensorConfigService.go b/service/SensorConfigService.go index bc45b23..f766285 100644 --- a/service/SensorConfigService.go +++ b/service/SensorConfigService.go @@ -7,10 +7,11 @@ import ( "os" "path" "reflect" + "compress/gzip" cda "git.loafle.net/commons/di-go/annotation" cdr "git.loafle.net/commons/di-go/registry" - logging "git.loafle.net/commons/logging-go" + "git.loafle.net/commons/logging-go" occp "git.loafle.net/overflow/commons-go/config/probe" ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig" ocsp "git.loafle.net/overflow/commons-go/service/probe" @@ -18,6 +19,9 @@ import ( // For annotation _ "git.loafle.net/overflow/commons-go/core/annotation" + "crypto/cipher" + "crypto/des" + "bytes" ) var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil)) @@ -31,6 +35,7 @@ type SensorConfigService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` ContainerService *ContainerService `annotation:"@Inject()"` + ProbeClientService *ProbeClientService `annotation:"@Inject()"` sensorConfigs map[string]*ocmsc.SensorConfig sensorConfigsPerContainer map[occp.ContainerType][]*ocmsc.SensorConfig @@ -80,7 +85,9 @@ func (s *SensorConfigService) SendInitConfig(containerType occp.ContainerType) e } func (s *SensorConfigService) AddConfig(tempFilePath string) error { - sc, buf, err := s.loadConfigFile(tempFilePath) + //sc, buf, err := s.loadConfigFile(tempFilePath) + + sc, buf, err := s.loadConfigFile1(tempFilePath) if nil != err { return err } @@ -94,12 +101,12 @@ func (s *SensorConfigService) AddConfig(tempFilePath string) error { return err } - s.sensorConfigs[sc.ID.String()] = sc - s.sortSensorConfigPerContainer() - - if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.AddConfig", sc); nil != err { - return err - } + //s.sensorConfigs[sc.ID.String()] = sc + //s.sortSensorConfigPerContainer() + // + //if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.AddConfig", sc); nil != err { + // return err + //} return nil } @@ -119,13 +126,13 @@ func (s *SensorConfigService) UpdateConfig(tempFilePath string) error { return err } - delete(s.sensorConfigs, sc.ConfigID) - s.sensorConfigs[sc.ID.String()] = sc - s.sortSensorConfigPerContainer() - - if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.UpdateConfig", sc); nil != err { - return err - } + //delete(s.sensorConfigs, sc.ConfigID) + //s.sensorConfigs[sc.ID.String()] = sc + //s.sortSensorConfigPerContainer() + // + //if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.UpdateConfig", sc); nil != err { + // return err + //} return nil } @@ -145,9 +152,9 @@ func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error { delete(s.sensorConfigs, sensorConfigID) s.sortSensorConfigPerContainer() - if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.RemoveConfig", sensorConfigID); nil != err { - return err - } + //if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.RemoveConfig", sensorConfigID); nil != err { + // return err + //} return nil } @@ -198,10 +205,14 @@ func (s *SensorConfigService) loadConfigDir(dirPath string) error { func (s *SensorConfigService) loadConfigFile(filePath string) (*ocmsc.SensorConfig, []byte, error) { logging.Logger().Debugf("filePath: %s", filePath) buf, err := ioutil.ReadFile(filePath) + if nil != err { return nil, nil, err } + // probe ecncreiption key decode + // gzip decode + var m = &ocmsc.SensorConfig{} if err := json.Unmarshal(buf, m); nil != err { return nil, nil, err @@ -210,6 +221,40 @@ func (s *SensorConfigService) loadConfigFile(filePath string) (*ocmsc.SensorConf return m, buf, nil } +func (s *SensorConfigService) loadConfigFile1(decodeStr string) (*ocmsc.SensorConfig, []byte, error) { + //keyStr := s.ProbeClientService.EncryptionKey + keyStr := "$2a$10$rPZYValfYvsVb8oDDz7gkuuf1FpqVnG8xbxQa.NhKdH7WDi.H2UD." + key := []byte(keyStr) + iv := []byte(keyStr) + block, err := des.NewCipher(key) + + if nil != err { + return nil, nil, err + } + blockMode := cipher.NewCBCDecrypter(block, iv) + origData := make([]byte, len(decodeStr)) + blockMode.CryptBlocks(origData, []byte(decodeStr)) + origData = s.PKCS5UnPadding(origData) + + gr, err := gzip.NewReader(bytes.NewBuffer(origData)) + defer gr.Close() + data, err := ioutil.ReadAll(gr) + + var m = &ocmsc.SensorConfig{} + if err := json.Unmarshal(data, m); nil != err { + return nil, nil, err + } + + fmt.Println(string(data)) + return m, data, nil +} + +func (s *SensorConfigService) PKCS5UnPadding(src []byte) []byte { + length := len(src) + unpadding := int(src[length-1]) + return src[:(length - unpadding)] +} + func (s *SensorConfigService) sortSensorConfigPerContainer() { if nil == s.sensorConfigs || 0 == len(s.sensorConfigs) { return @@ -218,8 +263,8 @@ func (s *SensorConfigService) sortSensorConfigPerContainer() { s.sensorConfigsPerContainer = nil s.sensorConfigsPerContainer = make(map[occp.ContainerType][]*ocmsc.SensorConfig) - for _, sensorConfig := range s.sensorConfigs { - containerType := occp.ToContainerType(sensorConfig.Crawler.Container) - s.sensorConfigsPerContainer[containerType] = append(s.sensorConfigsPerContainer[containerType], sensorConfig) - } + //for _, sensorConfig := range s.sensorConfigs { + // containerType := occp.ToContainerType(sensorConfig.Crawler.Container) + // s.sensorConfigsPerContainer[containerType] = append(s.sensorConfigsPerContainer[containerType], sensorConfig) + //} } diff --git a/service/SensorService.go b/service/SensorService.go index 7993ebf..1c500c9 100644 --- a/service/SensorService.go +++ b/service/SensorService.go @@ -1,6 +1,7 @@ package service import ( + b64 "encoding/base64" "reflect" cda "git.loafle.net/commons/di-go/annotation" @@ -8,6 +9,7 @@ import ( // For annotation _ "git.loafle.net/overflow/commons-go/core/annotation" + ) var SensorServiceType = reflect.TypeOf((*SensorService)(nil)) @@ -20,6 +22,7 @@ type SensorService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` ContainerService *ContainerService `annotation:"@Inject()"` + SensorConfigService *SensorConfigService `annotation:"@Inject()"` } func (s *SensorService) InitService() error { @@ -50,6 +53,16 @@ func (s *SensorService) StopSensor(id int64) error { func (s *SensorService) AddSensor(sensorConfigBase64 string) error { + // base64 decode + + bufByte, err := b64.StdEncoding.DecodeString(sensorConfigBase64) + + if nil == bufByte || nil != err { + return err + } + + s.SensorConfigService.AddConfig(string(bufByte[:])) + return nil } diff --git a/service/SensorService_test.go b/service/SensorService_test.go new file mode 100644 index 0000000..7e47a57 --- /dev/null +++ b/service/SensorService_test.go @@ -0,0 +1,17 @@ +package service + +import ( + "testing" + "fmt" +) + +func TestSensorService_AddSensor(t *testing.T) { + config := "o8rZu/ncU9dJ+0ryZ9fhy/WfBuSPHBlcdXMMvF8zEcAvRLlS/7b2iUduR9Z+8cGNPR8+fCvPNwgQb2JE2iHeQU6Dno3niWsp7sKC8KYpSPdB3B7QsqQ7Z4Svd4spmDgdxvggwceql1oFAfahQwGGnMxw8bt/phogtiDwo/jhDGGjfXvp9H4nlTjRIkHOC8w3pWWtjbvyb5ELn53YnaiQmzkFSUyz1Cs0IQzDorgvogz2rwmYmOw5kCepFDdnPVgtAsoxrhKrzg92SuyKcrZWGoPjgsl2PJ1ywojwEBgQ96cHIa0mxdC43qTrYbZ5o9+LXdIW9mewvfse8O+/yx8XzOHkZlONC9RtAIcYgoHiJS40ITp0XO7seg==" + + s := &SensorService{} + err := s.AddSensor(config) + + if nil != err { + fmt.Errorf(err.Error()) + } +} \ No newline at end of file