""" Get information about executed cron commands - number of commands, list of commands, and list of commands per user. Uses either journald or plain logfiles (path specified in config). 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 `cron_journald` instead of `cron`. NOTE: If using journald, the log level for cron.service should be at least 2 (default is 1). This can be changed with `sudo systemctl edit cron --full`, and ammend `-L 2` to the ExecStart command. TODO: also output a list of scheduled (future) jobs """ import datetime import re from logparse.formatting import * from logparse.util import readlog from logparse import config from logparse.load_parsers import Parser class CronCommand: """ Class representing a single cron session. Assigns its own variables of date, user and cmd when given a `systemd.journal.Record` object or a plain log message string on initialisation. NOTE: This class is used in both `cron.py` and `cron_journald.py`. """ def __init__(self, record, datefmt=""): """ Parse the date, user and command from the logfile string or record """ 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.user, self.cmd = re.search( r"^(?P