def get_data(self, **kwargs): # profile -> (syslog, snmp, other) r = dict([(p, [0, 0, 0]) for p in profile_loader.iter_profiles()]) for rule in EventClassificationRule.objects.all(): profile = None source = None for p in rule.patterns: if p.key_re in ("^profile$", "profile"): profile = p.value_re elif p.key_re in ("^source$", "source"): source = p.value_re if profile and source: break for p in r: if not profile or re.search(profile, p): d = r[p] if source in ("syslog", "^syslog$"): d[0] += 1 elif source in ("SNMP Trap", "^SNMP Trap$"): d[1] += 1 else: d[2] += 1 # Build data data = [(p, v[0], v[1], v[2], v[0] + v[1] + v[2]) for p, v in six.iteritems(r)] data = sorted(data, key=lambda x: -x[4]) return self.from_dataset( title=self.title, columns=[ "Profile", TableColumn("Syslog", align="right", format="integer", total="sum"), TableColumn("SNMP Traps", align="right", format="integer", total="sum"), TableColumn("Other", align="right", format="integer", total="sum"), TableColumn("Total", align="right", format="integer", total="sum"), ], data=data, )
def test_iter_profiles(): assert len(list(loader.iter_profiles())) > 0
def get_profiles(): if os.environ.get("NOC_TEST_PROFILE"): p_name = os.environ["NOC_TEST_PROFILE"] return [x for x in loader.iter_profiles() if x == p_name] else: return list(loader.iter_profiles())
def build_profile(self): """ Profile names :return: (profile name, profile name) """ return [{"id": n, "label": n} for n in profile_loader.iter_profiles()]
def load(self): """ Load rules from database """ self.lookup_cls = get_handler(config.classifier.lookup_handler) self.rules = {} logger.info("Loading rules") n = 0 cn = 0 profiles = list(profile_loader.iter_profiles()) rules = defaultdict(list) # Load cloning rules cloning_rules = [] for cr in CloneClassificationRule.objects.all(): try: cloning_rules += [CloningRule(cr)] except InvalidPatternException as why: logger.error("Failed to load cloning rule '%s': Invalid pattern: %s", cr.name, why) continue logger.info("%d cloning rules found", len(cloning_rules)) # profiles re cache rx_profiles = {} # Initialize rules for r in EventClassificationRule.objects.order_by("preference"): try: rule = Rule(self, r) except InvalidPatternException as e: logger.error("Failed to load rule '%s': Invalid patterns: %s", r.name, e) continue # Apply cloning rules rs = [rule] for cr in cloning_rules: if cr.match(rule): try: rs += [Rule(self, r, cr)] cn += 1 except InvalidPatternException as e: logger.error("Failed to clone rule '%s': Invalid patterns: %s", r.name, e) continue # Build chain for rule in rs: # Find profile restrictions rule_profiles = rx_profiles.get(rule.profile) if not rule_profiles: rx = re.compile(rule.profile) rule_profiles = [p for p in profiles if rx.search(p)] rx_profiles[rule.profile] = rule_profiles # Apply rules to appropriative chains for p in rule_profiles: rules[p, rule.chain] += [rule] n += 1 if cn: logger.info("%d rules are cloned", cn) self.default_rule = Rule( self, EventClassificationRule.objects.filter(name=config.classifier.default_rule).first(), ) # Apply lookup solution self.rules = dict((k, self.lookup_cls(rules[k])) for k in rules) logger.info("%d rules are loaded in the %d chains", n, len(self.rules)) # self.load_enumerations()