Beispiel #1
0
//Sets any invalid fields to their default value
func PerformDefault(conf Configuration) {
	err := conf.Validate()
	name := reflect.ValueOf(conf).Type()
	if err != nil {
		if errs, ok := err.(validator.ErrorMap); ok {
			for fieldName := range errs {
				if strings.Contains(fieldName, ".") {
					//If field Name contains a '.' than it is a subfield of a current field
					continue
				}
				glog.V(4).Info("Invalid field searching for default ", fieldName)
				if ok, err := defaults.HasDefault(conf, fieldName); ok {
					value, err := defaults.SetDefault(conf, fieldName)
					if err != nil {
						glog.Errorf("Failed to set default of %s on %s", fieldName, name)
					} else {
						glog.Infof("Defaulted %v.%s to '%v'", name, fieldName, value)
					}
				} else {
					glog.V(4).Info("No default found: ", err)
				}
			}
		} else {
			glog.Errorf("Failed to validate %s: %s", name, err)
		}
	}
}
Beispiel #2
0
// Set the defaults of all fields of obj that have been defined
func SetAllDefaults(obj interface{}) error {

	objValue := reflect.ValueOf(obj)
	if objValue.Kind() != reflect.Ptr {
		return errors.New("Must pass pointer to obj")
	}

	elem := objValue.Elem()
	elemType := elem.Type()

	num := elemType.NumField()
	for i := 0; i < num; i++ {
		field := elemType.Field(i)
		glog.Infof("Defaulting %s", field.Name)
		if def, err := HasDefault(obj, field.Name); def && err == nil {
			_, err := SetDefault(obj, field.Name)
			if err != nil {
				return err
			}
		} else if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #3
0
func (self *Manager) RecordAnomalous(w *Window) {
	glog.Infof("Found anomalous window: %s %s %s", w.Name, w.Tags, w.Start)
	err := self.engine.RecordAnomalous(w.Copy())
	if err != nil {
		glog.Errorf("Error recording anomaly: %s", err)
	}
}
Beispiel #4
0
func (self *App) signalHandler() {
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, syscall.SIGTERM)

	for signal := range signals {
		glog.Infof("Received %s, shuting down...", signal)
		self.shutdownHandler()
	}
}
Beispiel #5
0
func (self *App) Run() (err error) {
	glog.Info("Starting app")
	glog.Info("Setup signal handler")
	go self.signalHandler()

	glog.Info("Setup Engine")
	self.engine, err = self.config.EngineConf.GetEngine()
	if err != nil {
		return
	}
	err = self.engine.Initialize()
	if err != nil {
		return
	}

	glog.Info("Setup Mapper")
	detectorMatchers := make([]*DetectorMatcher, len(self.config.Mappings))
	for i, mapping := range self.config.Mappings {
		namePattern, err := regexp.Compile(mapping.Name)
		if err != nil {
			return fmt.Errorf("Error parsing regexp: %s: %s", mapping.Name, err)
		}
		tagPatterns := make(map[string]*regexp.Regexp, len(mapping.Tags))
		for tag, pattern := range mapping.Tags {
			r, err := regexp.Compile(pattern)
			if err != nil {
				return fmt.Errorf("Error parsing regexp: %s: %s", pattern, err)
			}
			tagPatterns[tag] = r
		}
		fingerprinters := make([]Fingerprinter, len(mapping.Detector.Fingerprinters))
		for i, fp := range mapping.Detector.Fingerprinters {
			f, err := fp.GetFingerprinter()
			if err != nil {
				return fmt.Errorf("Error creating Fingerprinter: %s", err)
			}

			fingerprinters[i] = f
		}
		detectorBuilder := NewDetectorBuilder(
			mapping.Detector.NormalCount,
			mapping.Detector.Consensus,
			mapping.Detector.MinSupport,
			mapping.Detector.ErrorTolerance,
			fingerprinters,
		)
		glog.V(1).Infof("Created detector builder: %v", mapping.Detector)
		matcher := NewDetectorMatcher(namePattern, tagPatterns, detectorBuilder)
		glog.V(1).Infof("Created detector matcher: %v", matcher)
		detectorMatchers[i] = matcher

	}
	detectorMappers := make([]*DetectorMapper, 0)
	self.mapper = NewMapper(detectorMappers, detectorMatchers)
	self.Stats.MapperStats = &self.mapper.Stats

	glog.Info("Setup Manager")
	scheduledDataQueries := make([]*scheduledDataQuery, len(self.config.Schedules))
	for i, sc := range self.config.Schedules {
		builder, err := self.engine.NewQueryBuilder(sc.Query, sc.GroupByInterval)
		if err != nil {
			return fmt.Errorf("Invalid query string: '%s', %s", sc.Query, err)
		}
		q := &scheduledDataQuery{
			sq: NewScheduledQuery(
				builder,
				sc.Delay,
				sc.Period,
			),
			tags: sc.Tags,
		}
		scheduledDataQueries[i] = q
	}

	self.manager = NewManager(self.mapper, self.engine, scheduledDataQueries)

	glog.Info("Starting Manager...")
	self.manager.Start()

	glog.Infof("Starting APIServer on :%d", self.config.APIPort)
	self.apiServer = NewAPIServer(self, self.config.APIPort)
	self.apiServer.Start()

	glog.Infof("Starting Alert Manger...")
	scheduledAlertQueries := make([]*scheduledAlertQuery, len(self.config.Alerts))
	for i, ac := range self.config.Alerts {
		builder, err := self.engine.NewQueryBuilder(ac.Query, ac.GroupByInterval)
		if err != nil {
			return fmt.Errorf("Invalid query string: '%s', %s", ac.Query, err)
		}

		notifiers := make([]Notifier, len(ac.Notifiers))
		for i, nc := range ac.Notifiers {
			n, err := nc.GetNotifier()
			if err != nil {
				return fmt.Errorf("Invalid Notifier for query: '%s' Err: %s", ac.Query, err)
			}
			notifiers[i] = n
		}

		q := &scheduledAlertQuery{
			sq: NewScheduledQuery(
				builder,
				ac.Delay,
				ac.Period,
			),
			threshold: ac.Threshold,
			notifiers: notifiers,
			message:   ac.Message,
		}

		scheduledAlertQueries[i] = q
	}

	self.alertsManager = NewAlertsManager(self.engine, scheduledAlertQueries)
	self.alertsManager.Start()

	// Wait for shutdown
	<-self.shutdownChan

	// Begin shutdown
	self.manager.Stop()
	self.apiServer.Stop()

	glog.Info("App shutdown")
	return
}