Ejemplo n.º 1
0
// Detect input metric with its matched rules via 3-sigma.
//
//	1. Get history values for this metric.
//	2. Get current index for this metric.
//	3. Calculate score via 3-sigma.
//	4. Get score trending via ewma.
//	5. Save the metric and index to db.
//	6. Test with its matched rules and output it.
//
func (d *Detector) detect(m *models.Metric, rules []*models.Rule) (*models.Event, error) {
	// Get index.
	idx, err := d.db.Index.Get(m.Name)
	if err != nil {
		if err == indexdb.ErrNotFound {
			idx = nil
		} else {
			return nil, err // unexcepted
		}
	}
	// Fill zero?
	fz := idx != nil && d.shouldFz(m)
	// History values.
	vals, err := d.values(m, fz)
	if err != nil {
		return nil, err // unexcepted
	}
	// Apply 3-sigma.
	d.div3Sigma(m, vals)
	// New index.
	idx = d.nextIdx(idx, m)
	// Test with rules.
	d.test(m, idx, rules)
	// Save
	err = d.save(m, idx)
	if len(m.TestedRules) > 0 {
		// Test ok.
		ev := models.NewEvent(m, idx)
		return ev, err
	}
	return nil, err
}
Ejemplo n.º 2
0
// detect input metric with its matched rules.
// Details:
//	1. Get its index from db.
//	2. If the metric need to be analyzed, analyze it.
//	3. Save its index and the metricinto db.
//	4. Test the metric with matched rules.
//	5. Make events with its tested rules.
func (d *Detector) detect(m *models.Metric, rules []*models.Rule) (evs []*models.Event, err error) {
	var idx *models.Index
	if idx, err = d.db.Index.Get(m.Name); err != nil {
		if err == indexdb.ErrNotFound {
			idx = nil
		} else {
			return nil, err
		}
	}
	idx = d.analyze(idx, m, rules)
	if err = d.save(m, idx); err != nil {
		return
	}
	for _, rule := range d.test(m, idx, rules) {
		evs = append(evs, models.NewEvent(m, idx, rule))
	}
	return
}