# # cron_journald.py # # List the logged (executed) cron jobs and their commands (uses journald module) # # TODO: also output a list of scheduled (future) jobs # from systemd import journal from logparse import config from logparse.formatting import * from logparse.load_parsers import Parser class CronJournald(Parser): def __init__(self): super().__init__() self.name = "cron_journald" self.info = "List the logged (executed) cron jobs and their commands (uses journald module)" def parse_log(self): logger.debug("Starting cron section") section = Section("cron") # Initiate journald reader j = journal.Reader() j.this_machine() j.log_level(journal.LOG_INFO) j.add_match(_COMM="cron") j.seek_realtime(section.period.startdate) logger.info("Obtaining cron logs") messages = [entry["MESSAGE"] for entry in j if "MESSAGE" in entry and " CMD " in entry["MESSAGE"]] total_jobs = len(messages) if total_jobs == 0: logger.warning("Couldn't find any cron commands") return 1 logger.info("Found " + str(total_jobs) + " cron jobs") section.append_data(Data("Total of " + plural("cron session", total_jobs) + " executed across all users")) logger.debug("Analysing cron commands for each user") users = {} for msg in messages: usr_cmd = re.search('\((\S+)\) CMD (.*)', msg) # [('user', 'cmd')] if usr_cmd: if not usr_cmd.group(1) in users: users[usr_cmd.group(1)] = [] users[usr_cmd.group(1)].append(usr_cmd.group(2)) for usr, cmdlist in users.items(): user_data = Data() user_data.subtitle = plural("cron session", len(cmdlist)) + " for " + usr user_data.items = ("`{0}`".format(cmd) for cmd in cmdlist) user_data.orderbyfreq() user_data.truncl(config.prefs.getint("logparse", "maxcmd")) section.append_data(user_data) logger.info("Finished cron section") return section