"""
import re
-import glob
from systemd import journal
-from logparse.formatting import *
-from logparse.util import readlog, resolve
from logparse import config
+from logparse.formatting import *
from logparse.load_parsers import Parser
+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")
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:
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
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