Skip to content

Commit

Permalink
fix packages, now on to config
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Jul 16, 2015
1 parent ce17465 commit 63de335
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 48 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package morgoth
import (
"github.com/nathanielc/morgoth/Godeps/_workspace/src/github.com/golang/glog"
"github.com/nathanielc/morgoth/schedule"
"github.com/nathanielc/morgoth/config"
"os"
"os/signal"
"sync"
Expand Down
9 changes: 7 additions & 2 deletions detector/counter.go → counter/counter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package detector
package counter

type Counter interface {
// Count a fingerprint and return the number of times
// that fingerprint has been seen
Count(Fingerprint) int
Count(Countable) int
}


type Countable interface {
IsMatch(other Countable) bool
}
12 changes: 6 additions & 6 deletions detector/lossy_counter.go → counter/lossy_counter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package detector
package counter

import (
"math"
Expand All @@ -14,7 +14,7 @@ type lossyCounter struct {
}

type entry struct {
fingerprint Fingerprint
countable Countable
count int
delta int
}
Expand All @@ -30,15 +30,15 @@ func NewLossyCounter(minSupport, errorTolerance float64) *lossyCounter {
}
}

// Count a fingerprint and return the number of time that fingerprint has
// Count a countable and return the number of time that countable has
// been seen within errorTolerance
func (self *lossyCounter) Count(fingerprint Fingerprint) int {
func (self *lossyCounter) Count(countable Countable) int {

self.total++

count := 0
for _, existing := range self.frequencies {
if existing.fingerprint.IsMatch(fingerprint) {
if existing.countable.IsMatch(countable) {
//Found match, count it
existing.count++
count = existing.count
Expand All @@ -50,7 +50,7 @@ func (self *lossyCounter) Count(fingerprint Fingerprint) int {
// No matches create new entry
count = 1
self.frequencies = append(self.frequencies, &entry{
fingerprint: fingerprint,
countable: countable,
count: count,
delta: self.bucket - 1,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package detector
package counter

import (
"flag"
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions detector/detector.go → detector.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package detector
package morgoth

import (
"github.com/nathanielc/morgoth/Godeps/_workspace/src/github.com/golang/glog"
"github.com/nathanielc/morgoth/window"
"github.com/nathanielc/morgoth/counter"
)

type Detector struct {
Expand All @@ -16,10 +16,10 @@ type Detector struct {
// Pair of fingerprinter and counter
type fingerprinterCounter struct {
fingerprinter Fingerprinter
counter Counter
counter counter.Counter
}

func New(normalCount int, consensus, minSupport, errorTolerance float64) *Detector {
func NewDetector(normalCount int, consensus, minSupport, errorTolerance float64) *Detector {
//TODO perform sanity check on minsupport and normalcount to make sure
// its still possible to mark something as anomalous
return &Detector{
Expand All @@ -41,11 +41,11 @@ func (self *Detector) AddFingerprinter(f Fingerprinter) {
}

// Determine if the window is anomalous
func (self *Detector) IsAnomalous(window *window.Window) bool {
func (self *Detector) IsAnomalous(window *morgoth.Window) bool {

vote := 0.0
for _, fc := range self.counters {
fingerprint := fc.fingerprinter.Fingerprint(window.Data)
fingerprint := fc.fingerprinter.Fingerprint(morgoth.Data)
count := fc.counter.Count(fingerprint)
glog.Infof("Count: %d", count)
if count < self.normalCount {
Expand Down
5 changes: 0 additions & 5 deletions detector/fingerprint.go

This file was deleted.

5 changes: 0 additions & 5 deletions detector/fingerprinter.go

This file was deleted.

2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package morgoth

import (
_ "github.com/nathanielc/morgoth/engines/list"
// _ "github.com/nathanielc/morgoth/engines/list"
"github.com/nathanielc/morgoth/registery"
)

Expand Down
13 changes: 13 additions & 0 deletions fingerprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package morgoth

import (
"github.com/nathanielc/morgoth/counter"
)

type Fingerprinter interface {
Fingerprint(window []float64) Fingerprint
}

type Fingerprint interface {
IsMatch(other counter.Countable) bool
}
12 changes: 5 additions & 7 deletions manager.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package morgoth

import (
"github.com/nathanielc/morgoth/engine"
"github.com/nathanielc/morgoth/schedule"
"github.com/nathanielc/morgoth/window"
"time"
)

Expand All @@ -18,7 +16,7 @@ func (self *Manager) Start() {
}
}

func (self *Manager) ProcessWindows(windows []*window.Window) {
func (self *Manager) ProcessWindows(windows []*Window) {

var detector *detection.Detection
for _, w := range windows {
Expand All @@ -33,14 +31,14 @@ func (self *Manager) ProcessWindows(windows []*window.Window) {
}
}

func (self *Manager) RecordAnomalous(w *window.Window) {
func (self *Manager) RecordAnomalous(w *Window) {
//TODO
}

type ScheduledQuery struct {
schedule *schedule.Schedule
query engine.Query
engine engine.Engine
query Query
engine Engine
manager *Manager
}

Expand All @@ -50,6 +48,6 @@ func (self *ScheduledQuery) Start() {

func (self *ScheduledQuery) callback(rot schedule.Rotation, start, stop time.Time) {
q := query.QueryForTimeRange(start, stop)
windows := engine.ExecuteQuery(q)
windows := ExecuteQuery(q)
self.manager.ProcessWindows(windows)
}
26 changes: 12 additions & 14 deletions mapper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package morgoth

import (
"github.com/nathanielc/morgoth/detection"
"github.com/nathanielc/morgoth/window"
"hash/fnv"
"regexp"
)
Expand All @@ -12,7 +10,7 @@ type Mapper struct {
detectorConfMaps *DetectorConfMap
}

func (self *Mapper) Map(w *window.Window) *detection.Detection {
func (self *Mapper) Map(w *Window) *Detector {

// First check for match in detector maps
hash := calcHash()
Expand Down Expand Up @@ -56,19 +54,19 @@ func calcHash(name string, tags map[string]string) uint64 {
type DetectorMap struct {
Name string
Tags map[string]string
detector *detection.Detection
detector *Detector
}

func NewDetectorMap(w *window.Window, detector *detection.Detection) *DetectorMap {
func NewDetectorMap(w *Window, detector *Detector) *DetectorMap {
return &DetectorMap{
Name: w.Name,
Tags: w.Tags,
detector: detector,
}
}

func (self *DetectorMap) IsMatch(w *window.window) bool {
if self.Name != window.Name {
func (self *DetectorMap) IsMatch(w *window) bool {
if self.Name != w.Name {
return false
}

Expand All @@ -87,7 +85,7 @@ func (self *DetectorMap) IsMatch(w *window.window) bool {
return true
}

func (self *DetectorMap) GetDetector() *detection.Detection {
func (self *DetectorMap) GetDetector() *Detector {
if self.detector != nil {
return self.detector
}
Expand All @@ -96,26 +94,26 @@ func (self *DetectorMap) GetDetector() *detection.Detection {
}

type DetectorConfMap struct {
NamePattern *regex.Regexp
NamePattern *regexp.Regexp
TagPatterns map[string]*regexp.Regexp
detectorConstructor func() *detection.Detection
detectorConstructor func() *Detector
}

func (self *DetectorConfMap) IsMatch(w *window.window) bool {
if !self.NamePattern.MatchString(window.Name) {
func (self *DetectorConfMap) IsMatch(w *Window) bool {
if !self.NamePattern.MatchString(w.Name) {
return false
}

//Check only defined tags match patterns
for k, pattern := range self.TagPatterns {
if tag, ok := window.Tags[k]; !ok || !pattern.MatchString(tag) {
if tag, ok := w.Tags[k]; !ok || !pattern.MatchString(tag) {
return false
}
}

return true
}

func (self *DetectorConfMap) NewDetectorMap(w *window.Window) *DetectorMap {
func (self *DetectorConfMap) NewDetectorMap(w *Window) *DetectorMap {
return NewDetectorMap(w, self.detectorConstructor())
}
File renamed without changes.
2 changes: 1 addition & 1 deletion detector/detector_test.go → old/detector_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package detector
package morgoth_test

import (
"github.com/nathanielc/morgoth/Godeps/_workspace/src/github.com/stretchr/testify/assert"
Expand Down
File renamed without changes.

0 comments on commit 63de335

Please sign in to comment.