Blame SOURCES/0009-Analyzer-support-parallel-requests-parsing.patch

9fb617
From d22ea2df62b6e245eef75d7201b678601bf63e98 Mon Sep 17 00:00:00 2001
9fb617
From: Justin Stephenson <jstephen@redhat.com>
9fb617
Date: Fri, 19 Aug 2022 14:44:11 -0400
9fb617
Subject: [PATCH 9/9] Analyzer: support parallel requests parsing
9fb617
MIME-Version: 1.0
9fb617
Content-Type: text/plain; charset=UTF-8
9fb617
Content-Transfer-Encoding: 8bit
9fb617
9fb617
Analyzer code(primarily the list verbose command) needs
9fb617
changes to handle parsing the necessary lines from
9fb617
NSS/PAM log files when multiple intermixed/parallel
9fb617
client requests are sent to SSSD.
9fb617
9fb617
Resolves: https://github.com/SSSD/sssd/issues/6307
9fb617
9fb617
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
9fb617
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
9fb617
9fb617
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
9fb617
---
9fb617
 src/tools/analyzer/modules/request.py | 119 +++++++++++++++-----------
9fb617
 1 file changed, 67 insertions(+), 52 deletions(-)
9fb617
9fb617
diff --git a/src/tools/analyzer/modules/request.py b/src/tools/analyzer/modules/request.py
9fb617
index 935e13adc..b9fe3caf8 100644
9fb617
--- a/src/tools/analyzer/modules/request.py
9fb617
+++ b/src/tools/analyzer/modules/request.py
9fb617
@@ -16,7 +16,6 @@ class RequestAnalyzer:
9fb617
     """
9fb617
     module_parser = None
9fb617
     consumed_logs = []
9fb617
-    done = ""
9fb617
     list_opts = [
9fb617
         Option('--verbose', 'Verbose output', bool, '-v'),
9fb617
         Option('--pam', 'Filter only PAM requests', bool),
9fb617
@@ -149,58 +148,74 @@ class RequestAnalyzer:
9fb617
                 print(line)
9fb617
         return found_results
9fb617
 
9fb617
-    def print_formatted(self, line, verbose):
9fb617
+    def print_formatted_verbose(self, source, patterns):
9fb617
+        """
9fb617
+        Parse line and print formatted verbose list_requests output
9fb617
+
9fb617
+        Args:
9fb617
+            source (Reader): source Reader object
9fb617
+            patterns (list): List of regex patterns to use for
9fb617
+                matching lines
9fb617
+        """
9fb617
+        # Get CID number, and print the basic line first
9fb617
+        for line in self.matched_line(source, patterns):
9fb617
+            cid = self.print_formatted(line)
9fb617
+
9fb617
+            # Loop through each line with this CID number to extract and
9fb617
+            # print the verbose data needed
9fb617
+            verbose_patterns = ["(cache_req_send|cache_req_process_input|"
9fb617
+                                "cache_req_search_send)"]
9fb617
+            for cidline in self.matched_line(source, verbose_patterns):
9fb617
+                plugin = ""
9fb617
+                name = ""
9fb617
+                id = ""
9fb617
+
9fb617
+                # skip any lines not pertaining to this CID
9fb617
+                if f"CID#{cid}]" not in cidline:
9fb617
+                    continue
9fb617
+                if "refreshed" in cidline:
9fb617
+                    continue
9fb617
+                # CR Plugin name
9fb617
+                if re.search("cache_req_send", cidline):
9fb617
+                    plugin = cidline.split('\'')[1]
9fb617
+                # CR Input name
9fb617
+                elif re.search("cache_req_process_input", cidline):
9fb617
+                    name = cidline.rsplit('[')[-1]
9fb617
+                # CR Input id
9fb617
+                elif re.search("cache_req_search_send", cidline):
9fb617
+                    id = cidline.rsplit()[-1]
9fb617
+
9fb617
+                if plugin:
9fb617
+                    print("   - " + plugin)
9fb617
+                if name:
9fb617
+                    print("       - " + name[:-2])
9fb617
+                if (id and ("UID" in cidline or "GID" in cidline)):
9fb617
+                    print("       - " + id)
9fb617
+
9fb617
+    def print_formatted(self, line):
9fb617
         """
