""" Get number of sudo sessions for each user NOTE: This parser supports reading from both journald and plain syslog files. By default the plain logfiles will be used, but the journald option is preferred for newer systems which support it. To use the journald mode, specify the parser as `sudo_journald` instead of `sudo`. """ import datetime import re from subprocess import Popen, PIPE from logparse.formatting import * from logparse.config import prefs from logparse.load_parsers import Parser class SudoCommand(): """ Class representing a single sudo log entry. Used for both sudo and sudo_journald, so it accepts either a dictionary output by systemd.Journal or a line from a logfile upon initialisation. """ def __init__(self, record, datefmt): """ Get instance variables from log message or record object """ if isinstance(record, str): if not datefmt: logger.error("Date format not provided - cannot parse this " "log message") # Parse from a raw logfile string self.date, self.init_user, self.pwd, self.su, self.command \ = re.search(r"^(?P