collector

This commit is contained in:
insanity@loafle.com 2017-04-17 13:13:23 +09:00
parent 142f267ab9
commit 6b0730f636
3 changed files with 61 additions and 56 deletions

View File

@ -12,20 +12,22 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
//crm "loafle.com/overflow/crawler_manager_go"
) )
const CONFIG_ROOT = "/config/container/" const CONFIG_ROOT = "/config/container"
type Collector struct { type Collector struct {
scheduler s.Scheduler scheduler s.Scheduler
configs []*conf.Config //configs []*conf.Config
configs map[string]*conf.Config
} }
func (c *Collector) Start() { func (c *Collector) Start() {
go func() { go func() {
c.configs = make([]*conf.Config, 0) c.configs = make(map[string]*conf.Config, 0)
if err := c.readAllConfig(CONFIG_ROOT); err != nil { if err := c.readAllConfig(); err != nil {
log.Println(err) log.Println(err)
} }
@ -40,8 +42,34 @@ func (c *Collector) Start() {
}() }()
} }
func (c *Collector) AddSensor(container, crawler, id string) { func (c *Collector) collect(id string) {
config := c.readConfig(CONFIG_ROOT + container + "/" + crawler + "/" + id + ".conf")
conf := c.configs[id]
log.Printf("COLLECT %s - [ID: %s] [Crawler : %s]", time.Now(), conf.Id, conf.Crawler.Name)
//conn, err := crm.GetClient(conf.Crawler.Container)
//if err != nil {
// log.Println(err)
//}
//defer conn.Close()
//dc := g.NewDataClient(conn);
//in := &g.Input{}
//
//in.Id = id
//in.Name = g.Crawlers_HEALTH_MONGODB
//
//out, err := dc.Get(context.Background(), in)
//
//if err != nil {
// log.Println(err)
//}
//log.Println(out)
}
func (c *Collector) AddSensor(path string) {
config := c.readConfig(CONFIG_ROOT + path)
if config != nil { if config != nil {
if err := c.addSensor(config); err != nil { if err := c.addSensor(config); err != nil {
log.Println(err) log.Println(err)
@ -55,12 +83,22 @@ func (c *Collector) RemoveSensor(id string) {
} }
} }
func (c *Collector) UpdateSensor(id, interval string) { func (c *Collector) UpdateSensor(path string) {
if !c.checkExist(id) { newConf := c.readConfig(CONFIG_ROOT + path)
log.Println("Cannot update Sensor : ID not exist [" + id + "]") if newConf != nil {
if !c.checkExist(newConf.Id) {
log.Println("Cannot update Sensor : ID not exist [" + newConf.Id + "]")
return return
} }
c.scheduler.UpdateSchedule(id, interval)
exConf := c.configs[newConf.Id]
c.configs[newConf.Id] = newConf
if exConf.Schedule.Interval != newConf.Schedule.Interval {
log.Println("???????????????")
c.scheduler.UpdateSchedule(newConf.Id, newConf.Schedule.Interval)
}
}
} }
func (c *Collector) Stop() { func (c *Collector) Stop() {
@ -68,17 +106,13 @@ func (c *Collector) Stop() {
c.scheduler.Stop() c.scheduler.Stop()
} }
func (c *Collector) collect(id string) {
log.Printf("COLLECT %s - [ID: %s]", time.Now(), id)
}
func (c *Collector) addSensor(conf *conf.Config) error { func (c *Collector) addSensor(conf *conf.Config) error {
return c.scheduler.NewSchedule(conf.Id, conf.Schedule.Interval, c.collect) return c.scheduler.NewSchedule(conf.Id, conf.Schedule.Interval, c.collect)
} }
func (c *Collector) readAllConfig(rootPath string) error { func (c *Collector) readAllConfig() error {
err := filepath.Walk(rootPath, func(path string, f os.FileInfo, err error) error { err := filepath.Walk(CONFIG_ROOT, func(path string, f os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -103,6 +137,7 @@ func (c *Collector) readConfig(path string) *conf.Config {
bytes, err := ioutil.ReadFile(path) bytes, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return nil
} }
conf := conf.Config{} conf := conf.Config{}
json.Unmarshal(bytes, &conf) json.Unmarshal(bytes, &conf)
@ -111,7 +146,7 @@ func (c *Collector) readConfig(path string) *conf.Config {
fmt.Println(err) fmt.Println(err)
return nil return nil
} }
c.configs = append(c.configs, &conf) c.configs[conf.Id] = &conf
return &conf return &conf
} }
@ -125,38 +160,8 @@ func (c *Collector) validateConfig(conf *conf.Config, configPath string) error {
} }
func (c *Collector) checkExist(id string) bool { func (c *Collector) checkExist(id string) bool {
for _, c := range c.configs { if _, exists := c.configs[id]; exists {
if c.Id == id {
return true return true
} }
}
return false return false
} }
// connection
//func CallGet() {
//
// conn, err := grpc.Dial(address, grpc.WithInsecure())
// if err != nil {
// log.Fatalf("did not connect: %v", err)
// }
// defer conn.Close()
//
//
// dc := g.NewDataClient(conn);
//
// in := &g.Input{}
//
// in.Id = ""
// in.Name = g.Crawlers_HEALTH_DNS
//
//
//
// out, err := dc.Get(context.Background(), in) ////
//
// if err != nil {
// log.Println(err)
// }
// log.Println(out)
//
//}

View File

@ -9,13 +9,13 @@ import (
func TestCallGet(t *testing.T) { func TestCallGet(t *testing.T) {
c := Collector{} c := Collector{}
c.Start() c.Start()
time.Sleep(time.Second * 30) time.Sleep(time.Second * 10)
//log.Println("add sensor") log.Println("add sensor")
//c.AddSensor("network", "smb", "ttt") c.AddSensor("/network/smb/t2.conf")
//time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
log.Println("update sonsor") log.Println("update sonsor")
c.UpdateSensor("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", "3") c.UpdateSensor("/network/smb/t1.conf")
time.Sleep(time.Second * 20) time.Sleep(time.Second * 20)
} }

View File

@ -71,7 +71,7 @@ func (t *Task) addNextAt() {
if t.period == 0 { if t.period == 0 {
t.period = time.Duration(t.intervalSec) t.period = time.Duration(t.intervalSec)
t.nextAt = t.lastAt.Add(1 * time.Second) t.nextAt = t.lastAt.Add(3 * time.Second)
} else { } else {
t.nextAt = t.lastAt.Add(t.period * time.Second) t.nextAt = t.lastAt.Add(t.period * time.Second)
} }