9fb617
         Parse line and print formatted list_requests output
9fb617
 
9fb617
         Args:
9fb617
             line (str): line to parse
9fb617
-            verbose (bool): If true, enable verbose output
9fb617
+        Returns:
9fb617
+            Client ID from printed line, 0 otherwise
9fb617
         """
9fb617
-        plugin = ""
9fb617
-        name = ""
9fb617
-        id = ""
9fb617
-
9fb617
         # exclude backtrace logs
9fb617
         if line.startswith('   *  '):
9fb617
-            return
9fb617
-        fields = line.split("[")
9fb617
-        cr_field = fields[3][7:]
9fb617
-        cr = cr_field.split(":")[0][4:]
9fb617
+            return 0
9fb617
         if "refreshed" in line:
9fb617
-            return
9fb617
-        # CR Plugin name
9fb617
-        if re.search("cache_req_send", line):
9fb617
-            plugin = line.split('\'')[1]
9fb617
-        # CR Input name
9fb617
-        elif re.search("cache_req_process_input", line):
9fb617
-            name = line.rsplit('[')[-1]
9fb617
-        # CR Input id
9fb617
-        elif re.search("cache_req_search_send", line):
9fb617
-            id = line.rsplit()[-1]
9fb617
-        # CID and client process name
9fb617
-        else:
9fb617
-            ts = line.split(")")[0]
9fb617
-            ts = ts[1:]
9fb617
-            fields = line.split("[")
9fb617
-            cid = fields[3][4:-9]
9fb617
-            cmd = fields[4][4:-1]
9fb617
-            uid = fields[5][4:-1]
9fb617
-            if not uid.isnumeric():
9fb617
-                uid = fields[6][4:-1]
9fb617
-            print(f'{ts}: [uid {uid}] CID #{cid}: {cmd}')
9fb617
-
9fb617
-        if verbose:
9fb617
-            if plugin:
9fb617
-                print("   - " + plugin)
9fb617
-            if name:
9fb617
-                if cr not in self.done:
9fb617
-                    print("       - " + name[:-2])
9fb617
-                    self.done = cr
9fb617
-            if id:
9fb617
-                if cr not in self.done:
9fb617
-                    print("       - " + id)
9fb617
-                    self.done = cr
9fb617
+            return 0
9fb617
+        ts = line.split(")")[0]
9fb617
+        ts = ts[1:]
9fb617
+        fields = line.split("[")
9fb617
+        cid = fields[3][4:-9]
9fb617
+        cmd = fields[4][4:-1]
9fb617
+        uid = fields[5][4:-1]
9fb617
+        if not uid.isnumeric():
9fb617
+            uid = fields[6][4:-1]
9fb617
+        print(f'{ts}: [uid {uid}] CID #{cid}: {cmd}')
9fb617
+        return cid
9fb617
 
9fb617
     def list_requests(self, args):
9fb617
         """
9fb617
@@ -215,20 +230,20 @@ class RequestAnalyzer:
9fb617
         # Log messages matching the following regex patterns contain
9fb617
         # the useful info we need to produce list output
9fb617
         patterns = [r'\[cmd']
9fb617
-        patterns.append("(cache_req_send|cache_req_process_input|"
9fb617
-                        "cache_req_search_send)")
9fb617
         if args.pam:
9fb617
             component = source.Component.PAM
9fb617
             resp = "pam"
9fb617
 
9fb617
         logger.info(f"******** Listing {resp} client requests ********")
9fb617
         source.set_component(component, False)
9fb617
-        self.done = ""
9fb617
-        for line in self.matched_line(source, patterns):
9fb617
-            if isinstance(source, Journald):
9fb617
-                print(line)
9fb617
-            else:
9fb617
-                self.print_formatted(line, args.verbose)
9fb617
+        if args.verbose:
9fb617
+            self.print_formatted_verbose(source, patterns)
9fb617
+        else:
9fb617
+            for line in self.matched_line(source, patterns):
9fb617
+                if isinstance(source, Journald):
9fb617
+                    print(line)
9fb617
+                else:
9fb617
+                    self.print_formatted(line)
9fb617
 
9fb617
     def track_request(self, args):
9fb617
         """
9fb617
-- 
9fb617
2.37.1
9fb617