Skip to content

Commit

Permalink
Merge pull request #20 from dpquigl/bpevents
Browse files Browse the repository at this point in the history
Issue #19: Introduce UNWARN event/add event sending to best practices
  • Loading branch information
Alan-R committed Dec 30, 2015
2 parents e3d0e1e + e09101c commit ee44042
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cma/assimevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ class AssimEvent(object):
OBJUP = 1 # Object status is now up
OBJDOWN = 2 # Object status is now down
OBJWARN = 3 # Object status is now in a warning state
OBJUPDATE = 4 # Object was updated
OBJDELETE = 5 # Object is about to be deleted
OBJUNWARN = 4 # Object status has exited a warning state
OBJUPDATE = 5 # Object was updated
OBJDELETE = 6 # Object is about to be deleted

eventtypenames = {
CREATEOBJ: 'create',
OBJUP: 'up',
OBJDOWN: 'down',
OBJWARN: 'warn',
OBJUNWARN: 'unwarn',
OBJUPDATE: 'update',
OBJDELETE: 'delete'
}
Expand Down
58 changes: 57 additions & 1 deletion cma/bestpractices.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
from graphnodeexpression import GraphNodeExpression, ExpressionContext
from AssimCclasses import pyConfigContext
from store import Store
from assimevent import AssimEvent
from assimeventobserver import AssimEventObserver
import os, logging, sys

class BestPractices(DiscoveryListener):
Expand Down Expand Up @@ -223,14 +225,33 @@ def processpkt(self, drone, srcaddr, jsonobj):
#print >> sys.stderr, 'RESULTS ARE:', statuses
self.log_rule_results(statuses, drone, srcaddr, discovertype, rulesobj)

@staticmethod
def send_rule_event(oldstat, newstat, drone, ruleid, _ruleobj):
''' Newstat can never be None. '''
if oldstat is None:
if newstat == 'fail':
AssimEvent(drone, AssimEvent.OBJWARN, extrainfo = {'ruleid' : ruleid})
elif oldstat == 'pass':
if newstat == 'fail':
AssimEvent(drone, AssimEvent.OBJWARN, extrainfo = {'ruleid' : ruleid})
elif oldstat == 'fail':
if newstat == 'pass' or newstat == 'ignore' or newstat == 'NA':
AssimEvent(drone, AssimEvent.OBJUNWARN, extrainfo = {'ruleid' : ruleid})
elif oldstat == 'ignore':
if newstat == 'fail':
AssimEvent(drone, AssimEvent.OBJWARN, extrainfo = {'ruleid' : ruleid})
elif oldstat == 'NA':
if newstat == 'fail':
AssimEvent(drone, AssimEvent.OBJWARN, extrainfo = {'ruleid' : ruleid})

def log_rule_results(self, results, drone, _srcaddr, discovertype, rulesobj):
'''Log the results of this set of rule evaluations'''
status_name = 'BP_%s_rulestatus' % discovertype
if hasattr(drone, status_name):
oldstats = pyConfigContext(getattr(drone, status_name))
else:
oldstats = {'pass': [], 'fail': [], 'ignore': [], 'NA': []}
for stat in ('pass', 'fail', 'ignore'):
for stat in ('pass', 'fail', 'ignore', 'NA'):
logmethod = self.log.info if stat == 'pass' else self.log.warning
for ruleid in results[stat]:
oldstat = None
Expand All @@ -241,6 +262,7 @@ def log_rule_results(self, results, drone, _srcaddr, discovertype, rulesobj):
if oldstat == stat or stat == 'NA':
# No change
continue
BestPractices.send_rule_event(oldstat, stat, drone, ruleid, rulesobj)
thisrule = rulesobj[ruleid]
rulecategory = thisrule['category']
logmethod('%s %sED %s rule %s: %s [%s]' % (drone,
Expand Down Expand Up @@ -333,6 +355,32 @@ def configcallback(config, changedname, _unusedargs):
for pkttype in config['allbpdiscoverytypes']:
BestPractices.register_sensitivity(BestPracticesCMA, pkttype)





class DebugEventObserver(AssimEventObserver):
'''
Event observer for testing the send event code
'''
expectResults = {
'f2p' : AssimEvent.OBJUNWARN,
'n2f' : AssimEvent.OBJWARN,
'p2f' : AssimEvent.OBJWARN,
'i2f' : AssimEvent.OBJWARN,
'f2i' : AssimEvent.OBJUNWARN,
'f2na' : AssimEvent.OBJUNWARN,
'na2f' : AssimEvent.OBJWARN
}
def __init__(self):
AssimEventObserver.__init__(self,None)
def notifynewevent(self,event):
if event.eventtype == DebugEventObserver.expectResults[event.extrainfo['ruleid']]:
print "Success Result for %s is correct" % event.extrainfo['ruleid']
else:
print "Failure Result for %s is incorrect" % event.extrainfo['ruleid']
sys.exit(1)

if __name__ == '__main__':
#import sys
JSON_data = '''
Expand Down Expand Up @@ -385,4 +433,12 @@ def configcallback(config, changedname, _unusedargs):
assert len(ourstats['pass']) >= 3
assert len(ourstats['ignore']) == 0
print ourstats
DebugEventObserver()
BestPractices.send_rule_event('fail', 'pass', 'testdrone', 'f2p', None)
BestPractices.send_rule_event(None, 'fail', 'testdrone', 'n2f', None)
BestPractices.send_rule_event('pass', 'fail', 'testdrone', 'p2f', None)
BestPractices.send_rule_event('ignore', 'fail', 'testdrone', 'i2f', None)
BestPractices.send_rule_event('fail', 'ignore', 'testdrone', 'f2i', None)
BestPractices.send_rule_event('fail', 'NA', 'testdrone', 'f2na', None)
BestPractices.send_rule_event('NA', 'fail', 'testdrone', 'na2f', None)
print 'Results look correct!'

0 comments on commit ee44042

Please sign in to comment.