rename parsers, better journald integration
[logparse.git] / logparse / parsers / smbd_journald.py
index 257b4438f2b88321b95ce3d6917d8659d84c8297..4f8a978e93ab77ac064157e8e751b1513f79540e 100644 (file)
@@ -15,11 +15,13 @@ from logparse.util import LogPeriod, resolve
 class SmbdJournald(Parser):
 
     def __init__(self):
+
         super().__init__()
         self.name = "smbd_journald"
         self.info = "Get login statistics for a samba server."
 
     def parse_log(self):
+
         logger.debug("Starting smbd section")
         section = Section("smbd")
 
@@ -30,15 +32,22 @@ class SmbdJournald(Parser):
 
         messages = [entry["MESSAGE"] for entry in j if "MESSAGE" in entry]
 
-        total_auths = 0     # total number of logins for all users and all shares
-        shares = {}         # file shares (each share is mapped to a list of user-hostname pairs)
+        total_auths = 0     # total no. of logins for all users and all shares
+
+        shares = {}         # file shares (each share is mapped to a list of
+                            # user-hostname pairs)
 
         logger.debug("Found {0} samba logins".format(str(len(messages))))
+        logger.debug("Parsing data")
 
         for msg in messages:  # one log file for each client
 
             if "connect to service" in msg:
-                entry = re.search('(\w*)\s*\(ipv.:(.+):.+\) connect to service (\S+) initially as user (\S+)', msg)  # [('client', 'ip', 'share', 'user')]
+
+                # Generate list of [('client', 'ip', 'share', 'user')]
+                entry = re.search("(\w*)\s*\(ipv.:(.+):.+\) connect to service"
+                    "(\S+) initially as user (\S+)", msg)
+
                 try:
                     client, ip, share, user = entry.group(1,2,3,4)
                 except:
@@ -55,13 +64,15 @@ class SmbdJournald(Parser):
 
                 if (not client.strip()):
                     client = ip
-                userhost = user + '@' + resolve(client, fqdn=config.prefs.get("smbd", "smbd-resolve-domains"))
+                userhost = user + '@' + resolve(client, 
+                        fqdn=config.prefs.get("smbd", "smbd-resolve-domains"))
 
                 user_match = False
                 for pattern in config.prefs.get("smbd", "users").split():
                     user_match = re.fullmatch(pattern, userhost) or user_match
                 if not user_match:
-                    logger.debug("Ignoring login to {0} by user {1} due to config".format(share, userhost))
+                    logger.debug("Ignoring login to {0} by user {1} "
+                        "due to config".format(share, userhost))
                     continue
 
                 total_auths += 1
@@ -70,16 +81,22 @@ class SmbdJournald(Parser):
                 else:
                     shares[share] = [userhost]
 
-        section.append_data(Data(subtitle="Total of {0} authentications".format(str(total_auths))))
+        # Format Data() objects
+
+        section.append_data(Data(subtitle="Total of {0} authentications"
+            .format(str(total_auths))))
 
         for share, logins in shares.items():
             share_data = Data()
             share_data.items = logins
             share_data.orderbyfreq()
             share_data.truncl(config.prefs.getint("logparse", "maxlist"))
-            share_data.subtitle = share +  " ({0}, {1})".format(plural("user", len(share_data.items)), plural("login", len(logins)))
+            share_data.subtitle = share +  " ({0}, {1})".format(
+                    plural("user", len(share_data.items)),
+                    plural("login", len(logins)))
             section.append_data(share_data)
-            logger.debug("Found {0} logins for share {1}".format(str(len(logins)), share))
+            logger.debug("Found {0} logins for share {1}".format(
+                str(len(logins)), share))
 
         logger.info("Finished smbd section")
         return section