collector
This commit is contained in:
parent
142f267ab9
commit
6b0730f636
105
collector.go
105
collector.go
|
@ -12,20 +12,22 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
//crm "loafle.com/overflow/crawler_manager_go"
|
||||
)
|
||||
|
||||
const CONFIG_ROOT = "/config/container/"
|
||||
const CONFIG_ROOT = "/config/container"
|
||||
|
||||
type Collector struct {
|
||||
scheduler s.Scheduler
|
||||
configs []*conf.Config
|
||||
//configs []*conf.Config
|
||||
configs map[string]*conf.Config
|
||||
}
|
||||
|
||||
func (c *Collector) Start() {
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -40,8 +42,34 @@ func (c *Collector) Start() {
|
|||
}()
|
||||
}
|
||||
|
||||
func (c *Collector) AddSensor(container, crawler, id string) {
|
||||
config := c.readConfig(CONFIG_ROOT + container + "/" + crawler + "/" + id + ".conf")
|
||||
func (c *Collector) collect(id string) {
|
||||
|
||||
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 err := c.addSensor(config); err != nil {
|
||||
log.Println(err)
|
||||
|
@ -55,12 +83,22 @@ func (c *Collector) RemoveSensor(id string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Collector) UpdateSensor(id, interval string) {
|
||||
if !c.checkExist(id) {
|
||||
log.Println("Cannot update Sensor : ID not exist [" + id + "]")
|
||||
return
|
||||
func (c *Collector) UpdateSensor(path string) {
|
||||
newConf := c.readConfig(CONFIG_ROOT + path)
|
||||
if newConf != nil {
|
||||
if !c.checkExist(newConf.Id) {
|
||||
log.Println("Cannot update Sensor : ID not exist [" + newConf.Id + "]")
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
c.scheduler.UpdateSchedule(id, interval)
|
||||
}
|
||||
|
||||
func (c *Collector) Stop() {
|
||||
|
@ -68,17 +106,13 @@ func (c *Collector) 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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -103,6 +137,7 @@ func (c *Collector) readConfig(path string) *conf.Config {
|
|||
bytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
conf := conf.Config{}
|
||||
json.Unmarshal(bytes, &conf)
|
||||
|
@ -111,7 +146,7 @@ func (c *Collector) readConfig(path string) *conf.Config {
|
|||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
c.configs = append(c.configs, &conf)
|
||||
c.configs[conf.Id] = &conf
|
||||
return &conf
|
||||
}
|
||||
|
||||
|
@ -125,38 +160,8 @@ func (c *Collector) validateConfig(conf *conf.Config, configPath string) error {
|
|||
}
|
||||
|
||||
func (c *Collector) checkExist(id string) bool {
|
||||
for _, c := range c.configs {
|
||||
if c.Id == id {
|
||||
return true
|
||||
}
|
||||
if _, exists := c.configs[id]; exists {
|
||||
return true
|
||||
}
|
||||
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)
|
||||
//
|
||||
//}
|
||||
|
|
|
@ -9,13 +9,13 @@ import (
|
|||
func TestCallGet(t *testing.T) {
|
||||
c := Collector{}
|
||||
c.Start()
|
||||
time.Sleep(time.Second * 30)
|
||||
time.Sleep(time.Second * 10)
|
||||
|
||||
//log.Println("add sensor")
|
||||
//c.AddSensor("network", "smb", "ttt")
|
||||
//time.Sleep(time.Second * 3)
|
||||
log.Println("add sensor")
|
||||
c.AddSensor("/network/smb/t2.conf")
|
||||
time.Sleep(time.Second * 3)
|
||||
|
||||
log.Println("update sonsor")
|
||||
c.UpdateSensor("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", "3")
|
||||
c.UpdateSensor("/network/smb/t1.conf")
|
||||
time.Sleep(time.Second * 20)
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ func (t *Task) addNextAt() {
|
|||
|
||||
if t.period == 0 {
|
||||
t.period = time.Duration(t.intervalSec)
|
||||
t.nextAt = t.lastAt.Add(1 * time.Second)
|
||||
t.nextAt = t.lastAt.Add(3 * time.Second)
|
||||
} else {
|
||||
t.nextAt = t.lastAt.Add(t.period * time.Second)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user