sql problem has solved

This commit is contained in:
insanity@loafle.com 2017-05-18 13:45:05 +09:00
parent ca5d24c9a0
commit 36751de2f7
7 changed files with 82 additions and 107 deletions

View File

@ -9,13 +9,12 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
) )
type configManager struct { type configManager struct {
config_manager.ConfigManager // interface implements config_manager.ConfigManager // interface implements
configs map[string] *config_manager.Config configs map[string]*config_manager.Config
globalConfig config_manager.GlobalConfig globalConfig config_manager.GlobalConfig
} }
var _configManager *configManager var _configManager *configManager
@ -33,7 +32,6 @@ func GetInstance() *configManager {
func (c *configManager) stop() {} func (c *configManager) stop() {}
func (c *configManager) getConfigPath() string { func (c *configManager) getConfigPath() string {
return c.globalConfig.Paths.RootFolder + c.globalConfig.Paths.ConfigFolder return c.globalConfig.Paths.RootFolder + c.globalConfig.Paths.ConfigFolder
} }
@ -46,8 +44,6 @@ func (c *configManager) getConfigFilePath(config *config_manager.Config) string
return c.getContainerPath() + appendSeperator(config.Crawler.Container) + appendSeperator(config.Crawler.Name) + config.Id return c.getContainerPath() + appendSeperator(config.Crawler.Container) + appendSeperator(config.Crawler.Name) + config.Id
} }
func appendSeperator(str string) string { func appendSeperator(str string) string {
if strings.LastIndex(str, "/") != len(str)-1 { if strings.LastIndex(str, "/") != len(str)-1 {
return str + "/" return str + "/"
@ -92,7 +88,7 @@ func (c *configManager) loadCrawlerConfig(root string, dir string) {
} }
for _, file := range files { for _, file := range files {
// 디렉터리라면 재귀 / 파일이라면 설정 로드 // 디렉터리라면 재귀 / 파일이라면 설정 로드
if file.IsDir() == true { if file.IsDir() == true {
c.loadCrawlerConfig(currentDir, file.Name()) c.loadCrawlerConfig(currentDir, file.Name())
@ -112,75 +108,73 @@ func (c *configManager) loadCrawlerConfig(root string, dir string) {
func (c *configManager) addConfig(tmp string) { func (c *configManager) addConfig(tmp string) {
//for data := range c.addCh { //for data := range c.addCh {
path := tmp path := tmp
b, err := ioutil.ReadFile(path) b, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
// error process // error process
log.Panic(err) log.Panic(err)
} }
// 임시파일을 로드 , Config로 변환 // 임시파일을 로드 , Config로 변환
var m = config_manager.Config{} var m = config_manager.Config{}
err = json.Unmarshal(b, &m) err = json.Unmarshal(b, &m)
if err != nil { if err != nil {
// error process // error process
log.Panic(err) log.Panic(err)
} }
// agent 폴더 / config / crawler / .. / .. / .. / 에 해당하는 파일이 있는지 확인, 있다면 삭제 // agent 폴더 / config / crawler / .. / .. / .. / 에 해당하는 파일이 있는지 확인, 있다면 삭제
// Config 파일로 저장 // Config 파일로 저장
savePath := c.getConfigFilePath(&m) savePath := c.getConfigFilePath(&m)
ioutil.WriteFile(savePath, b, 0644) ioutil.WriteFile(savePath, b, 0644)
// tempfile remove // tempfile remove
err = os.Remove(path) err = os.Remove(path)
if err != nil { if err != nil {
// error process // error process
log.Panic(err) log.Panic(err)
} }
// Config 맵에 저장 // Config 맵에 저장
c.configs[m.Id] = &m c.configs[m.Id] = &m
// notify id // notify id
//err = observer.Notify(messages.SCF_SENSOR_ADD_DONE, m.Id) //err = observer.Notify(messages.SCF_SENSOR_ADD_DONE, m.Id)
//if err != nil { //if err != nil {
// continue // continue
//} //}
//} //}
} }
func (c *configManager) removeConfig(id string) { func (c *configManager) removeConfig(id string) {
//for data := range c.removeCh { //for data := range c.removeCh {
removeid := id removeid := id
// check exists // check exists
config, ok := c.configs[removeid] config, ok := c.configs[removeid]
if !ok { if !ok {
return; return
} }
// 해당 파일 삭제 // 해당 파일 삭제
path := c.getConfigFilePath(config) path := c.getConfigFilePath(config)
err := os.Remove(path) err := os.Remove(path)
if err != nil { if err != nil {
// error check // error check
log.Panic(err) log.Panic(err)
} }
// 해당 id 삭제 // 해당 id 삭제
delete(c.configs, removeid) delete(c.configs, removeid)
// notify id // notify id
//err = observer.Notify("TASK_DONE", removeid) //err = observer.Notify("TASK_DONE", removeid)
//if err != nil { //if err != nil {
// continue // continue
//} //}
//} //}
} }
// implements methods // implements methods
func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig { func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig {
return &c.globalConfig return &c.globalConfig
@ -188,6 +182,6 @@ func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig {
func (c *configManager) GetSensorById(id string) *config_manager.Config { func (c *configManager) GetSensorById(id string) *config_manager.Config {
return c.configs[id] return c.configs[id]
} }
func (c *configManager) GetSensors() map[string] *config_manager.Config { func (c *configManager) GetSensors() map[string]*config_manager.Config {
return c.configs return c.configs
} }

View File

@ -3,18 +3,18 @@ package config_manager_go
import ( import (
"testing" "testing"
//"github.com/stretchr/testify/assert" //"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"loafle.com/overflow/agent_api/config_manager"
"encoding/json" "encoding/json"
"github.com/google/uuid" "github.com/google/uuid"
"loafle.com/overflow/agent_api/observer" "io/ioutil"
"loafle.com/overflow/agent_api/config_manager"
"loafle.com/overflow/agent_api/messages" "loafle.com/overflow/agent_api/messages"
"loafle.com/overflow/agent_api/observer"
"log"
"time" "time"
) )
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
// notify temp channel // notify temp channel
//ch := make(chan interface{},0) //ch := make(chan interface{},0)
//observer.Add(messages.CFG_LOADED,ch) //observer.Add(messages.CFG_LOADED,ch)
@ -35,10 +35,9 @@ func TestLoadConfig(t *testing.T) {
//time.Sleep(1 * time.Second) //time.Sleep(1 * time.Second)
} }
func TestAddConfig(t *testing.T) { func TestAddConfig(t *testing.T) {
_configManager.loadGlobalConfig("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml") _configManager.loadGlobalConfig("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml")
// 원본 테스트 설정 파일 로드 // 원본 테스트 설정 파일 로드
b, err := ioutil.ReadFile("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/test.json") b, err := ioutil.ReadFile("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/test.json")
if err != nil { if err != nil {
@ -46,24 +45,23 @@ func TestAddConfig(t *testing.T) {
} }
var m = config_manager.Config{} var m = config_manager.Config{}
json.Unmarshal(b, &m) json.Unmarshal(b, &m)
// uuid로 원본 파일 복제 // uuid로 원본 파일 복제
rid,_ := uuid.NewRandom() rid, _ := uuid.NewRandom()
m.Id = rid.String() m.Id = rid.String()
b,err = json.Marshal(&m) b, err = json.Marshal(&m)
ioutil.WriteFile("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/" + m.Id,b,0644) ioutil.WriteFile("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/"+m.Id, b, 0644)
// add test // add test
observer.Notify(messages.TASK_SENSOR_ADD,"/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/" + m.Id) observer.Notify(messages.TASK_SENSOR_ADD, "/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/"+m.Id)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
func TestRemoveConfig(t *testing.T) { func TestRemoveConfig(t *testing.T) {
GetInstance() GetInstance()
_configManager.loadGlobalConfig("/home/snoop/develop/path/go/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml") _configManager.loadGlobalConfig("/home/snoop/develop/path/go/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml")
_configManager.loadCrawlerConfigAll() _configManager.loadCrawlerConfigAll()
// remove test // remove test
//observer.Notify(messages.TASK_SENSOR_REMOVE,"d0fcc7b1-43a7-4acd-a7bf-c9572a9d4c9e") //observer.Notify(messages.TASK_SENSOR_REMOVE,"d0fcc7b1-43a7-4acd-a7bf-c9572a9d4c9e")
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)

View File

@ -2,7 +2,6 @@ package config_manager_go
import ( import (
"loafle.com/overflow/agent_api/config_manager" "loafle.com/overflow/agent_api/config_manager"
) )
//import ( //import (
@ -11,10 +10,6 @@ import (
//) //)
// //
func Start(ch chan *config_manager.GlobalConfig, path string) error { func Start(ch chan *config_manager.GlobalConfig, path string) error {
_configManager = GetInstance() _configManager = GetInstance()
_configManager.loadGlobalConfig(path + "/" + "global.yaml") _configManager.loadGlobalConfig(path + "/" + "global.yaml")
@ -24,7 +19,7 @@ func Start(ch chan *config_manager.GlobalConfig, path string) error {
func StartSensorConfig(ch chan config_manager.ConfigManager, authKey string) error { func StartSensorConfig(ch chan config_manager.ConfigManager, authKey string) error {
if err := _configManager.loadCrawlerConfigAll(); err != nil { if err := _configManager.loadCrawlerConfigAll(); err != nil {
return err return err
} }
ch <- _configManager ch <- _configManager
return nil return nil
@ -35,7 +30,6 @@ func Stop(ch chan bool) {
ch <- true ch <- true
} }
func AddSensor(tmpFilePath string) { func AddSensor(tmpFilePath string) {
GetInstance().addConfig(tmpFilePath) GetInstance().addConfig(tmpFilePath)
@ -67,12 +61,10 @@ func RemoveSensor(id string) {
GetInstance().removeConfig(id) GetInstance().removeConfig(id)
} }
func UpdateSensor(tmpFile string) { func UpdateSensor(tmpFile string) {
//GetInstance(). //GetInstance().
//overwrite file //overwrite file
@ -82,14 +74,6 @@ func UpdateSensor(tmpFile string) {
} }
//func agentStartHandler() { //func agentStartHandler() {
// agentStart := make(chan interface{}, 0) // agentStart := make(chan interface{}, 0)
// observer.Add(messages.AGT_STARTING, agentStart) // observer.Add(messages.AGT_STARTING, agentStart)
@ -129,5 +113,3 @@ func UpdateSensor(tmpFile string) {
// observer.Add(messages.TASK_SENSOR_REMOVE,_configManager.removeCh) // observer.Add(messages.TASK_SENSOR_REMOVE,_configManager.removeCh)
// go _configManager.removeConfig() // go _configManager.removeConfig()
//} //}

View File

@ -24,13 +24,13 @@
{ {
"keys" : [ "keys" : [
{ {
"metric":"net.connection_count", "metric":"net.mysql.connection_count",
"key":"Connections" "key":"Connections"
} }
], ],
"queryInfo" : { "queryInfo" : {
"query":"show status where `variable_name` = 'Connections'" "query":"show status where variable_name = 'Connections'"
}, },
"mappingInfo" : { "mappingInfo" : {
"parseDirection" : "row", "parseDirection" : "row",

View File

@ -14,7 +14,7 @@
} }
}, },
"schedule" : { "schedule" : {
"interval" : "5" "interval" : "10"
}, },
"crawler" : { "crawler" : {
"name":"SQL", "name":"SQL",
@ -24,8 +24,8 @@
{ {
"keys" : [ "keys" : [
{ {
"metric" :"net.connection_count", "metric" :"net.sqlserver.connection_count",
"key" : "Connections" "key" : "connection_count"
} }
], ],
"queryInfo" : { "queryInfo" : {

View File

@ -2,19 +2,19 @@
"id" : "989238744", "id" : "989238744",
"target" : { "target" : {
"connection" : { "connection" : {
"ip" : "192.168.1.107", "ip" : "192.168.1.106",
"port" : "5432", "port" : "5432",
"ssl" : false, "ssl" : false,
"portType" : "tcp" "portType" : "tcp"
}, },
"auth" : { "auth" : {
"url":"jdbc:postgresql://192.168.1.107:5432/postgres", "url":"jdbc:postgresql://192.168.1.106:5432/postgres",
"id":"postgres", "id":"vertx",
"pw":"!@#$qwer1234" "pw":"qwe123"
} }
}, },
"schedule" : { "schedule" : {
"interval" : "10" "interval" : "3"
}, },
"crawler" : { "crawler" : {
"name":"SQL", "name":"SQL",
@ -24,13 +24,14 @@
{ {
"keys" : [ "keys" : [
{ {
"metric":"net.connection_count", "metric":"net.pgsql.connection_count",
"key" : "connection_count" "key" : "connection_count"
} }
], ],
"queryInfo":{ "queryInfo":{
"query" : "select count(pid) as connection_count from pg_catalog.pg_stat_activity where state <> 'idle'" "query" : "select count(pid) as connection_count from pg_catalog.pg_stat_activity where state <> 'idle';"
} },
"mappingInfo" : {}
} }
] ]
} }