Пример #1
0
// migrate db schema.
func (db *DB) migrate() error {
	log.Debugf("migrate sql schemas..")
	rule := &models.Rule{}
	user := &models.User{}
	proj := &models.Project{}
	return db.db.AutoMigrate(rule, user, proj).Error
}
Пример #2
0
// match a metric with rules, and return matched rules.
// Details:
//	1. If no rules matched, return false.
//	2. If any black patterns matched, return false.
//	3. Else, return true and matched rules.
func (d *Detector) match(m *models.Metric) (bool, []*models.Rule) {
	// Check rules.
	timer := util.NewTimer() // Filter timer
	rules := d.flt.MatchedRules(m)
	elapsed := timer.Elapsed()
	health.AddFilterCost(elapsed)
	if len(rules) == 0 { // Hit no rules.
		return false, rules
	}
	// Check blacklist.
	for _, p := range d.cfg.Detector.BlackList {
		ok, err := filepath.Match(p, m.Name)
		if err != nil {
			// Invalid black pattern.
			log.Errorf("invalid black pattern: %s, %v", p, err)
			continue
		}
		if ok {
			// Hit black pattern.
			log.Debugf("%s hit black pattern %s", m.Name, p)
			return false, rules
		}
	}
	return true, rules // OK
}
Пример #3
0
// startIdleTracking starts a goroutine to track idle metrics by interval.
func (d *Detector) startIdleTracking() {
	go func() {
		log.Debugf("starting idle metrics tracking..")
		interval := time.Duration(d.cfg.Detector.IdleMetricCheckInterval) * time.Second
		ticker := time.NewTicker(interval)
		for _ = range ticker.C {
			d.checkIdles()
		}
	}()
}
Пример #4
0
// initFromDB inits rules from db.
func (f *Filter) initFromDB(db *storage.DB) {
	log.Debugf("init filter's rules from cache..")
	// Listen rules changes.
	db.Admin.RulesCache.OnAdd(f.addRuleCh)
	db.Admin.RulesCache.OnDel(f.delRuleCh)
	// Add rules from cache
	rules := db.Admin.RulesCache.All()
	for _, rule := range rules {
		f.addRule(rule)
	}
}
Пример #5
0
// UpdateWithYamlFile updates the config from a yaml file.
func (c *Config) UpdateWithYamlFile(fileName string) error {
	log.Debugf("read config from %s..", fileName)
	b, err := ioutil.ReadFile(fileName)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(b, c)
	if err != nil {
		return err
	}
	return err
}
Пример #6
0
// MatchedRules returns the matched rules by metric name.
func (f *Filter) MatchedRules(m *models.Metric) (rules []*models.Rule) {
	d := f.trie.Matched(m.Name)
	for _, v := range d {
		n := v.(*node)
		if f.cfg.Detector.EnableIntervalHitLimit {
			hits := atomic.AddUint32(&n.hits, 1)
			if hits > f.cfg.Detector.IntervalHitLimit {
				log.Debugf("%s hits over interval hit limit", n.rule.Pattern)
				return []*models.Rule{}
			}
		}
		rules = append(rules, n.rule)
	}
	return
}
Пример #7
0
// Open a DB by filename.
func Open(fileName string, opts *Options) (*DB, error) {
	_, err := os.Stat(fileName)
	if os.IsNotExist(err) {
		// Create if not exist
		if err := os.Mkdir(fileName, filemode); err != nil {
			return nil, err
		}
		log.Debugf("dir %s created", fileName)
	}
	db := &DB{opts: opts, name: fileName}
	if err = db.init(); err != nil {
		return nil, err
	}
	return db, nil
}
Пример #8
0
// Init cache from db.
func (c *rulesCache) Init(db *gorm.DB) error {
	log.Debugf("init rules from admindb..")
	// Query
	var rules []models.Rule
	err := db.Find(&rules).Error
	if err != nil {
		return err
	}
	// Load
	for i := 0; i < len(rules); i++ {
		rule := &rules[i]
		rule.Share()
		c.rules.Set(rule.ID, rule)
	}
	return nil
}
Пример #9
0
// init opens all storages on DB open.
func (db *DB) init() error {
	infos, err := ioutil.ReadDir(db.name)
	if err != nil {
		return err
	}
	for _, info := range infos {
		fileName := path.Join(db.name, info.Name())
		s, err := openStorage(fileName)
		if err != nil {
			return err
		}
		db.pool = append(db.pool, s)
		log.Debugf("storage %d opened", s.id)
	}
	sort.Sort(byID(db.pool))
	return nil
}
Пример #10
0
// migrate storage schmea.
func (s *storage) migrate() error {
	log.Debugf("migrate storage %d schema", s.id)
	return s.db.AutoMigrate(&EventWrapper{}).Error
}