From dd18a75a10e08507c20c07790252666250f2e272 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 16 Feb 2021 20:05:57 +0700 Subject: [PATCH 1/3] vak-1505 added --reject param. --- commands/parse-events.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/commands/parse-events.py b/commands/parse-events.py index 3750ab4f3d..47ce526887 100755 --- a/commands/parse-events.py +++ b/commands/parse-events.py @@ -1,7 +1,7 @@ # ---------------------------------------------------------------------- # parse-events command # ---------------------------------------------------------------------- -# Copyright (C) 2007-2019 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # ---------------------------------------------------------------------- @@ -24,16 +24,24 @@ from noc.fm.models.activeevent import ActiveEvent from noc.core.fileutils import iter_open from noc.core.text import format_table from noc.core.perf import metrics +from noc.sa.models.profile import Profile class Command(BaseCommand): def add_arguments(self, parser): + parser.add_argument("paths", nargs="+", help="List of input file paths") parser.add_argument("--profile", default="Generic.Host", help="Object profile") parser.add_argument("--format", default="syslog", help="Input format") + parser.add_argument( + "--reject", + type=argparse.FileType("w", encoding="UTF-8"), + required=False, + metavar="FILE", + help="Path output file unknown data", + ) parser.add_argument("--progress", action="store_true", help="Display progress") - parser.add_argument("paths", nargs=argparse.REMAINDER, help="List of input file paths") - def handle(self, paths, profile, format, progress=False, *args, **options): + def handle(self, paths, profile, format, reject, progress=False, *args, **options): assert profile_loader.get_profile(profile), "Invalid profile: %s" % profile connect() t0 = time.time() @@ -43,7 +51,7 @@ class Command(BaseCommand): reader = getattr(self, "read_%s" % format, None) assert reader, "Invalid format %s" % format self.managed_object = ManagedObject( - id=1, name="test", address="127.0.0.1", profile_name=profile + id=1, name="test", address="127.0.0.1", profile=Profile.get_by_name("Generic.Host") ) t0 = time.time() stats = defaultdict(int) @@ -57,6 +65,8 @@ class Command(BaseCommand): if event.source == "SNMP Trap": e_vars.update(MIB.resolve_vars(event.raw_vars)) rule, r_vars = ruleset.find_rule(event, e_vars) + if reject and rule.is_unknown: + reject.write(f'{event.raw_vars["message"]}\n') stats[rule.event_class.name] += 1 total += 1 if progress and total % 1000 == 0: -- GitLab From ff5360c550f575e9b2f084bc2ba78f93ad8c483a Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Wed, 17 Feb 2021 17:38:15 +0700 Subject: [PATCH 2/3] vak-1505 set default value reject=None. --- commands/parse-events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/parse-events.py b/commands/parse-events.py index 47ce526887..4a17fab23c 100755 --- a/commands/parse-events.py +++ b/commands/parse-events.py @@ -41,7 +41,7 @@ class Command(BaseCommand): ) parser.add_argument("--progress", action="store_true", help="Display progress") - def handle(self, paths, profile, format, reject, progress=False, *args, **options): + def handle(self, paths, profile, format, reject=None, progress=False, *args, **options): assert profile_loader.get_profile(profile), "Invalid profile: %s" % profile connect() t0 = time.time() -- GitLab From 0a3bb5bdeed0f75d12fa34a8f41b26769dbb8169 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Sat, 20 Feb 2021 19:13:23 +0700 Subject: [PATCH 3/3] vak-1505 fixed arg getting Profile. --- commands/parse-events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/parse-events.py b/commands/parse-events.py index 4a17fab23c..d2acd35478 100755 --- a/commands/parse-events.py +++ b/commands/parse-events.py @@ -42,7 +42,7 @@ class Command(BaseCommand): parser.add_argument("--progress", action="store_true", help="Display progress") def handle(self, paths, profile, format, reject=None, progress=False, *args, **options): - assert profile_loader.get_profile(profile), "Invalid profile: %s" % profile + assert profile_loader.has_profile(profile), "Invalid profile: %s" % profile connect() t0 = time.time() ruleset = RuleSet() @@ -51,7 +51,7 @@ class Command(BaseCommand): reader = getattr(self, "read_%s" % format, None) assert reader, "Invalid format %s" % format self.managed_object = ManagedObject( - id=1, name="test", address="127.0.0.1", profile=Profile.get_by_name("Generic.Host") + id=1, name="test", address="127.0.0.1", profile=Profile.get_by_name(profile) ) t0 = time.time() stats = defaultdict(int) -- GitLab